シリアル ポートを使用してカスタム ハードウェアからデータを読み込もうとしています。構成は正しかったと思います。問題は、一部のバイトが欠落していることです。データでは、データの長さが指定されているので、一部のデータが欠落しているかどうかがわかります。
私の構成は次のとおりです。
int set_interface_attribs (int fd, int speed, int parity)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0) return -1;
cfsetospeed (&tty, speed);
cfsetispeed (&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // raw input
tty.c_cc[VMIN] = 1; // read does block
tty.c_cc[VTIME] = 0; // 0 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl (flow control)
//disable hw flow control
#ifdef CRTSCTS
tty.c_cflag &= ~CRTSCTS;
#endif
#ifdef CNEW_RTSCTS
tty.c_cflag &= ~CNEW_RTSCTS;
#endif
tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_iflag &= ~(ISTRIP | IGNCR | INLCR | ICRNL
#ifdef IUCLC
| IUCLC
#endif
);
tty.c_oflag &= ~(OPOST
#ifdef ONLCR
| ONLCR
#endif
#ifdef OCRNL
| OCRNL
#endif
#ifdef ONOCR
| ONOCR
#endif
#ifdef ONLRET
| ONLRET
#endif
);
if (tcsetattr (fd, TCSANOW, &tty) != 0) return -1;
return 0;
}
これは私がそれを読む方法です:
int fd = open (portname, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
char buf [10];
while(1) {
int n = 0;
while (n = read (fd, buf, sizeof buf) > 0) {
write(STDOUT_FILENO, buf, n); //this printing
.. other job ..
}
}
以下はデータの例です:
// correct data
#*69880001000800010007003F4530302403322402FE24080524000024012C2400142430303030302452B4FF
// example of lost data (lost data is signed by _ )
#*69880001000800010007003F4530302403322402FE2408092_000_24012C240014243030303030241674FF
#*69880001000800010007003F4530302403312402FF24080524_00024012C240014243030303030244EF9FF
構成に必要なものは次のとおりです: ボーレート: 19200 データ ビット: 8 ストップ ビット: 1 パリティ: なし フロー制御: なし
参考までに、私のハードウェアはバイトごとにデータを送信しています。したがって、上記のコードのnは常に 1 (または 0) です。
パテを使ってみると、大丈夫です。私が逃したものはありますか?
ありがとうございました