可变大小消息的 VMIN 和 VTIME 终端设置 [英] VMIN and VTIME Terminal Settings for variable sized messages
问题描述
我正在尝试通过串行端口与设备连接.设备每 700 毫秒发送一个 10 字节
轮询作为心跳".每次我读到心跳时,我都必须用 12 字节
长的响应来回复.
I'm trying to interface with a device via the serial port. The device sends out a 10 byte
poll as a "heartbeat" every 700ms. Everytime I read the heartbeat I have to reply with a 12 byte
long response.
在此响应中,我可以请求设备在轮询之间发送特定数据.不同请求的数据量不同.有没有办法设置串行端口,使其无论大小如何都始终读取一个块中的消息?
Within this response I can request that the device sends particular data inbetween polls. The amount of data is different for different requests. Is there a way to set up the serial port such that it will always read messages in one chunk regardless of their size?
我目前的终端设置如下:
My current terminal settings are as follows:
int ttySetRaw(int fd, struct termios *prevTermios)
{
struct termios t;
if (tcgetattr(fd, &t) == -1)
return -1;
if (prevTermios != NULL)
*prevTermios = t;
t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);
t.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR | INPCK | ISTRIP | IXON | PARMRK);
t.c_oflag &= ~OPOST; /* Disable all output processing */
t.c_cc[VMIN] = 12; /* 12 chars at a time, enough for the poll and the reply to be sent/received in one chunk, when I change this I no longer receive the poll*/
t.c_cc[VTIME] = 10; /* maximum timeout 1 second */
t.c_cflag |= PARENB;
t.c_cflag |= PARODD;
if (tcsetattr(fd, TCSAFLUSH, &t) == -1)
return -1;
return 0;
}
我尝试更改 VMIN 和 VTIME,我认为设置 VTIME = 7 意味着它会读取缓冲区中的所有内容,直到 700 毫秒过去,但这失败了.当我希望设备在轮询间隔内发送另一条更长的消息时,这也是不够的.
I've tried changing VMIN and VTIME, I thought setting VTIME = 7 would mean it would read everything in the buffer until 700ms had passed, but this fails. It would also not be sufficient for the times when I want the device to send another, longer, message inside the polling interval.
是否有能够实现我想要的设置,或者我是否必须逐字节读取数据并一次一个字节地读取数据并在单独的函数中构建消息?
Is there a setup that would be able to achieve what I want, or am I going to have to byte the bullet and read the data one byte at a time and build the messages up in a separate function?
推荐答案
有没有办法设置串口,使其始终读取无论大小如何,消息都在一个块中?
Is there a way to set up the serial port such that it will always read messages in one chunk regardless of their size?
一般来说,没有.与 TCP 流一样,串行链路也是字节流,没有大于 1 个字节的消息边界.
In general, no. Like TCP streams, serial links are byte streams and have no message-boundaries bigger than one byte.
您需要一个允许从字节流中解析消息的协议.
You need a protocol that allows the messages to be parsed out of the byte stream.
也就是说,一些串行硬件和驱动程序允许发送和检测中断"信号,但我不知道还有人使用该功能,即使可用.
That said, some serial hardware and drivers allow a 'break' signal to be sent and detected, but I don't know anyone who uses that feture any more, even if available.
这篇关于可变大小消息的 VMIN 和 VTIME 终端设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!