45

FTDI を使用して USB ポート経由でデータを送受信しようとしているため、C/C++ を使用してシリアル通信を処理する必要があります。Linux(Ubuntu)で作業しています。

基本的に、着信コマンドをリッスンしているデバイスに接続しています。これらのコマンドを送信し、デバイスの応答を読み取る必要があります。コマンドもレスポンスもASCII文字です。

GtkTerm を使用するとすべて正常に動作しますが、C プログラミングに切り替えると問題が発生します。

これが私のコードです:

#include <stdio.h>      // standard input / output functions
#include <stdlib.h>
#include <string.h>     // string function definitions
#include <unistd.h>     // UNIX standard function definitions
#include <fcntl.h>      // File control definitions
#include <errno.h>      // Error number definitions
#include <termios.h>    // POSIX terminal control definitions

/* Open File Descriptor */
int USB = open( "/dev/ttyUSB0", O_RDWR| O_NONBLOCK | O_NDELAY );

/* Error Handling */
if ( USB < 0 )
{
cout << "Error " << errno << " opening " << "/dev/ttyUSB0" << ": " << strerror (errno) << endl;
}

/* *** Configure Port *** */
struct termios tty;
memset (&tty, 0, sizeof tty);

/* Error Handling */
if ( tcgetattr ( USB, &tty ) != 0 )
{
cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << endl;
}

/* Set Baud Rate */
cfsetospeed (&tty, B9600);
cfsetispeed (&tty, B9600);

/* Setting other Port Stuff */
tty.c_cflag     &=  ~PARENB;        // Make 8n1
tty.c_cflag     &=  ~CSTOPB;
tty.c_cflag     &=  ~CSIZE;
tty.c_cflag     |=  CS8;
tty.c_cflag     &=  ~CRTSCTS;       // no flow control
tty.c_lflag     =   0;          // no signaling chars, no echo, no canonical processing
tty.c_oflag     =   0;                  // no remapping, no delays
tty.c_cc[VMIN]      =   0;                  // read doesn't block
tty.c_cc[VTIME]     =   5;                  // 0.5 seconds read timeout

tty.c_cflag     |=  CREAD | CLOCAL;     // turn on READ & ignore ctrl lines
tty.c_iflag     &=  ~(IXON | IXOFF | IXANY);// turn off s/w flow ctrl
tty.c_lflag     &=  ~(ICANON | ECHO | ECHOE | ISIG); // make raw
tty.c_oflag     &=  ~OPOST;              // make raw

/* Flush Port, then applies attributes */
tcflush( USB, TCIFLUSH );

if ( tcsetattr ( USB, TCSANOW, &tty ) != 0)
{
cout << "Error " << errno << " from tcsetattr" << endl;
}

/* *** WRITE *** */

unsigned char cmd[] = {'I', 'N', 'I', 'T', ' ', '\r', '\0'};
int n_written = write( USB, cmd, sizeof(cmd) -1 );

/* Allocate memory for read buffer */
char buf [256];
memset (&buf, '\0', sizeof buf);

/* *** READ *** */
int n = read( USB, &buf , sizeof buf );

/* Error Handling */
if (n < 0)
{
     cout << "Error reading: " << strerror(errno) << endl;
}

/* Print what I read... */
cout << "Read: " << buf << endl;

close(USB);

何が起こるかというと、read()0 (バイトがまったく読み取られない) を返すか、タイムアウトになるまでブロックする ( VTIME) ことです。write()何も送信しないため、これが発生すると想定しています。その場合、デバイスはコマンドを受信せず、応答を受信できません。実際、プログラムの読み取りがブロックされている間にデバイスの電源を切ると、実際に応答を得ることができました (デバイスはシャットダウン中に何かを送信します)。

奇妙なことは、これを追加することです

cout << "I've written: " << n_written << "bytes" << endl; 

write()電話の直後に、私は受け取ります:

I've written 6 bytes

これはまさに私が期待するものです。私のデバイスが実際にポートに書き込んでいるものを受信できないなど、私のプログラムだけが正常に機能しません。

データ型に関しても、さまざまなことと解決策を試しました(cmd = "INIT \r"orなどの std::string を使用してみましconst charた)が、実際には何も機能しませんでした。

誰かが私が間違っている場所を教えてもらえますか?

前もって感謝します。

編集: このコードの以前のバージョンが使用されました

unsigned char cmd[] = "INIT \n"

そしてまたcmd[] = "INIT \r\n"。デバイスのコマンド sintax が次のように報告されているため、変更しました。

<command><SPACE><CR>.

また、読み取り時にフラグを回避しようとしましO_NONBLOCKたが、その後は永遠にブロックするだけです。使用してみましselect()たが、何も起こりません。試しに、データが利用可能になるまで待機ループを作成しましたが、私のコードはループを終了しません。ところで、待っているか、usleep()私が避ける必要があるものです。報告されたものは、私のコードの抜粋にすぎません。完全なコードはリアルタイム環境(特に OROCOS) で動作する必要があるため、スリープのような機能はあまり必要ありません。

