Serial port: flushing & changing baudrate problem



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

.



Relevant Pages

  • Re: Krumme Baudraten unter Windows (XP)
    ... > Ich habe mir mal vom Codeproject eine Klasse CSerialPort heruntergeladen ... > Baud verwendet. ... Üblicherweise besitzen die seriellen Schnittstellen einen Quarz, ... Baudrate heruntergeteilt. ...
    (microsoft.public.de.vc)
  • Re: Daten von COM-Port lesen
    ... daß das Hyperterminalprogramm sich irgendwie mit dem ... >> Scanner auf eine Baudrate einigt? ... Metrologic-Scanner und die lassen sich nur bis 38400 Baud programmieren. ...
    (microsoft.public.de.fox)
  • Re: 8051 Baudrate
    ... I have a 8051 compatible controller with 13MHz Osc. ... Is it true that 9600 is the fastest possible Baudrate that somehow fits to the standard baudrates of a PC? ... Newer ones have Baud Dividers, with higher ratio baud options, so can divide /16/N. ...
    (comp.arch.embedded)
  • Re: Micro choice with 4 UARTs
    ... >>>quickly (4800 baud to 19200 baud) and only a few hundred bytes per ... >> At that baudrate you could easily do it in software without too much ... >And one piece of bit bang code could run all serial ports running at the ... >same baud rate. ...
    (comp.arch.embedded)