2016-06-27 2 views
0

Silicon Labs CP210x USB-UARTチップを使用するシリアルデバイスと通信しようとしています。私はドライバをインストールし、メーカーが提供するグラフィカルユーザインタフェースを使用していくつかのデバイスと正常に通信できます。libserialportがポートにデータを書き込まない

しかし、私は現在、libserialportを使用して、シリアル仕様のある特定のデバイスと通信しようとしています。私のコードは以下の通りです:

// find list of available devices 
sp_return status = sp_list_ports(&ports); 

// list available ports 
int j = 0; 
for (j = 0; ports[j]; j++) { 
    printf("Found port %d: %s\n", j, sp_get_port_name(ports[j])); 
} 

// connect to serial port 
struct sp_port *port = ports[1]; // /dev/cu.SLAB_USBtoUART 
sp_set_baudrate(port, 9600); 
sp_set_bits(port, 8); 
sp_set_stopbits(port, 1); 
sp_set_parity(port, SP_PARITY_NONE); 
status = sp_open(port, SP_MODE_READ_WRITE); 

// attempt first write 
uint8_t buf[] = {0x02, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}; 
status = sp_blocking_write(port, buf, 10, 0); 
printf("Bytes written: %d\n", status); 

コードは、私は明確にするためにそれを削除しているが、各機能が正常に返すことを確認するためにチェック広範囲の誤差を含んでいます。上記のコードは、10バイトをシリアルポートに書き込んだことを正常に表示します。

しかし、デバイスを式から削除するには、オシロスコープとパルス検出器でシリアル信号を接続し、シリアルラインに書き込まれているバイトがないことを確認してください(libserialportが書き込んだ10バイト)。

誰もこの問題に遭遇しましたか?これはOS X 10.11で動作しています

私はデバイスドライバを再インストールし、手動でechocatでバイトを書き込もうとしましたが、最終結果はワイヤに書き込まれていません。ヘッダファイルから

答えて

0

:libserialport.h:あなたはsp_free_port()を使用し、 'ポート' 上のmallocを使用してする必要はありません

enum sp_return sp_list_ports(struct sp_port ***list_ptr); 

/** 
* Make a new copy of an sp_port structure. 
* 
* The user should allocate a variable of type "struct sp_port *" and pass a 
* pointer to this to receive the result. 
* 
* The copy should be freed after use by calling sp_free_port(). 
* 
* @param[in] port Pointer to a port structure. Must not be NULL. 
* @param[out] copy_ptr If any error is returned, the variable pointed to by 
*      copy_ptr will be set to NULL. Otherwise, it will be set 
*      to point to the newly allocated copy. Must not be NULL. 
* 
* @return SP_OK upon success, a negative error code otherwise. 
* 
* @since 0.1.0 
*/ 

0

ここで私は仕事をしています。私は送信されたものを読むためにミニコムを使用していましたが、9600ボーより速く動作するようにはできませんでした。

#include <stdio.h> 
#include <stdlib.h> 
#include "libserialport.h" 

typedef unsigned int uint8_t; 


int main(void) 
{ 

struct sp_port *ports; 
struct sp_port **psp_ports; 
int i; 
int j; 
uint8_t mychar; 

ports = (struct sp_port*)malloc(sizeof(ports)*2); 
psp_ports = &ports; 
int status = sp_list_ports(&psp_ports); 
printf("sp_list_ports returned %d\n",status); 
// list available ports 
printf("port: %s\n",sp_get_port_name(psp_ports[0])); 
printf("port: %s\n",sp_get_port_name(psp_ports[1])); 
j = 0; 
sp_set_baudrate(psp_ports[j], 9600); 
sp_set_bits(psp_ports[j], 8); 
sp_set_stopbits(psp_ports[j], 1); 
sp_set_parity(psp_ports[j], SP_PARITY_NONE); 
status = sp_open(psp_ports[j], SP_MODE_READ_WRITE); 
printf("status = %d\n",status); 
if(status < 0) 
    return 0; 

for(i = 0;i < 100;i++) 
{ 
    for(mychar = 0x21;mychar < 0x7e;mychar++) 
     status = sp_nonblocking_write(psp_ports[j], &mychar, 1); 
}  
sp_free_port(psp_ports[j]); 
sp_close(psp_ports[j]); 
return 0; 
} 

githubで検索したところ、libserialportの使用例が見つかりませんでした。がんばろう。

関連する問題