QSerialPort: problem with waitForReadyRead()
-
Hello everyone!
Now I try to use QSerialPort to get data from some device. In order to get data I need to send any symbol (1 byte) and then I will get 5 bytes data word.
The problem is next: sometimes I write symbol to port and don't get data with waitForReadyRead(), but with next writing I get double size data (10 bytes instead of 5 bytes). In waitForReadyRead(time) I can use even long time, but anyway I can't get data.
I have been trying many times to solve this problem, but no success. I only found, where the problem is.
My code:
while (1) { QByteArray input, output; input.clear(); input.append('0'); deviceControl_SerialPort->write(input); // <-- WRITE count++; if (deviceControl_SerialPort->waitForBytesWritten(30000)) // <-- WAIT FOR WRITE { QThread::msleep(500); // <-- SLEEP FOR COMFORTABLE DEBUGING qDebug() << "WRITTEN " << count << endl; if (deviceControl_SerialPort->waitForReadyRead(3000)) // <-- AFTER WRITE WAIT FOR READ { qDebug() << "READING..." << endl; output = deviceControl_SerialPort->readAll(); // <-- READ while (deviceControl_SerialPort->waitForReadyRead(10)) // <-- READ AGAIN output += deviceControl_SerialPort->readAll(); if (output.count() >= 5) { deviceControl_List.at(0)->setAngleEncoderData(output.at(1), output.at(2)); deviceControl_List.at(1)->setAngleEncoderData(output.at(3), output.at(4)); //qDebug("Data"); } } else { qDebug() << "I AM NOT READY!" << endl; // <-- SOMETIMES (OR OFTEN) I AM HERE } } else { qDebug("SOMETHING WRONG!"); } }
Some Debug Information during run:
WRITTEN 1 READING... WRITTEN 2 READING... WRITTEN 3 READING... WRITTEN 4 READING... WRITTEN 5 READING... WRITTEN 6 // <-- DATA WAS WRITTEN I AM NOT READY! // <-- BUT EVEN AFTER 3 SECONDS NO DATA WRITTEN 7 READING... WRITTEN 8 READING... WRITTEN 9 READING... WRITTEN 10 READING... WRITTEN 11 READING... WRITTEN 12 READING... WRITTEN 13 READING... WRITTEN 14 // <-- HERE - SAME PROBLEM I AM NOT READY! // <-- AND THEN ONLY MORE OFTEN WRITTEN 15 READING... WRITTEN 16 I AM NOT READY! WRITTEN 17 READING... WRITTEN 18 I AM NOT READY! WRITTEN 19 READING... WRITTEN 20 I AM NOT READY! WRITTEN 21 READING... WRITTEN 22 I AM NOT READY! WRITTEN 23 READING... WRITTEN 24 I AM NOT READY! WRITTEN 25 READING... WRITTEN 26
Realy hope for your help! I can't find useful information to solve this problem in Internet.
I think it's not problem in device, because in another application I can't see my problem.
Thank you in advance!
-
Hello everyone!
Now I try to use QSerialPort to get data from some device. In order to get data I need to send any symbol (1 byte) and then I will get 5 bytes data word.
The problem is next: sometimes I write symbol to port and don't get data with waitForReadyRead(), but with next writing I get double size data (10 bytes instead of 5 bytes). In waitForReadyRead(time) I can use even long time, but anyway I can't get data.
I have been trying many times to solve this problem, but no success. I only found, where the problem is.
My code:
while (1) { QByteArray input, output; input.clear(); input.append('0'); deviceControl_SerialPort->write(input); // <-- WRITE count++; if (deviceControl_SerialPort->waitForBytesWritten(30000)) // <-- WAIT FOR WRITE { QThread::msleep(500); // <-- SLEEP FOR COMFORTABLE DEBUGING qDebug() << "WRITTEN " << count << endl; if (deviceControl_SerialPort->waitForReadyRead(3000)) // <-- AFTER WRITE WAIT FOR READ { qDebug() << "READING..." << endl; output = deviceControl_SerialPort->readAll(); // <-- READ while (deviceControl_SerialPort->waitForReadyRead(10)) // <-- READ AGAIN output += deviceControl_SerialPort->readAll(); if (output.count() >= 5) { deviceControl_List.at(0)->setAngleEncoderData(output.at(1), output.at(2)); deviceControl_List.at(1)->setAngleEncoderData(output.at(3), output.at(4)); //qDebug("Data"); } } else { qDebug() << "I AM NOT READY!" << endl; // <-- SOMETIMES (OR OFTEN) I AM HERE } } else { qDebug("SOMETHING WRONG!"); } }
Some Debug Information during run:
WRITTEN 1 READING... WRITTEN 2 READING... WRITTEN 3 READING... WRITTEN 4 READING... WRITTEN 5 READING... WRITTEN 6 // <-- DATA WAS WRITTEN I AM NOT READY! // <-- BUT EVEN AFTER 3 SECONDS NO DATA WRITTEN 7 READING... WRITTEN 8 READING... WRITTEN 9 READING... WRITTEN 10 READING... WRITTEN 11 READING... WRITTEN 12 READING... WRITTEN 13 READING... WRITTEN 14 // <-- HERE - SAME PROBLEM I AM NOT READY! // <-- AND THEN ONLY MORE OFTEN WRITTEN 15 READING... WRITTEN 16 I AM NOT READY! WRITTEN 17 READING... WRITTEN 18 I AM NOT READY! WRITTEN 19 READING... WRITTEN 20 I AM NOT READY! WRITTEN 21 READING... WRITTEN 22 I AM NOT READY! WRITTEN 23 READING... WRITTEN 24 I AM NOT READY! WRITTEN 25 READING... WRITTEN 26
Realy hope for your help! I can't find useful information to solve this problem in Internet.
I think it's not problem in device, because in another application I can't see my problem.
Thank you in advance!
@oBOXPOH said in QSerialPort: problem with waitForReadyRead():
output = deviceControl_SerialPort->readAll(); // <-- READ while (deviceControl_SerialPort->waitForReadyRead(10)) // <-- READ AGAIN output += deviceControl_SerialPort->readAll();
readAll()
only reads whatever is available at the instant it is called. This can be anywhere from 1 byte to the total number you expect. Yourwhile (waitForReadyRead(10))
may return false, and so exit the loop, when there is no data right now but there will be later on, meaning that you will miss the data this time round and pick it up only next time.I don't know much about serial ports, but at least try upping that
10
to a bigger number and see if it makes any difference?Make sure the other end is flushing whatever it writes immediately.
You're not really "supposed to" use
waitForReadyRead(10)
. You're "supposed to" use thereadyRead()
slot with signals. You could try that and see if it makes a difference. -
What is OS, and what is Qt version?
-
@oBOXPOH said in QSerialPort: problem with waitForReadyRead():
output = deviceControl_SerialPort->readAll(); // <-- READ while (deviceControl_SerialPort->waitForReadyRead(10)) // <-- READ AGAIN output += deviceControl_SerialPort->readAll();
readAll()
only reads whatever is available at the instant it is called. This can be anywhere from 1 byte to the total number you expect. Yourwhile (waitForReadyRead(10))
may return false, and so exit the loop, when there is no data right now but there will be later on, meaning that you will miss the data this time round and pick it up only next time.I don't know much about serial ports, but at least try upping that
10
to a bigger number and see if it makes any difference?Make sure the other end is flushing whatever it writes immediately.
You're not really "supposed to" use
waitForReadyRead(10)
. You're "supposed to" use thereadyRead()
slot with signals. You could try that and see if it makes a difference.@JonB said in QSerialPort: problem with waitForReadyRead():
@oBOXPOH said in QSerialPort: problem with waitForReadyRead():
output = deviceControl_SerialPort->readAll(); // <-- READ while (deviceControl_SerialPort->waitForReadyRead(10)) // <-- READ AGAIN output += deviceControl_SerialPort->readAll();
readAll()
only reads whatever is available at the instant it is called. This can be anywhere from 1 byte to the total number you expect. Yourwhile (waitForReadyRead(10))
may return false, and so exit the loop, when there is no data right now but there will be later on, meaning that you will miss the data this time round and pick it up.I don't know much about serial ports, but at least try upping that
10
to a bigger number and see if it makes any difference?Make sure the other end is flushing whatever it writes immediately.
You're not really "supposed to" use
waitForReadyRead(10)
. You're "supposed to" use thereadyRead()
slot with signals. You could try that and see if it makes a difference.In order to get data with readAll() I need to go through waitForReadyRead(), but sometimes I wait even 10 seconds - and no data anyway. When waitForReadyRead() returns true, then readAll() works fine.
-
while loops and sleeps... why don't you simply use the asynchronous signals that QSerialPort provides ?
Also connect the error signal to an output see if something is triggered there
-
@kuzulis said in QSerialPort: problem with waitForReadyRead():
What is OS, and what is Qt version?
OS: Windows 10
Qt Version: 5.13.1
Building: MinGW 32-bit -
@kuzulis said in QSerialPort: problem with waitForReadyRead():
What is OS, and what is Qt version?
OS: Windows 10
Qt Version: 5.13.1
Building: MinGW 32-bit@oBOXPOH
I would suggest to use a different Qt version QSerialPort has some serious issues in 5.13.1see this bugreport
https://bugreports.qt.io/browse/QTBUG-78086 -
@oBOXPOH
I would suggest to use a different Qt version QSerialPort has some serious issues in 5.13.1see this bugreport
https://bugreports.qt.io/browse/QTBUG-78086@J-Hilk said in QSerialPort: problem with waitForReadyRead():
@oBOXPOH
I would suggest to use a different Qt version QSerialPort has some serious issues in 5.13.1see this bugreport
https://bugreports.qt.io/browse/QTBUG-78086Tried Qt 5.9.8.
In this case waitForReadyRead() always returns false with accepted write command.
-
@oBOXPOH
Like I & @J-Hilk have said, suggest you at least try it withreadyRead
signal instead of waits and see if that makes the difference?@JonB said in QSerialPort: problem with waitForReadyRead():
@oBOXPOH
Like I & @J-Hilk have said, suggest you at least try it withreadyRead
signal instead of waits and see if that makes the difference?I tried next (all is inside the class):
connect(deviceControl_SerialPort, &QSerialPort::readyRead, this, &Class::readData_slot);
void Class::readData_slot() { qDebug() << "Ready Read" << endl; deviceControl_readData.append(deviceControl_SerialPort->readAll()); }
In this case I can't reach readData_slot() method.
Then I tried next:
connect(deviceControl_SerialPort, &QSerialPort::errorOccurred, this, &Class::serialPortError_slot);
void Class::serialPortError_slot(QSerialPort::SerialPortError error) { qDebug() << error << endl; }
and got some error (see log below):
WRITTEN 1 READING... QSerialPort::TimeoutError WRITTEN 2 READING... QSerialPort::TimeoutError WRITTEN 3 QSerialPort::TimeoutError I AM NOT READY! Ready Read Ready Read
and, by the way, message for readyRead signal. Strangely, that I got it only after 2 real readings...
-
@JonB said in QSerialPort: problem with waitForReadyRead():
@oBOXPOH
Like I & @J-Hilk have said, suggest you at least try it withreadyRead
signal instead of waits and see if that makes the difference?I tried next (all is inside the class):
connect(deviceControl_SerialPort, &QSerialPort::readyRead, this, &Class::readData_slot);
void Class::readData_slot() { qDebug() << "Ready Read" << endl; deviceControl_readData.append(deviceControl_SerialPort->readAll()); }
In this case I can't reach readData_slot() method.
Then I tried next:
connect(deviceControl_SerialPort, &QSerialPort::errorOccurred, this, &Class::serialPortError_slot);
void Class::serialPortError_slot(QSerialPort::SerialPortError error) { qDebug() << error << endl; }
and got some error (see log below):
WRITTEN 1 READING... QSerialPort::TimeoutError WRITTEN 2 READING... QSerialPort::TimeoutError WRITTEN 3 QSerialPort::TimeoutError I AM NOT READY! Ready Read Ready Read
and, by the way, message for readyRead signal. Strangely, that I got it only after 2 real readings...
Hi @oBOXPOH,
Qt is an event-driven framework. By using endless loops, wait routines you create problems if you don't know what you are exactly doing.
You should use QSerialPort with signals and slots. An example is shown here: https://doc.qt.io/qt-5/qtserialport-terminal-example.html
Regards
-
Hi @oBOXPOH,
Qt is an event-driven framework. By using endless loops, wait routines you create problems if you don't know what you are exactly doing.
You should use QSerialPort with signals and slots. An example is shown here: https://doc.qt.io/qt-5/qtserialport-terminal-example.html
Regards
@aha_1980 said in QSerialPort: problem with waitForReadyRead():
Hi @oBOXPOH,
Qt is an event-driven framework. By using endless loops, wait routines you create problems if you don't know what you are exactly doing.
You should use QSerialPort with signals and slots. An example is shown here: https://doc.qt.io/qt-5/qtserialport-terminal-example.html
Regards
Terminal Example also works badly -> https://forum.qt.io/topic/108848/terminal-example-for-qserialport-doesn-t-work-correctly
-
@aha_1980 said in QSerialPort: problem with waitForReadyRead():
Hi @oBOXPOH,
Qt is an event-driven framework. By using endless loops, wait routines you create problems if you don't know what you are exactly doing.
You should use QSerialPort with signals and slots. An example is shown here: https://doc.qt.io/qt-5/qtserialport-terminal-example.html
Regards
Terminal Example also works badly -> https://forum.qt.io/topic/108848/terminal-example-for-qserialport-doesn-t-work-correctly
@oBOXPOH said in QSerialPort: problem with waitForReadyRead():
@aha_1980 said in QSerialPort: problem with waitForReadyRead():
Hi @oBOXPOH,
Qt is an event-driven framework. By using endless loops, wait routines you create problems if you don't know what you are exactly doing.
You should use QSerialPort with signals and slots. An example is shown here: https://doc.qt.io/qt-5/qtserialport-terminal-example.html
Regards
Terminal Example also works badly -> https://forum.qt.io/topic/108848/terminal-example-for-qserialport-doesn-t-work-correctly
Solution is to install 5.13.2 or more version of Qt.
-
@oBOXPOH Hello i am having the same exact problem with QT, i don't receive any output by triggering the readyread signal or by using the return value of waitForReadyRead(), is it really issue related to QT version??
=> I do have 5.12 version of QT, i want to make sure that that is the problem before deciding to do an upgrade..Thank you in advance for your reply!
-
@oBOXPOH
I would suggest to use a different Qt version QSerialPort has some serious issues in 5.13.1see this bugreport
https://bugreports.qt.io/browse/QTBUG-78086 -
iirc 5.12 should work fine, only 5.13 had the issues
-
@J-Hilk Thank you for your reply, i had upgraded my QT and i still had the same issue, when i send one byte i don't receive any feedback from my device(readyRead is not triggered), it has to be more than one byte in order to receive something. do you please have any idea why is this happening?
-
@J-Hilk Thank you for your reply, i had upgraded my QT and i still had the same issue, when i send one byte i don't receive any feedback from my device(readyRead is not triggered), it has to be more than one byte in order to receive something. do you please have any idea why is this happening?
@DiaaQTdev
Can you show your code?