Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSerialPort & QDataStream - Data not sent to the Serial Device
Forum Update on Monday, May 27th 2025

QSerialPort & QDataStream - Data not sent to the Serial Device

Scheduled Pinned Locked Moved Solved General and Desktop
qserialportqdatastreamqt6serialhex
3 Posts 2 Posters 809 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin470
    wrote on 12 May 2022, 13:58 last edited by Kevin470 5 Dec 2022, 14:09
    #1

    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.

    Hterm.PNG

    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.

    C 1 Reply Last reply 12 May 2022, 17:40
    0
    • K Kevin470
      12 May 2022, 13:58

      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.

      Hterm.PNG

      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.

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 12 May 2022, 17:40 last edited by
      #2

      @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()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      K 1 Reply Last reply 13 May 2022, 07:21
      4
      • C Christian Ehrlicher
        12 May 2022, 17:40

        @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()

        K Offline
        K Offline
        Kevin470
        wrote on 13 May 2022, 07:21 last edited by
        #3

        @Christian-Ehrlicher Yup, it worked! Thanks a lot.

        1 Reply Last reply
        0

        1/3

        12 May 2022, 13:58

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved