Serial Port Programming Part 3 - Finding out the number of bytes available in serial read buffer

Consider the scenario in which I want to wait until 'n' number of bytes received on serial port before calling read, as read system call will remove the contents of the serial input buffer. Such situation can be in case where your packet length is 50 bytes and you want to wait until all the 50 bytes are received.

The FIONREAD ioctl returns the number of bytes present in the serial input buffer.

ioctl(fd, FIONREAD, &bytes);

Let's write a sample code to wait until 50 bytes are present in the serial input buffer and then perform read.

Two USB to serial devices should be connected (TX->RX, RX->TX) to run the program.

Code:



Run the noncanonical_write we wrote in previous post multiple times.While loop will exit when more than 50 bytes are present in the serial input buffer

Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

PR, PN and PV Variable in Yocto