8
votes

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?

2
I'm facing the opposite problem, (converting from Linux to Windows), do you have a link, code or documentation about COMSTATS ?Fifi

2 Answers

12
votes

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

5
votes

In C language you can ask this with an ioctl :

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