I am trying to read the data sent by a device plug via usb. First i read the data via this command
- sudo stty -F /dev/ttyUSB0 1200 sane parenb evenp cs7 -crtscts
- cat /dev/ttyUSB0
And the data are like this
TGPHI_s -0,24 =
MESURES2 BT 4 SUP36 A
PTCOUR2 HPH /
Now i want to read the data via a Qt5.3 program
QSerialPort serial;
serial.setPortName("ttyUSB0");
if(!serial.setBaudRate(QSerialPort::Baud1200 , QSerialPort::Input))
qDebug() << serial.errorString();
if(!serial.setDataBits(QSerialPort::Data7))
qDebug() << serial.errorString();
if(!serial.setParity(QSerialPort::EvenParity))
qDebug() << serial.errorString();
if(!serial.setFlowControl(QSerialPort::HardwareControl))
qDebug() << serial.errorString();
if(!serial.setStopBits(QSerialPort::OneStop))
qDebug() << serial.errorString();
if(!serial.open(QIODevice::ReadOnly))
qDebug() << serial.errorString();
qDebug() << serial.bytesAvailable();
while(true)
{
if (serial.isOpen()) {
qDebug() << "Serial port is open...";
QByteArray datas = serial.readAll();
if (datas.size() == 0) {
qDebug() << "Arrived data: 0";
} else {
for (int i = 0; i < datas.size(); i++){
if (datas.at(i)) {
qDebug() << datas[i];
}
}
}
} else {
qDebug() << "OPEN ERROR: " << serial.errorString();
}
}
return 0;
and the answer is ->
"/dev/ttyUSB0"
0
Serial port is open...
Arrived data: 0
Serial port is open...
Arrived data: 0
So there is no data catch by my program ... My questions are :
- Did i miss something in the setting of the QSerialPort ?
- If no why there is no data display via the qDebug()
EDIT
Thanks to mike i can finaly read this usb device !!! here is my final code
QSerialPort serial;
serial.setPortName("ttyUSB0");
if(!serial.setBaudRate(QSerialPort::Baud1200))
qDebug() << serial.errorString();
if(!serial.setDataBits(QSerialPort::Data7))
qDebug() << serial.errorString();
if(!serial.setParity(QSerialPort::EvenParity))
qDebug() << serial.errorString();
if(!serial.setFlowControl(QSerialPort::HardwareControl))
qDebug() << serial.errorString();
if(!serial.setStopBits(QSerialPort::OneStop))
qDebug() << serial.errorString();
if(!serial.open(QIODevice::ReadOnly))
qDebug() << serial.errorString();
QObject::connect(&serial, &QSerialPort::readyRead, [&]
{
//this is called when readyRead() is emitted
//qDebug() << "New data available: " << serial.bytesAvailable();
qDebug() << "New data available: " << serial.bytesAvailable();
QByteArray datas = serial.readAll();
qDebug() << datas;
});
QObject::connect(&serial,
static_cast<void(QSerialPort::*)(QSerialPort::SerialPortError)>
(&QSerialPort::error),
[&](QSerialPort::SerialPortError error)
{
//this is called when a serial communication error occurs
qDebug() << "An error occured: " << error;
return qApp->quit();
});
if(!serial.open(QIODevice::ReadOnly))
qDebug() << serial.errorString();
return qApp->exec();