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);
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);
To copy to clipboard, switch view to plain text mode
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);
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);
To copy to clipboard, switch view to plain text mode
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();
qApp->processEvents();
To copy to clipboard, switch view to plain text mode
after writing in each ports with
port1->flush();
port1->flush();
To copy to clipboard, switch view to plain text mode
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();
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();
To copy to clipboard, switch view to plain text mode
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.
Bookmarks