整个上午我们一直在抨击这一个。我们已经在嵌入式Linux设备和Ubuntu盒子之间建立了一些串行线路。因为我们的代码通常会返回两个(有时更多,有时只是一个)消息读取,而不是每发送一个实际消息读取一条消息,所以我们的读取被搞砸了。Linux termios VTIME不工作?
这里是打开串口的代码。 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
现在,当我运行在接收计算机上的读出线程,“你好世界”通常会通过几条消息而分解。以下是一些示例输出:
1: Hello
2: world 1
3: Hello
4: world 2
5: Hello
6: world 3
其中number后跟一个冒号是一个消息收到。你能看到我们正在犯的任何错误吗?
谢谢。请致电section 3.2 of the Linux Serial Programming HOWTO。根据我的理解,用几秒钟的VTIME(意味着vtime设置在10到50之间的任何地方,试错)以及1的VMIN,应该没有理由将消息分解成两个单独的消息。
你需要使用某种简单的组帧方案。 – caf 2009-07-16 08:31:30
我试过令人难以置信的高VTIME值,大约4或5秒。我尝试过很多值。如果你参考http://www.faqs.org/docs/Linux-HOWTO/Serial-Programming-HOWTO.html 3.2节的内容,你会明白我为什么会“惊讶” – 2009-07-16 12:09:49