lindebear lindebear - 5 months ago 29
Linux Question

Is there a way to inquire the amount of data available at a serial port?

I just started looking into serial data acquisition and was wondering if there is a way to check, if data is available at a given port and if so how much?

To be a little more specific: If anyone has ever looked into programming on an Arduino, one can easily check whether data is available for reading from the USB-serial interface via the call (from the Arduino API):

if(Serial.available() > 0) { ... }

This way one can avoid a possibly blocking read()-call and simply read data, whenever necessary or desired.

However given the inverse situation: "A program is checking for serial sent from the Arduino", the only way, I've seen so far, to achieve something similar in C, is to steer the behavior of read() through setting the
to specific values. That's not really what I want though... I don't want a non-blocking read()-call or a read()-call that only blocks if data is available. I want to know IF there is data to read to begin with, before even trying to read, and if so HOW MUCH data can be read (currently) from the serial.


Use the FIONREAD ioctl.

int nread;
ioctl(Serial, FIONREAD, &nread);
if (nread > 0) {