整个上午我们都在敲打我们的脑袋。我们在嵌入式 linux 设备和 Ubuntu 机器之间设置了一些串行线路。因为我们的代码通常会返回两个(有时更多,有时正好是一个)消息读取,而不是每个实际发送的消息读取一个消息,所以我们的读取操作已经搞砸了。
这是打开串行端口的代码。InterCharTime 设置为 4。
void COMClass::openPort()
{
struct termios tio;
this->fd = -1;
int tmpFD;
tempFD = open( port, O_RDWR | O_NOCTTY);
if (tempFD < 0)
{
cerr<< "the port is not opened"<< port <<"\n";
portOpen = 0;
return;
}
tio.c_cflag = BaudRate | CS8 | CLOCAL | CREAD ;
tio.c_oflag = 0;
tio.c_iflag = IGNPAR;
newtio.c_cc[VTIME] = InterCharTime;
newtio.c_cc[VMIN] = readBufferSize;
newtio.c_lflag = 0;
tcflush(tempFD, TCIFLUSH);
tcsetattr(tempFD,TCSANOW,&tio);
this->fd = tempFD;
portOpen = true;
}
另一端为通信进行了类似的配置,并且有一小部分特定的 iterest:
while (1)
{
sprintf(out, "\r\nHello world %lu", ++ulCount);
puts(out);
WritePort((BYTE *)out, strlen(out)+1);
sleep(2);
} //while
现在,当我在接收机器上运行一个读取线程时,“hello world”通常被分成几条消息。这是一些示例输出:
1: Hello
2: world 1
3: Hello
4: world 2
5: Hello
6: world 3
其中数字后跟冒号是收到的一条消息。你能看出我们犯了什么错误吗?
谢谢你。
编辑:为清楚起见,请查看Linux Serial Programming HOWTO 的第 3.2 节。据我了解,VTIME 为几秒钟(意味着 vtime 设置在 10 到 50 之间的任意位置,试错),VMIN 为 1,应该没有理由将消息分解为两条单独的消息.