Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QSerialPort does not wait for data
I'm using QSerialPort wich I've got from "here...":http://qt-project.org/wiki/QtSerialPort
I'm successfully sending data to another PC, but after sending I want my code to wait for a response for further actions.
Somewhere I found this code snippet but waitForReadyRead returns immediate with false result.
What is that I'm missing? Where could I find more information about the library?.
//wait for response
QByteArray inputMsg = serial->readAll();
numRead = inputMsg.length();
// Do whatever with the array
numReadTotal += numRead;
if (numRead == 0 && !serial->waitForReadyRead(mTimeOut*1000))
Jeroentjehome last edited by
hmm, numRead isn't zero, so why should the compiler run the waitForReadyRead function? It will not be called at all. if you change the two arguments, the compiler might call the waitForReadyRead signal, but do not count on it!
But numRead is zero and waitForReadyRead is actually executed but returns false
UlesQt, use the connect()! (See here: http://qt-project.org/doc/qt-4.8/signalsandslots.html)
connect(SingletonSession::getComunicaoSerial(), SIGNAL(signalResposta(Resposta*)), this, SLOT(respostaDetectarSensores(Resposta*)));
I'm using on my project and works very nice! I used some other classes to manupilate data for me, but it's very simple to implement this!
This signals and slots is very useful, and powerful resource, for a lot of stuff on application!
- I'm using other API, but it's possible that Signals and Slots solve your problem!
Tnxs! That is other approach I'm using, but this time I wanted the code to be blocking, so the function won't return until receiving a valid response message.
could I do that with qextserialport?
I not really sure about!
I used on my project like I show you, and before I use the data received I use some methods to validate and transform the data received from USB/Serial.
Works very well here, on linux and Windows!
I think that use a loop for wait some data is worse, and can consume a lot of cpu time!
The Qt has this very powerful feature (signals and slots), I recommend to use them! Easy to use, easy to maintain, lightweight resource...
bq. What is that I’m missing?
Use blocking I / O is necessary only in a separate thread.
Also have some features using the methods read() / write(). The fact is that in blocking mode, these functions do not work correctly by themselves, because do not work event-loop.
For their proper functioning is necessary to use methods such as waitForXXX (), that handle events that I / O.
Should be the following guidelines on blocking I / O:
- After call write() need call waitForBytesWritten()
- Before call read() need call waitForReadyRead()
At the moment, blocking I / O does not work in QtSerialPort on Windows, but the solution is available, but not yet merged to master branch.
Please use this patch:
Also exists examples of the use of blocking I / O:
Kuzulis very nive informations!