Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSerialPort read and write in the same QThread
Qt 6.12 LTS is out! See what's new in the release blog

QSerialPort read and write in the same QThread

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 8 Posters 14.5k Views 3 Watching
  • 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.
  • A Offline
    A Offline
    addebito
    wrote on last edited by
    #8

    @romain-donze look this code.
    I hope you find it useful.

        m_serial->clear(QSerialPort::AllDirections);
        
        // write a request
        const QByteArray requestData = QString("your request\r\n").toUtf8();
        m_serial->write(requestData);
        
        if (m_serial->waitForBytesWritten(m_waitTimeout))
        {
            // wait and read the response
            if (m_serial->waitForReadyRead(m_waitTimeout))
            {
                QByteArray responseData = m_serial->readAll();
                while (m_serial->waitForReadyRead(100))
                    responseData += m_serial->readAll();
    
            }
        }
    
    R 1 Reply Last reply
    0
    • R romain.donze

      @aha_1980 hey so after a few try i am a bit lost here.

      So firstly I know I must use a thread for my use case (I use this thread to transfer complete files via xbee module using serial port so it takes some time)

      But I ran in some issues.

      Since my thread must run indefinitly, I put a while(1){} in it and I am doing all my stuff in this loop. But unfortunatly, it always use 100% of the cpu. Do you have any idea on how to put the thread in sleep mode and wake it when I make a write request or when a QSerialPort::readyRead occurs?

      thanks for your help

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #9

      @romain-donze

      So firstly I know I must use a thread for my use case (I use this thread to transfer complete files via xbee module using serial port so it takes some time)

      As said multiple times, that is no reason to need a thread.

      Regards

      Qt has to stay free or it will die.

      R 1 Reply Last reply
      2
      • A addebito

        @romain-donze look this code.
        I hope you find it useful.

            m_serial->clear(QSerialPort::AllDirections);
            
            // write a request
            const QByteArray requestData = QString("your request\r\n").toUtf8();
            m_serial->write(requestData);
            
            if (m_serial->waitForBytesWritten(m_waitTimeout))
            {
                // wait and read the response
                if (m_serial->waitForReadyRead(m_waitTimeout))
                {
                    QByteArray responseData = m_serial->readAll();
                    while (m_serial->waitForReadyRead(100))
                        responseData += m_serial->readAll();
        
                }
            }
        
        R Offline
        R Offline
        romain.donze
        wrote on last edited by
        #10

        @addebito yeah I have already seen something like this in the masterthread example for QSerialPort but the thing is that data could arrive at any moment and not only after a specifi write

        A 1 Reply Last reply
        0
        • aha_1980A aha_1980

          @romain-donze

          So firstly I know I must use a thread for my use case (I use this thread to transfer complete files via xbee module using serial port so it takes some time)

          As said multiple times, that is no reason to need a thread.

          Regards

          R Offline
          R Offline
          romain.donze
          wrote on last edited by
          #11

          @aha_1980 ok but I don't understand how this could not block my current thread (e.g. gui thread) if I have to send a big file that could take minutes to send...

          How would I use waitForBytesWritten() to not block my code

          aha_1980A 1 Reply Last reply
          0
          • R romain.donze

            @aha_1980 ok but I don't understand how this could not block my current thread (e.g. gui thread) if I have to send a big file that could take minutes to send...

            How would I use waitForBytesWritten() to not block my code

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by aha_1980
            #12

            @romain-donze said in QSerialPort read and write in the same QThread:

            How would I use waitForBytesWritten() to not block my code

            You would not use it at all. You would use the bytesWritten() signal.

            Do note however, that this does not mean the bytes are actually transmitted. It only means, the bytes are transferred from Qt's buffer to the operating system buffer.

            Serial devices are slow. You can calculate the needed time approximately with the baud rate the device is set to.

            Regards

            Qt has to stay free or it will die.

            R 1 Reply Last reply
            3
            • aha_1980A aha_1980

              @romain-donze said in QSerialPort read and write in the same QThread:

              How would I use waitForBytesWritten() to not block my code

              You would not use it at all. You would use the bytesWritten() signal.

              Do note however, that this does not mean the bytes are actually transmitted. It only means, the bytes are transferred from Qt's buffer to the operating system buffer.

              Serial devices are slow. You can calculate the needed time approximately with the baud rate the device is set to.

              Regards

              R Offline
              R Offline
              romain.donze
              wrote on last edited by
              #13

              @aha_1980 ok so is there some examples on how to use it?

              jsulmJ 1 Reply Last reply
              0
              • R romain.donze

                @addebito yeah I have already seen something like this in the masterthread example for QSerialPort but the thing is that data could arrive at any moment and not only after a specifi write

                A Offline
                A Offline
                addebito
                wrote on last edited by
                #14

                @romain-donze you can swap the code, before you wait to receive data with a specific timeout and after you can send a reply.
                You can manage your other asyncronous write in the main loop with a Semaphor or Event for example.

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  ollarch
                  wrote on last edited by ollarch
                  #15

                  Hi,

                  Use the "readyRead" SIGNAL to receive the data.
                  In your write method you could write the data into chunks (N bytes). After write a chunk, call QApplication::processEvents() that forces the calling thread to process the events and so it will receive the data if it is there.

                  1 Reply Last reply
                  0
                  • R romain.donze

                    @aha_1980 ok so is there some examples on how to use it?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #16

                    @romain-donze said in QSerialPort read and write in the same QThread:

                    so is there some examples on how to use it?

                    https://doc.qt.io/qt-5/qtserialport-examples.html

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      romain.donze
                      wrote on last edited by romain.donze
                      #17

                      @aha_1980 just so you know, I did some test with my uart device plugged to my board and I am receiving data evry 2ms (I am receiving a string that looks like that "#Z%d,%.2f\r")

                      In my readData() function, I do this (this function is connected to the readyRead signal)

                      void mUart::readData()
                      {
                          QByteArray singleData = mSerial->readAll();
                          mBufferData = mBufferData + QString::fromUtf8(singleData);
                      
                          QStringList dataList;
                      
                          if(mBufferData.contains('\r'))
                          {
                              dataList = mBufferData.split('\r');
                      
                              foreach (mTrame, dataList)
                              {
                                  emit this->dataReceived(mTrame);
                                  qDebug() << mTrame;
                              }
                          }
                      }
                      

                      the mUart class is move to a thread and even so, I am using more than 100% of my cpu... Is ther something I do wrong?

                      J.HilkJ jsulmJ 2 Replies Last reply
                      0
                      • R romain.donze

                        @aha_1980 just so you know, I did some test with my uart device plugged to my board and I am receiving data evry 2ms (I am receiving a string that looks like that "#Z%d,%.2f\r")

                        In my readData() function, I do this (this function is connected to the readyRead signal)

                        void mUart::readData()
                        {
                            QByteArray singleData = mSerial->readAll();
                            mBufferData = mBufferData + QString::fromUtf8(singleData);
                        
                            QStringList dataList;
                        
                            if(mBufferData.contains('\r'))
                            {
                                dataList = mBufferData.split('\r');
                        
                                foreach (mTrame, dataList)
                                {
                                    emit this->dataReceived(mTrame);
                                    qDebug() << mTrame;
                                }
                            }
                        }
                        

                        the mUart class is move to a thread and even so, I am using more than 100% of my cpu... Is ther something I do wrong?

                        J.HilkJ Online
                        J.HilkJ Online
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #18

                        @romain-donze said in QSerialPort read and write in the same QThread:

                        the mUart class is move to a thread and even so, I am using more than 100% of my cpu... Is ther something I do wrong?

                        of course, just because you moved the invite loop into a thread, doesn't mean it uses less CPU!


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        R 1 Reply Last reply
                        3
                        • R romain.donze

                          @aha_1980 just so you know, I did some test with my uart device plugged to my board and I am receiving data evry 2ms (I am receiving a string that looks like that "#Z%d,%.2f\r")

                          In my readData() function, I do this (this function is connected to the readyRead signal)

                          void mUart::readData()
                          {
                              QByteArray singleData = mSerial->readAll();
                              mBufferData = mBufferData + QString::fromUtf8(singleData);
                          
                              QStringList dataList;
                          
                              if(mBufferData.contains('\r'))
                              {
                                  dataList = mBufferData.split('\r');
                          
                                  foreach (mTrame, dataList)
                                  {
                                      emit this->dataReceived(mTrame);
                                      qDebug() << mTrame;
                                  }
                              }
                          }
                          

                          the mUart class is move to a thread and even so, I am using more than 100% of my cpu... Is ther something I do wrong?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #19
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • J.HilkJ J.Hilk

                            @romain-donze said in QSerialPort read and write in the same QThread:

                            the mUart class is move to a thread and even so, I am using more than 100% of my cpu... Is ther something I do wrong?

                            of course, just because you moved the invite loop into a thread, doesn't mean it uses less CPU!

                            R Offline
                            R Offline
                            romain.donze
                            wrote on last edited by
                            #20

                            @J-Hilk yes I know, but as you see, as I explained, I receive a lot of data and I think that is why my readData function is constantly called... so I don't think it's a good thing to let it in the main thread

                            P.S. sometimes my app crashes. Maybe because I have to many data to read? ):

                            mrjjM 1 Reply Last reply
                            0
                            • R romain.donze

                              @J-Hilk yes I know, but as you see, as I explained, I receive a lot of data and I think that is why my readData function is constantly called... so I don't think it's a good thing to let it in the main thread

                              P.S. sometimes my app crashes. Maybe because I have to many data to read? ):

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #21

                              @romain-donze
                              Hi
                              Do you clear your buffer once you processed it ?
                              You do
                              mBufferData = mBufferData + QString::fromUtf8(singleData);

                              so it will grow until you run out of memory unless you clear it at some point.

                              You could move your processing to a worker thread so it will do the split and
                              emit to the objects.

                              1 Reply Last reply
                              1

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt
                              • Unsolved