如何使用C ++在Linux中的串行端口上检测缓冲区溢出 [英] how to detect a buffer over run on serial port in linux using c++
问题描述
我有一个大问题.目前,我正在通过以下挂钩访问串行端口:
I have a big problem. At present I am accessing a serial port via the following hooks:
fd = open( "/dev/ttyS1", O_RDWR | O_NOCTTY )
然后我使用下面的代码块从中读取
then I read from it using the following chunk of code
i = select( fd + 1, &rfds, NULL, NULL, &tv )
...
iLen = read( fd, buf, MAX_PACKET_LEN )
问题在于,在阅读之前,我需要检测是否有任何缓冲区溢出.既在串行端口级别,又在内部tty翻转缓冲区中.
the problem is that before I read, I need to detect if there were any buffer overruns. Both at the serial port level and the internal tty flip buffers.
我们尝试了cat /proc/tty/driver/serial
,但它似乎没有列出超限(请参见下面的输出)
We tried cat /proc/tty/driver/serial
but it doesn't seem to list the overruns (see output below)
1: uart:16550A port:000002F8 irq:3 tx:70774 rx:862484 fe:44443 pe:270023 brk:30301 RTS|CTS|DTR
推荐答案
根据内核源代码,您应该使用TIOCGICOUNT ioctl.第三个ioctl参数应该是指向<linux/serial.h>
中定义的以下结构的指针:
According to the kernel sources, you should use the TIOCGICOUNT ioctl. The third ioctl argument should be a pointer to the following struct, defined in <linux/serial.h>
:
/*
* Serial input interrupt line counters -- external structure
* Four lines can interrupt: CTS, DSR, RI, DCD
*/
struct serial_icounter_struct {
int cts, dsr, rng, dcd;
int rx, tx;
int frame, overrun, parity, brk;
int buf_overrun;
int reserved[9];
};
我不知道每个驾驶员是否都能检测到所有情况.
I don't know if every driver detect all conditions however.
这篇关于如何使用C ++在Linux中的串行端口上检测缓冲区溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!