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: readyRead() signal is emitted 2 times instead of 1 time
Qt 6.11 is out! See what's new in the release blog

QSerialPort: readyRead() signal is emitted 2 times instead of 1 time

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.7k 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.
  • O Offline
    O Offline
    oBOXPOH
    wrote on last edited by oBOXPOH
    #1

    Hello everyone!

    Now I try to study using of QSerialPort with QThread. And during the study I got some strange thing: I get 2 readyRead() signals => 2 readyRead() slots, and I can't to construct response data as one.

    What I have:

    • I have the class for QSerialPort, there are some slots for open port, for write to port, for read from port. Slot for reading from port s connected to readyRead() signal in constructor of the class:

    connect(serialPort, &QSerialPort::readyRead, this, &SerialPort_Class::slot_ReadInPort);

    Slot for writing to port:

    void SerialPort_Class::slot_WriteToPort(QByteArray writeData)
    {
        qDebug() << "SLOT WriteToPort" << endl;
        if (serialPort->isOpen())
        {
            QByteArray readData = serialPort->readAll();
    
            qDebug() << "Write Request = " << writeData << endl;
    
            serialPort->write(writeData);
    
            if (serialPort->waitForBytesWritten(1000))
            {}
            else
            {
                qDebug() << "Write request TIMEOUT" << endl;
            }
        }
    }
    

    Slot for readyRead:

    void SerialPort_Class::slot_ReadInPort()
    {
        QThread::msleep(1000);
    
        qDebug() << "EMIT SendReadData" << endl;
        emit signal_SendReadData("");
    }
    
    • I have the Thread for emitting of signal for writing to port after opening the port and after slot for readyRead() for processing of response data.

    And what I get in the Debug log (only 1 write command per opening the port):

    "No error" 
    checkDevices_slot
    SLOT WriteToPort 
    Write Request =  "\xA5\x01""c0000\xC9" 
    EMIT SendReadData 
    

    This is a normal situation: one request - one response (EMIT SendReadData message).
    But often I see the next situation for one cycle:

    "No error" 
    checkDevices_slot
    SLOT WriteToPort 
    Write Request =  "\xA5\x01""c0000\xC9" 
    EMIT SendReadData 
    EMIT SendReadData 
    

    This is a unnormal situation: one request - two responses (2 EMIT SendReadData messages).

    And I can't to fix it! I used waitForReadyRead() method even with 1sec delay - and the same problem. I even removed all reading functions in slot (like now) - and the same problem. Maybe, it exists because of baudrate = 9600... I don't know.

    Hope for your help! Thank you in advance!

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • O oBOXPOH

      @J-Hilk said in QSerialPort: readyRead() signal is emitted 2 times instead of 1 time:

      @oBOXPOH first of all, do not QThread::msleep(1000); in your slot!

      This is a normal situation: one request - one response (EMIT SendReadData message).

      thats actually the exception. Normal is: "An unspecified amount of readyRead Signals per request send"

      with a normal Serialport connection, there is no guarantee that all data is already there, when the signal is emitted. Usually one has some kind of protocol on top of the serial port to fix this issue.

      You only get a signal, notifying you that there is some data available to fetch, and you have to decide if ist the complete answer yet or not.

      I used msleep() method only to recognize the problem, when I get the second signal.

      I understand, that signal only notifying me that I have some data. And I used waitForReadyRead() method to catch all. But anyway I got the second readyRead() signal without, by the way, response data.

      What do you advice to do with processing of such situations?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #5

      @oBOXPOH said in QSerialPort: readyRead() signal is emitted 2 times instead of 1 time:

      What do you advice to do with processing of such situations?

      https://forum.qt.io/topic/117760/qtserialport-problems-with-the-continuous-reading-of-data-from-the-serial-port/2


      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.

      1 Reply Last reply
      2
      • O oBOXPOH

        Hello everyone!

        Now I try to study using of QSerialPort with QThread. And during the study I got some strange thing: I get 2 readyRead() signals => 2 readyRead() slots, and I can't to construct response data as one.

        What I have:

        • I have the class for QSerialPort, there are some slots for open port, for write to port, for read from port. Slot for reading from port s connected to readyRead() signal in constructor of the class:

        connect(serialPort, &QSerialPort::readyRead, this, &SerialPort_Class::slot_ReadInPort);

        Slot for writing to port:

        void SerialPort_Class::slot_WriteToPort(QByteArray writeData)
        {
            qDebug() << "SLOT WriteToPort" << endl;
            if (serialPort->isOpen())
            {
                QByteArray readData = serialPort->readAll();
        
                qDebug() << "Write Request = " << writeData << endl;
        
                serialPort->write(writeData);
        
                if (serialPort->waitForBytesWritten(1000))
                {}
                else
                {
                    qDebug() << "Write request TIMEOUT" << endl;
                }
            }
        }
        

        Slot for readyRead:

        void SerialPort_Class::slot_ReadInPort()
        {
            QThread::msleep(1000);
        
            qDebug() << "EMIT SendReadData" << endl;
            emit signal_SendReadData("");
        }
        
        • I have the Thread for emitting of signal for writing to port after opening the port and after slot for readyRead() for processing of response data.

        And what I get in the Debug log (only 1 write command per opening the port):

        "No error" 
        checkDevices_slot
        SLOT WriteToPort 
        Write Request =  "\xA5\x01""c0000\xC9" 
        EMIT SendReadData 
        

        This is a normal situation: one request - one response (EMIT SendReadData message).
        But often I see the next situation for one cycle:

        "No error" 
        checkDevices_slot
        SLOT WriteToPort 
        Write Request =  "\xA5\x01""c0000\xC9" 
        EMIT SendReadData 
        EMIT SendReadData 
        

        This is a unnormal situation: one request - two responses (2 EMIT SendReadData messages).

        And I can't to fix it! I used waitForReadyRead() method even with 1sec delay - and the same problem. I even removed all reading functions in slot (like now) - and the same problem. Maybe, it exists because of baudrate = 9600... I don't know.

        Hope for your help! Thank you in advance!

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

        @oBOXPOH said in QSerialPort: readyRead() signal is emitted 2 times instead of 1 time:

        readyRead

        There is no guarantee that readyRead will be emitted only once for each write - data can come in several packages. You should also print data you get in slot_ReadInPort() to see whether this is indeed the case.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • O oBOXPOH

          Hello everyone!

          Now I try to study using of QSerialPort with QThread. And during the study I got some strange thing: I get 2 readyRead() signals => 2 readyRead() slots, and I can't to construct response data as one.

          What I have:

          • I have the class for QSerialPort, there are some slots for open port, for write to port, for read from port. Slot for reading from port s connected to readyRead() signal in constructor of the class:

          connect(serialPort, &QSerialPort::readyRead, this, &SerialPort_Class::slot_ReadInPort);

          Slot for writing to port:

          void SerialPort_Class::slot_WriteToPort(QByteArray writeData)
          {
              qDebug() << "SLOT WriteToPort" << endl;
              if (serialPort->isOpen())
              {
                  QByteArray readData = serialPort->readAll();
          
                  qDebug() << "Write Request = " << writeData << endl;
          
                  serialPort->write(writeData);
          
                  if (serialPort->waitForBytesWritten(1000))
                  {}
                  else
                  {
                      qDebug() << "Write request TIMEOUT" << endl;
                  }
              }
          }
          

          Slot for readyRead:

          void SerialPort_Class::slot_ReadInPort()
          {
              QThread::msleep(1000);
          
              qDebug() << "EMIT SendReadData" << endl;
              emit signal_SendReadData("");
          }
          
          • I have the Thread for emitting of signal for writing to port after opening the port and after slot for readyRead() for processing of response data.

          And what I get in the Debug log (only 1 write command per opening the port):

          "No error" 
          checkDevices_slot
          SLOT WriteToPort 
          Write Request =  "\xA5\x01""c0000\xC9" 
          EMIT SendReadData 
          

          This is a normal situation: one request - one response (EMIT SendReadData message).
          But often I see the next situation for one cycle:

          "No error" 
          checkDevices_slot
          SLOT WriteToPort 
          Write Request =  "\xA5\x01""c0000\xC9" 
          EMIT SendReadData 
          EMIT SendReadData 
          

          This is a unnormal situation: one request - two responses (2 EMIT SendReadData messages).

          And I can't to fix it! I used waitForReadyRead() method even with 1sec delay - and the same problem. I even removed all reading functions in slot (like now) - and the same problem. Maybe, it exists because of baudrate = 9600... I don't know.

          Hope for your help! Thank you in advance!

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #3

          @oBOXPOH first of all, do not QThread::msleep(1000); in your slot!

          This is a normal situation: one request - one response (EMIT SendReadData message).

          thats actually the exception. Normal is: "An unspecified amount of readyRead Signals per request send"

          with a normal Serialport connection, there is no guarantee that all data is already there, when the signal is emitted. Usually one has some kind of protocol on top of the serial port to fix this issue.

          You only get a signal, notifying you that there is some data available to fetch, and you have to decide if ist the complete answer yet or not.


          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.

          O 1 Reply Last reply
          3
          • J.HilkJ J.Hilk

            @oBOXPOH first of all, do not QThread::msleep(1000); in your slot!

            This is a normal situation: one request - one response (EMIT SendReadData message).

            thats actually the exception. Normal is: "An unspecified amount of readyRead Signals per request send"

            with a normal Serialport connection, there is no guarantee that all data is already there, when the signal is emitted. Usually one has some kind of protocol on top of the serial port to fix this issue.

            You only get a signal, notifying you that there is some data available to fetch, and you have to decide if ist the complete answer yet or not.

            O Offline
            O Offline
            oBOXPOH
            wrote on last edited by
            #4

            @J-Hilk said in QSerialPort: readyRead() signal is emitted 2 times instead of 1 time:

            @oBOXPOH first of all, do not QThread::msleep(1000); in your slot!

            This is a normal situation: one request - one response (EMIT SendReadData message).

            thats actually the exception. Normal is: "An unspecified amount of readyRead Signals per request send"

            with a normal Serialport connection, there is no guarantee that all data is already there, when the signal is emitted. Usually one has some kind of protocol on top of the serial port to fix this issue.

            You only get a signal, notifying you that there is some data available to fetch, and you have to decide if ist the complete answer yet or not.

            I used msleep() method only to recognize the problem, when I get the second signal.

            I understand, that signal only notifying me that I have some data. And I used waitForReadyRead() method to catch all. But anyway I got the second readyRead() signal without, by the way, response data.

            What do you advice to do with processing of such situations?

            J.HilkJ 1 Reply Last reply
            0
            • O oBOXPOH

              @J-Hilk said in QSerialPort: readyRead() signal is emitted 2 times instead of 1 time:

              @oBOXPOH first of all, do not QThread::msleep(1000); in your slot!

              This is a normal situation: one request - one response (EMIT SendReadData message).

              thats actually the exception. Normal is: "An unspecified amount of readyRead Signals per request send"

              with a normal Serialport connection, there is no guarantee that all data is already there, when the signal is emitted. Usually one has some kind of protocol on top of the serial port to fix this issue.

              You only get a signal, notifying you that there is some data available to fetch, and you have to decide if ist the complete answer yet or not.

              I used msleep() method only to recognize the problem, when I get the second signal.

              I understand, that signal only notifying me that I have some data. And I used waitForReadyRead() method to catch all. But anyway I got the second readyRead() signal without, by the way, response data.

              What do you advice to do with processing of such situations?

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #5

              @oBOXPOH said in QSerialPort: readyRead() signal is emitted 2 times instead of 1 time:

              What do you advice to do with processing of such situations?

              https://forum.qt.io/topic/117760/qtserialport-problems-with-the-continuous-reading-of-data-from-the-serial-port/2


              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.

              1 Reply Last reply
              2

              • Login

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