Serial Port Programming Part 5 - tcflush - TCIFLUSH,TCOFLUSH example

tcflush flushes/discards
  • data which is written but not transmitted and/or 
  • data which is received but not read by user
The exact operation depends on the 'queue_selector' argument .


#include <termios.h>
int tcflush(int fildes, int queue_selector);

Possible values of queue_selector are:

TCIFLUSH - Flushes/Discards received data, but not read
TCOFLUSH - Flushes/Discards written data, but not transmitted
TCIOFLUSH - Flushes/Discards both

Let's write a sample code to check what happens when we
1. Loop until there are bytes in input buffer using 'FIONREAD' ioctl 
2. Call tcflush with queue selector 'TCIFLUSH'
3. Check how many bytes are present in input buffer with 'FIONREAD' ioctl

Need two USB to serial/serial devices connected (TX-RX/RX-TX)

Code:



Output:



Same behavior will happen when we call write on the serial port with large number of bytes, and in the next statement call tcflush with queue selector 'TCOFLUSH' not all data will be received by the other serial port.

Receive Code: (fionread)



TransmitCode:(tcflush_output)



Output:



Comments

Popular posts from this blog

bb.utils.contains yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig

PR, PN and PV Variable in Yocto