Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 950
Steven Smethurst Avatar asked May 05 '11 15:05

Steven Smethurst


2 Answers

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

like image 121
Chris Stratton Avatar answered Oct 25 '22 13:10

Chris Stratton


In C language you can ask this with an ioctl :

int bytes_avaiable;
ioctl(serial_file_descriptor, FIONREAD, &bytes_available);
like image 39
Cédric Julien Avatar answered Oct 25 '22 13:10

Cédric Julien