获取读取前串口等待的字节数,linux
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5900216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the count of bytes waiting on a serial port before reading, linux
提问by Steven smethurst
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 上,如何在不将串行端口缓冲区中删除的情况下读取在串行端口上等待的字节/字符的数量?
采纳答案by Chris Stratton
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)
这篇文档非常值得一读,因为它和许多其他问题(规范与原始模式等)
回答by Cédric Julien
In C language you can ask this with an ioctl :
在 C 语言中,您可以使用 ioctl 询问:
int bytes_avaiable;
ioctl(serial_file_descriptor, FIONREAD, &bytes_available);