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
#2
2
In C language you can ask this with an ioctl :
在C語言中,您可以使用ioctl來詢問:
int bytes_avaiable;
ioctl(serial_file_descriptor, FIONREAD, &bytes_available);