Get the count of bytes waiting on a serial port before reading, linux

半腔热情 提交于 2019-12-22 04:13:20

问题


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.

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.

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?


回答1:


You need to use an 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:


In C language you can ask this with an ioctl :

int bytes_avaiable;
ioctl(serial_file_descriptor, FIONREAD, &bytes_available);


来源:https://stackoverflow.com/questions/5900216/get-the-count-of-bytes-waiting-on-a-serial-port-before-reading-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!