QSerialPort & QDataStream - Data not sent to the Serial Device
-
Hello,
I am new to Qt and I am trying to read and write to a Serial Device using QSerialPort with Qt6.
I need to send three bytes of data for my device to respond.
Hterm was used to test if it successfully receives Hex data and it also gives a valid response.I tried sending the same using QSerialPort. The port opens and closes successfully, but no write shown in the com port sniffer.
I am unable to find where I am making a mistake.
Any help would be amazing.The code I run to send those three bytes is here
if (m_serialPort.isOpen()) { qDebug() << "Serial port is open..."; Sleep(1000); QByteArray buffer; QDataStream stream(&buffer, QIODevice::WriteOnly); stream << START_BYTE; //0x02 stream << GET_TEMP; //0x74 stream << LAST_BYTE; //0x03 qInfo() << buffer; qInfo() << "Buffer length:"<< buffer.length(); if(m_serialPort.isWritable()) { qInfo() << m_serialPort.write(buffer,buffer.length()); } else { qInfo() << "Not Writable"; } Sleep(6000); m_serialPort.close(); qDebug() << "...serial port is closed!";
(qInfo() << buffer) gives "\x02t\x03" as output
Edit - Added QDataStream in title.
-
@Kevin470 said in QSerialPort & QDataStream - Data not sent to the Serial Device:
Sleep(6000);
Don't do this... To make sure the buffer is flushed you should use... QSerialPort::flush()
-
@Christian-Ehrlicher Yup, it worked! Thanks a lot.