PDA

View Full Version : Synchronized writing in two serialport.



Atef
13th July 2018, 15:02
Hi All,


We are using a QT 5.10 and Ubuntu as OS.
We aim to communicate with an FTDI chip which have two (virtual) serial ports. Here is the initialization of the two ports:

port_1->setPortName("ttyUSB0");
port_1->setBaudRate(QSerialPort::Baud9600);
port_1->setDataBits(QSerialPort::Data8);
port_1->setParity(QSerialPort::NoParity);
port_1->setStopBits(QSerialPort::OneStop);
port_1->setFlowControl(QSerialPort::SoftwareControl);


port_2->setPortName("ttyUSB1");
port_2->setBaudRate(QSerialPort::Baud9600);
port_2->setDataBits(QSerialPort::Data8);
port_2->setParity(QSerialPort::NoParity);
port_2->setStopBits(QSerialPort::OneStop);
port_2->setFlowControl(QSerialPort::SoftwareControl);
port_2->setReadBufferSize(10);

Opening, reading and Closing port work properly.
Writing is each port work properly.
In our case, we always read from one port, but we write in two ports. The writing should be in a port after the other. Here is an example:

n_wrote1 = port1->write(cmd1, COUNT);
n_wrote2 = port2->write(cmd2, COUNT);

n_wrote3 = port1->write(cmd3, COUNT);
n_wrote4 = port2->write(cmd4, COUNT);

n_wrote5 = port1->write(cmd5, COUNT);
n_wrote6 = port2->write(cmd6, COUNT);

But when we use this previous code in a loop, it's look like that the writing is not anymore synchronized. Which mean that it can happen that the cmd1 and cmd3 is written in a the port1 before the port2 write the cmd2.

After looking in different post in this forum, I added
qApp->processEvents(); after writing in each ports with
port1->flush(); like in the example below


n_wrote1 = port1->write(cmd, COUNT);
port->waitForBytesWritten(30000);
qApp->processEvents();
port->flush();
qApp->processEvents();

n_wrote2 = port2->write(cmd, COUNT);
port->waitForBytesWritten(30000);
qApp->processEvents();
port->flush();
qApp->processEvents();


Applying these changes helped quite a bit, but from time to time these "glitches" still happen. So what i am looking is to undrestand what qApp->processEvents(); is doing exactely in my case? is it ok to call it many times in my code? and if there is a better way to fix this isssue?

Many thanks for your help.

nix
17th July 2018, 10:38
Hi,

First of all, regarding the last code example you provided, I can't find differences between waitForBytesWritten() and flush(). Both are blocking calls waiting for the data to be written inside low level layer (OS).

There is maybe another reason why you can't make it work as you wish. Qt doesn't actually write the data to the hardware, the OS do that.

Assuming you are using Linux, you have driver managing each serial port, this mean Qt can write the data to the serial port file but the OS scheduler and drivers are responsible of writing the data into the hardware and actually sending them (there will be some buffering and delaying in the process).

I think sending messages inside a loop on two serial ports and expect than they are sent in a specific order over two different hardwares is not possible (at least with a full reliability). You can only control order in a single serial port but not between two serial port.

Regards,