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?
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?