シリアル接続でread()関数を処理しようとしています。シリアルread()はデータを受け取らない値を返しません
は、私は次の設定でシリアルポートを初期化します。
bool HardwareSerial::begin(speed_t speed) {
int USB = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
if (USB == 1) {
std::cout << "\n Error! in Opening ttyUSB0\n" << std::endl;
} else {
std::cout << "\n ttyUSB0 Opened Successfully\n" << std::endl;
}
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) speed);
cfsetispeed(&tty, (speed_t) speed);
// Setting other Port Stuff
tty.c_cflag &= ~PARENB; // Make 8n1
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
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;
}
_USB = USB;
return true;
}
その後、私は定期的にクラスのメンバは、()ストリームが読み込み呼び出す関数を読んで呼び出す:ポートがデータの読み出しを受け
int HardwareSerial::read() {
int n = 0;
char buf;
n = ::read(_USB, &buf, 1);
std::cout << std::hex << static_cast<int> (buf) << " n:";
std::cout << n << std::endl;
}
ながら()期待どおりに動作し、着信バイトを出力します。しかし、私がバイトを送るのを止めれば、プログラムはいくつかのバイトが受信されなくなるまでハングします。 私は:: readは0を返しますが、何も返さず、入ってくるデータを待つことを期待しています。新しいデータが受信されると、プログラムは動作を継続し、:: readは1を返します。
私は設定で迷ったのですか? 違うVMINとVTIMEを試しましたが、結果は同じです。