PDA

View Full Version : QtSerialPort synchronous approach



codein
3rd September 2012, 19:58
Hi!
i'm trying to communicate some device that have been programmed myself to PC. Just a simple one to test QtSerialPort. In terminal example on QtSerialPort's help its work fine.
Here is the reading result :


308
307
309
308
.
.
.



But when i write my code using synchronous approach, the reading is terrible. Here is the code :


#include <QtCore/QCoreApplication>
#include <QtDebug>
#include <QtAddOnSerialPort/serialport.h>

QT_USE_NAMESPACE_SERIALPORT

void setup();
SerialPort serial;

int main(int argc, char *argv[])
{
serial.setPort("COM20");
serial.open(QIODevice::ReadOnly | QIODevice::Unbuffered);
setup();

QCoreApplication a(argc, argv);

//here is the problem
while (serial.waitForReadyRead(100)) {
qDebug() << serial.read(8);
}

return a.exec();
}

void setup()
{
serial.setRate(SerialPort::Rate9600);
// serial.setBaudRate(AbstractSerial::BaudRate9600);
serial.setDataBits(SerialPort::Data8);
serial.setParity(SerialPort::NoParity);
serial.setStopBits(SerialPort::OneStop);
serial.setFlowControl(SerialPort::NoFlowControl);
}


and the result :


"308"
"307"
"308"
"3"
"0"
"9"
"
"
"3"
"0"
"7"
"
"3"
"0"
"9"
.
.



Well previusly i using QSerialDevice for Serial Communication its work fine with simple reading like that. Until i use it for multithreading, it mostly crash all over time. Then i use QtSerialPort for multithreading its work fine until i found the reading is bad. Is there a way to solve that problem?

kuzulis
4th September 2012, 08:01
What exactly do not like? That received an one by one byte?

UPD: Is not recommended to use synchronous reading, because it is not tested.
Also expected to implement a big change of the internal structure of the library.

Lesiok
4th September 2012, 09:45
But all is OK. Terminal is writing to screen received bytes one by one. Yours program reads a few bytes from COM buffer and writes it to screen with qDebug which adds new line. It is all.