4

3 に答える 3

74

私は自分の問題を解決したので、誰かが同様のものを必要とする場合に備えて、正しいコードをここに投稿します。

ポートを開く

int USB = open( "/dev/ttyUSB0", O_RDWR| O_NOCTTY );

パラメータの設定

struct termios tty;
struct termios tty_old;
memset (&tty, 0, sizeof tty);

/* Error Handling */
if ( tcgetattr ( USB, &tty ) != 0 ) {
   std::cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << std::endl;
}

/* Save old tty parameters */
tty_old = tty;

/* Set Baud Rate */
cfsetospeed (&tty, (speed_t)B9600);
cfsetispeed (&tty, (speed_t)B9600);

/* Setting other Port Stuff */
tty.c_cflag     &=  ~PARENB;            // Make 8n1
tty.c_cflag     &=  ~CSTOPB;
tty.c_cflag     &=  ~CSIZE;
tty.c_cflag     |=  CS8;

tty.c_cflag     &=  ~CRTSCTS;           // no flow control
tty.c_cc[VMIN]   =  1;                  // read doesn't block
tty.c_cc[VTIME]  =  5;                  // 0.5 seconds read timeout
tty.c_cflag     |=  CREAD | CLOCAL;     // turn on READ & ignore ctrl lines

/* Make raw */
cfmakeraw(&tty);

/* Flush Port, then applies attributes */
tcflush( USB, TCIFLUSH );
if ( tcsetattr ( USB, TCSANOW, &tty ) != 0) {
   std::cout << "Error " << errno << " from tcsetattr" << std::endl;
}

書く

unsigned char cmd[] = "INIT \r";
int n_written = 0,
    spot = 0;

do {
    n_written = write( USB, &cmd[spot], 1 );
    spot += n_written;
} while (cmd[spot-1] != '\r' && n_written > 0);

バイトごとに書き込む必要はまったくなく、問題なく機能しint n_written = write( USB, cmd, sizeof(cmd) -1)ました。

最後に、読んでください:

int n = 0,
    spot = 0;
char buf = '\0';

/* Whole response*/
char response[1024];
memset(response, '\0', sizeof response);

do {
    n = read( USB, &buf, 1 );
    sprintf( &response[spot], "%c", buf );
    spot += n;
} while( buf != '\r' && n > 0);

if (n < 0) {
    std::cout << "Error reading: " << strerror(errno) << std::endl;
}
else if (n == 0) {
    std::cout << "Read nothing!" << std::endl;
}
else {
    std::cout << "Response: " << response << std::endl;
}

これは私のために働いた。皆さん、ありがとうございました!

于 2013-08-08T19:53:35.943 に答える
0

1) init の後に /n を追加します。すなわちwrite( USB, "init\n", 5);

2) シリアル ポートの構成を再確認します。そこに何かが間違っている可能性があります。^Q/^S やハードウェア フロー制御を使用していないからといって、相手側がそれを期待していないわけではありません。

3)最も可能性が高い: write()の後に"usleep(100000); を追加します。ファイル記述子は、ブロックまたは待機しないように設定されていますよね? read を呼び出す前に、応答を取得するのにどのくらいの時間がかかりますか? ( read()する前に、システムのハードウェア割り込みを介して、カーネルによって受信され、バッファリングされる必要があります

追加するために編集:

DTR/RTS回線は必要ですか? コンピュータデータを送信するよう相手側に指示するハードウェアフロー制御? 例えば

int tmp, serialLines;

cout << "Dropping Reading DTR and RTS\n";
ioctl ( readFd, TIOCMGET, & serialLines );
serialLines &= ~TIOCM_DTR;
serialLines &= ~TIOCM_RTS;
ioctl ( readFd, TIOCMSET, & serialLines );
usleep(100000);
ioctl ( readFd, TIOCMGET, & tmp );
cout << "Reading DTR status: " << (tmp & TIOCM_DTR) << endl;
sleep (2);

cout << "Setting Reading DTR and RTS\n";
serialLines |= TIOCM_DTR;
serialLines |= TIOCM_RTS;
ioctl ( readFd, TIOCMSET, & serialLines );
ioctl ( readFd, TIOCMGET, & tmp );
cout << "Reading DTR status: " << (tmp & TIOCM_DTR) << endl;
于 2013-08-07T19:07:19.940 に答える
0

一部の受信者は、通常は 2 文字\r\nの EOL シーケンスを想定しているため、コード内で行を置き換えてみてください。

unsigned char cmd[] = {'I', 'N', 'I', 'T', ' ', '\r', '\0'};

unsigned char cmd[] = "INIT\r\n";

ところで、上記の方法はおそらくより効率的です。すべての文字を引用する必要はありません。

于 2013-08-07T16:43:29.633 に答える