これは、シリアルポートを開くための私の関数のようです(Ubuntu 12.04を使用):
int open_port(void)
{
int fd; /* File descriptor for the port */
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
// Could not open the port.
perror("open_port: Unable to open /dev/ttyUSB0 - ");
}
else
fcntl(fd, F_SETFL, 0);
struct termios options;
tcgetattr(fd, &options);
//setting baud rates and stuff
cfsetispeed(&options, B19200);
cfsetospeed(&options, B19200);
options.c_cflag |= (CLOCAL | CREAD);
tcsetattr(fd, TCSANOW, &options);
tcsetattr(fd, TCSAFLUSH, &options);
options.c_cflag &= ~PARENB;//next 4 lines setting 8N1
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
//options.c_cflag &= ~CNEW_RTSCTS;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //raw input
options.c_iflag &= ~(IXON | IXOFF | IXANY); //disable software flow control
return (fd);
}
問題は、このプログラムを実行したときに、シリアル デバイスが既に接続されている場合、バッファにコンテンツが含まれていることです。バッファーから読み取りを開始する前に、バッファーをクリアする方法が必要です。ポートを初期化する前にIOバッファをフラッシュすることで、この問題を解決できると思いtcsetattr(fd, TCSAFLUSH, &options);
ましたが、そのような運はありませんでした。洞察はありますか?