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

Flushing.
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);
or
tcdrain(fd);

Changing the baudrate:

tcgetattr(fd, &newopt);
[..]
int baud;
switch (arg)
{
default:
case 115200:
baud = B115200;
break;
case 230400:
baud = B230400;
break;
case 57600:
baud = B57600;
break;
}

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

//tcdrain(fd);
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.
Mik