I am converting a Win32 serial class to Linux (Ubuntu) one of the required functions of this serial class is to "peek" at the serial buffer to see how many bytes are waiting on the serial port before reading the serial port.
我正在将Win32串行类转换为Linux(Ubuntu)这个串行类所需的功能之一是“查看”串行缓冲区,以查看在读取串行端口之前在串行端口上等待了多少字节。
In this pedicure situation I only need to know if there are ANY bytes on the port before attempting to read it.
在这种修脚情况下,我只需要知道端口上是否有任何字节,然后再尝试读取它。
In windows I used COMSTATS but I can't seem to find a similar function in Linux.
在Windows中我使用了COMSTATS,但我似乎无法在Linux中找到类似的功能。
My question is:
我的问题是:
On Linux How do you read the amount of BYTES/chars waiting on a serial port without removing them from the serial port buffer?
在Linux上如何读取等待串口的BYTES / chars数量而不将其从串口缓冲区中删除?
2 个解决方案
#1
11
You need to use an ioctl
你需要使用ioctl
ioctl(serial_fd, FIONREAD, &bytes_avail);
This document is very much worth reading, for that and many other issues (canonical vs raw mode, etc)
对于该文档和许多其他问题(规范与原始模式等),本文档非常值得一读。
http://www.cmrr.umn.edu/~strupp/serial.html
http://www.cmrr.umn.edu/~strupp/serial.html
#2
2
In C language you can ask this with an ioctl :
在C语言中,您可以使用ioctl来询问:
int bytes_avaiable;
ioctl(serial_file_descriptor, FIONREAD, &bytes_available);
#1
11
You need to use an ioctl
你需要使用ioctl
ioctl(serial_fd, FIONREAD, &bytes_avail);
This document is very much worth reading, for that and many other issues (canonical vs raw mode, etc)
对于该文档和许多其他问题(规范与原始模式等),本文档非常值得一读。
http://www.cmrr.umn.edu/~strupp/serial.html
http://www.cmrr.umn.edu/~strupp/serial.html
#2
2
In C language you can ask this with an ioctl :
在C语言中,您可以使用ioctl来询问:
int bytes_avaiable;
ioctl(serial_file_descriptor, FIONREAD, &bytes_available);