Issue with read() and flush()

Sebastian Huber sebastian.huber at
Fri Mar 3 06:48:30 UTC 2023

Hello Jeff,

On 02.03.23 22:57, jeff.hatton at wrote:
> I am working on a project that uses the RTEMS OS on a Leon3 processor. I 
> am trying to read/write from a UART that is connected up through the 
> termios interface. It works for the most part but I am running into an 
> issue with flushing and reading from the UART. It seems if I read a 
> small portion of the data use read() then flush then read again there is 
> still data returned. Is this expected behavior? If so is there any way 
> to clear out whatever is cached in read?
> Pseudo Code
> Open UART with open("/dev/console_b", O_RDWR | O_NONBLOCK); setup 
> terminal into raw mode with cfmakeraw() and tcsetattr(fd,
> Setup the UART into loop back mode so its TX data is looped back into 
> its RX Write data into UART with write(fd, "TEST",4) Read a single 
> character with read(fd, buff, 1); buff will contain "T" here flush 
> buffers with tcflush(fd, TCIOFLUSH) Read a more from UART with read(fd, 
> buff, 3); Would expect nothing is returned here but "EST" will be returned.

this could be a limitation of the current Termios implementation.  It 
doesn't flush device buffers:

static void
flushInput (struct rtems_termios_tty *tty)
   rtems_termios_device_context *ctx = tty->device_context;
   rtems_interrupt_lock_context lock_context;

   rtems_termios_device_lock_acquire (ctx, &lock_context);
   tty->rawInBuf.Tail = 0;
   tty->rawInBuf.Head = 0;
   rtems_termios_device_lock_release (ctx, &lock_context);

If the characters are still in the UART FIFO, then they are not flushed.

This could be fixed by calling the device ioctl handler in this function 
and additional device-specific handling.

embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
email: sebastian.huber at
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:

More information about the users mailing list