Home > Mobile >  The thread run () function, serial port data, but the serial. WaitForReadyRead () always returns fal
The thread run () function, serial port data, but the serial. WaitForReadyRead () always returns fal

Time:09-21


in a child thread Qthread
The run () function, called serial. WaitForReadyRead (5000) has been a timeout returns false, but become truly a serial port is data sent to come over, call serial - & gt; BytesAvailable () will always return 0

I am in accordance with the QT documentation written in
Help document address: https://doc.qt.io/qt-5/qtserialport-blockingslave-example.html

The main code:

# include "masterthread. H" masterthread: : masterthread (QObject * parent) : QThread (parent) {} void masterthread: : run () {QSerialPort serial; Serial. SetPortName (" COM4 "); Serial. SetBaudRate (QSerialPort: : Baud115200); Serial. SetDataBits (QSerialPort: : Data8); Serial. SetParity (QSerialPort: : NoParity); Serial. SetFlowControl (QSerialPort: : NoFlowControl); if (! Serial. Open (QIODevice: : ReadWrite))

{qDebug () & lt; & lt;" The Open err "; }
While (1) {serial. Write (" requestData... "); If (serial. WaitForBytesWritten (1000))

{qDebug () & lt; & lt;" WaitForBytesWritten "; }
The else {qDebug () & lt; & lt;" The Write ok "; }//read the response if (serial waitForReadyRead (1000)) {QByteArray responseData=https://bbs.csdn.net/topics/serial.readAll (); While (serial. WaitForReadyRead (10)) responseData +=serial. ReadAll (); QDebug ()
  •  Tags:  
  • Qt
  • Related