Unable to read from QSerialPort using MSVC compiler
-
Hi,
I'm developing an application to communicate via serial using master/slave architectures with an application running on Linux. My application is the master and runs on Windows 10 and the application on Linux is slave. I'm using the QSerialPort class to do this.
My proble is: if I use the mingw compiler the communication works fine, but if I use the MSVC compiler with the same code, my application is able to write on serial port but is unable to read the data.Here a snippet of code where use the serial.
qint64 pBytesWritten = serial->write(dataToSend); serial->flush(); if (pBytesWritten == dataToSend.length()) { qDebug().nospace().noquote() << "Write ok"; QThread::msleep(50); if (readMsg() == E_OK) { qDebug().nospace().noquote() << "Read ok"; } else { qDebug().nospace().noquote() << "Read not ok"; }
readMsg function:
bool readMsg(){ bool res = serial->waitForReadyRead(ReceptionTimeout); QByteArray recBuff; if (res) recBuff = serial->readAll(); . . . return res; }
The output is always:
Write ok Read not ok
-
@Turi said in Unable to read from QSerialPort using MSVC compiler:
if (pBytesWritten == dataToSend.length()) {
qDebug().nospace().noquote() << "Write ok";
QThread::msleep(50);
if (readMsg() == E_OK) {This is not how you should use QSerialPort.
You should read from the port as soon as there is actually something to read.
To do so connect a slot to https://doc.qt.io/qt-5/qiodevice.html#readyRead signal and do the reading there.If, for some reason, you want to do it in sequence, then call https://doc.qt.io/qt-5/qiodevice.html#waitForReadyRead after writing and read afterwards.
-
@Turi for the love of my sanity, please use the provided signals:
https://doc.qt.io/qt-5/qiodevice.html#signalsbytesWritten and readyRead
do not use flush, Qthread::sleep and waitForXXXX!
also connect to the error signal
errorOccured
https://doc.qt.io/qt-5/qserialport.html#errorOccurred -
@jsulm said in Unable to read from QSerialPort using MSVC compiler:
afterwards
I need to do it in sequence. I already use waitForReadyRead function to read from serial but this not work if I compile my application with MSVC.
-
@Turi said in Unable to read from QSerialPort using MSVC compiler:
I already use waitForReadyRead
Then QThread::msleep(50); is completely useless.
Why do you need to do it in sequence?
Qt is an event driven framework and you should use it as such instead of trying to force it to behave in a way it was not designed to work? -
@Turi said in Unable to read from QSerialPort using MSVC compiler:
the connect is never called. Also with the connect, it is solicited if I use MinGW compiler but not with MSVC.
I don't understand this.
Do you mean the connect(...) is never called? Then fix that.
Or do you mean the slot is not called?
Please show your connect call and also check its return value. -
@Turi said in Unable to read from QSerialPort using MSVC compiler:
I have already followed the suggestions of @J-Hilk
No, I was refering to this:
"also connect to the error signal errorOccured
https://doc.qt.io/qt-5/qserialport.html#errorOccurred"And you should use the Qt5 connect syntax: https://doc.qt.io/qt-5/signalsandslots.html
-
@Turi said in Unable to read from QSerialPort using MSVC compiler:
Based on Qt 5.12.2 (MSVC 2017, 32 bit)
This is the Qt version which was used to build QtCreator.
What Qt version do you use?