Hi folks,

I apologize if the following has already been discussed but googling
for old posts I didn't find what I was looking for.

My problem: I have to communicate with a device through a serial port.
Everything works properly if I communicate at 57600, but on the
attempt of changing baudrate (which also the remote device does), I
lose data coming from device.

I think the problem is a delay in one of the two (or both):
.. flushing function
.. new baud rate settings

My understanding is that tcdrain and tcflush care just about flushing
the output buffer, but there is no way of assuring that data coming
through serial are actually read before the baudrate exchange.
Am I wrong here? any hint?
The way I use tcflush / tcdrain is:
tcflush(fd, TCIFLUSH);

Changing the baudrate:

tcgetattr(fd, &newopt);
int baud;
switch (arg)
case 115200:
baud = B115200;
case 230400:
baud = B230400;
case 57600:
baud = B57600;

cfsetispeed(&newopt, baud);
cfsetospeed(&newopt, baud);

int err = tcsetattr(fd, TCSANOW, &newopt);

Anything obviously wrong here?

To my understanding what really happens is that the baudrate it is
changed, but it happens too slow and on the while I miss data coming
from the serial device.

Thank you for any hint.