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: problem with waitForReadyRead()

QSerialPort: problem with waitForReadyRead()

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 6 Posters 6.2k 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 use QSerialPort to get data from some device. In order to get data I need to send any symbol (1 byte) and then I will get 5 bytes data word.

    The problem is next: sometimes I write symbol to port and don't get data with waitForReadyRead(), but with next writing I get double size data (10 bytes instead of 5 bytes). In waitForReadyRead(time) I can use even long time, but anyway I can't get data.

    I have been trying many times to solve this problem, but no success. I only found, where the problem is.

    My code:

    while (1)
    {
           QByteArray input, output;
    
           input.clear();
           input.append('0');
           deviceControl_SerialPort->write(input); // <-- WRITE
           count++;
    
           if (deviceControl_SerialPort->waitForBytesWritten(30000)) // <-- WAIT FOR WRITE
           {
                  QThread::msleep(500); // <-- SLEEP FOR COMFORTABLE DEBUGING
                  qDebug() << "WRITTEN " << count << endl;
    
                  if (deviceControl_SerialPort->waitForReadyRead(3000)) // <-- AFTER WRITE WAIT FOR READ
                  {
                        qDebug() << "READING..." << endl;
                        output = deviceControl_SerialPort->readAll(); // <-- READ
                        while (deviceControl_SerialPort->waitForReadyRead(10)) // <-- READ AGAIN
                            output += deviceControl_SerialPort->readAll();
    
                        if (output.count() >= 5)
                        {
                            deviceControl_List.at(0)->setAngleEncoderData(output.at(1), output.at(2));
                            deviceControl_List.at(1)->setAngleEncoderData(output.at(3), output.at(4));
                            //qDebug("Data");
                        }
                  }
    
                  else
                  {
                        qDebug() << "I AM NOT READY!" << endl; // <-- SOMETIMES (OR OFTEN) I AM HERE
                  }
            }
           else
           {
                  qDebug("SOMETHING WRONG!");
           }
    }
    

    Some Debug Information during run:

    WRITTEN  1 
    
    READING... 
    
    WRITTEN  2 
    
    READING... 
    
    WRITTEN  3 
    
    READING... 
    
    WRITTEN  4 
    
    READING... 
    
    WRITTEN  5 
    
    READING... 
    
    WRITTEN  6 // <-- DATA WAS WRITTEN
    
    I AM NOT READY! // <-- BUT EVEN AFTER 3 SECONDS NO DATA
    
    WRITTEN  7 
    
    READING... 
    
    WRITTEN  8 
    
    READING... 
    
    WRITTEN  9 
    
    READING... 
    
    WRITTEN  10 
    
    READING... 
    
    WRITTEN  11 
    
    READING... 
    
    WRITTEN  12 
    
    READING... 
    
    WRITTEN  13 
    
    READING... 
    
    WRITTEN  14 // <-- HERE - SAME PROBLEM
    
    I AM NOT READY! // <-- AND THEN ONLY MORE OFTEN
    
    WRITTEN  15 
    
    READING... 
    
    WRITTEN  16 
    
    I AM NOT READY! 
    
    WRITTEN  17 
    
    READING... 
    
    WRITTEN  18 
    
    I AM NOT READY! 
    
    WRITTEN  19 
    
    READING... 
    
    WRITTEN  20 
    
    I AM NOT READY! 
    
    WRITTEN  21 
    
    READING... 
    
    WRITTEN  22 
    
    I AM NOT READY! 
    
    WRITTEN  23 
    
    READING... 
    
    WRITTEN  24 
    
    I AM NOT READY! 
    
    WRITTEN  25 
    
    READING... 
    
    WRITTEN  26
    

    Realy hope for your help! I can't find useful information to solve this problem in Internet.

    I think it's not problem in device, because in another application I can't see my problem.

    Thank you in advance!

    JonBJ 1 Reply Last reply
    0
    • O oBOXPOH

      Hello everyone!

      Now I try to use QSerialPort to get data from some device. In order to get data I need to send any symbol (1 byte) and then I will get 5 bytes data word.

      The problem is next: sometimes I write symbol to port and don't get data with waitForReadyRead(), but with next writing I get double size data (10 bytes instead of 5 bytes). In waitForReadyRead(time) I can use even long time, but anyway I can't get data.

      I have been trying many times to solve this problem, but no success. I only found, where the problem is.

      My code:

      while (1)
      {
             QByteArray input, output;
      
             input.clear();
             input.append('0');
             deviceControl_SerialPort->write(input); // <-- WRITE
             count++;
      
             if (deviceControl_SerialPort->waitForBytesWritten(30000)) // <-- WAIT FOR WRITE
             {
                    QThread::msleep(500); // <-- SLEEP FOR COMFORTABLE DEBUGING
                    qDebug() << "WRITTEN " << count << endl;
      
                    if (deviceControl_SerialPort->waitForReadyRead(3000)) // <-- AFTER WRITE WAIT FOR READ
                    {
                          qDebug() << "READING..." << endl;
                          output = deviceControl_SerialPort->readAll(); // <-- READ
                          while (deviceControl_SerialPort->waitForReadyRead(10)) // <-- READ AGAIN
                              output += deviceControl_SerialPort->readAll();
      
                          if (output.count() >= 5)
                          {
                              deviceControl_List.at(0)->setAngleEncoderData(output.at(1), output.at(2));
                              deviceControl_List.at(1)->setAngleEncoderData(output.at(3), output.at(4));
                              //qDebug("Data");
                          }
                    }
      
                    else
                    {
                          qDebug() << "I AM NOT READY!" << endl; // <-- SOMETIMES (OR OFTEN) I AM HERE
                    }
              }
             else
             {
                    qDebug("SOMETHING WRONG!");
             }
      }
      

      Some Debug Information during run:

      WRITTEN  1 
      
      READING... 
      
      WRITTEN  2 
      
      READING... 
      
      WRITTEN  3 
      
      READING... 
      
      WRITTEN  4 
      
      READING... 
      
      WRITTEN  5 
      
      READING... 
      
      WRITTEN  6 // <-- DATA WAS WRITTEN
      
      I AM NOT READY! // <-- BUT EVEN AFTER 3 SECONDS NO DATA
      
      WRITTEN  7 
      
      READING... 
      
      WRITTEN  8 
      
      READING... 
      
      WRITTEN  9 
      
      READING... 
      
      WRITTEN  10 
      
      READING... 
      
      WRITTEN  11 
      
      READING... 
      
      WRITTEN  12 
      
      READING... 
      
      WRITTEN  13 
      
      READING... 
      
      WRITTEN  14 // <-- HERE - SAME PROBLEM
      
      I AM NOT READY! // <-- AND THEN ONLY MORE OFTEN
      
      WRITTEN  15 
      
      READING... 
      
      WRITTEN  16 
      
      I AM NOT READY! 
      
      WRITTEN  17 
      
      READING... 
      
      WRITTEN  18 
      
      I AM NOT READY! 
      
      WRITTEN  19 
      
      READING... 
      
      WRITTEN  20 
      
      I AM NOT READY! 
      
      WRITTEN  21 
      
      READING... 
      
      WRITTEN  22 
      
      I AM NOT READY! 
      
      WRITTEN  23 
      
      READING... 
      
      WRITTEN  24 
      
      I AM NOT READY! 
      
      WRITTEN  25 
      
      READING... 
      
      WRITTEN  26
      

      Realy hope for your help! I can't find useful information to solve this problem in Internet.

      I think it's not problem in device, because in another application I can't see my problem.

      Thank you in advance!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @oBOXPOH said in QSerialPort: problem with waitForReadyRead():

                      output = deviceControl_SerialPort->readAll(); // <-- READ
                      while (deviceControl_SerialPort->waitForReadyRead(10)) // <-- READ AGAIN
                          output += deviceControl_SerialPort->readAll();
      

      readAll() only reads whatever is available at the instant it is called. This can be anywhere from 1 byte to the total number you expect. Your while (waitForReadyRead(10)) may return false, and so exit the loop, when there is no data right now but there will be later on, meaning that you will miss the data this time round and pick it up only next time.

      I don't know much about serial ports, but at least try upping that 10 to a bigger number and see if it makes any difference?

      Make sure the other end is flushing whatever it writes immediately.

      You're not really "supposed to" use waitForReadyRead(10). You're "supposed to" use the readyRead() slot with signals. You could try that and see if it makes a difference.

      O 1 Reply Last reply
      1
      • K Offline
        K Offline
        kuzulis
        Qt Champions 2020
        wrote on last edited by
        #3

        What is OS, and what is Qt version?

        O 1 Reply Last reply
        0
        • JonBJ JonB

          @oBOXPOH said in QSerialPort: problem with waitForReadyRead():

                          output = deviceControl_SerialPort->readAll(); // <-- READ
                          while (deviceControl_SerialPort->waitForReadyRead(10)) // <-- READ AGAIN
                              output += deviceControl_SerialPort->readAll();
          

          readAll() only reads whatever is available at the instant it is called. This can be anywhere from 1 byte to the total number you expect. Your while (waitForReadyRead(10)) may return false, and so exit the loop, when there is no data right now but there will be later on, meaning that you will miss the data this time round and pick it up only next time.

          I don't know much about serial ports, but at least try upping that 10 to a bigger number and see if it makes any difference?

          Make sure the other end is flushing whatever it writes immediately.

          You're not really "supposed to" use waitForReadyRead(10). You're "supposed to" use the readyRead() slot with signals. You could try that and see if it makes a difference.

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

          @JonB said in QSerialPort: problem with waitForReadyRead():

          @oBOXPOH said in QSerialPort: problem with waitForReadyRead():

                          output = deviceControl_SerialPort->readAll(); // <-- READ
                          while (deviceControl_SerialPort->waitForReadyRead(10)) // <-- READ AGAIN
                              output += deviceControl_SerialPort->readAll();
          

          readAll() only reads whatever is available at the instant it is called. This can be anywhere from 1 byte to the total number you expect. Your while (waitForReadyRead(10)) may return false, and so exit the loop, when there is no data right now but there will be later on, meaning that you will miss the data this time round and pick it up.

          I don't know much about serial ports, but at least try upping that 10 to a bigger number and see if it makes any difference?

          Make sure the other end is flushing whatever it writes immediately.

          You're not really "supposed to" use waitForReadyRead(10). You're "supposed to" use the readyRead() slot with signals. You could try that and see if it makes a difference.

          In order to get data with readAll() I need to go through waitForReadyRead(), but sometimes I wait even 10 seconds - and no data anyway. When waitForReadyRead() returns true, then readAll() works fine.

          1 Reply Last reply
          0
          • J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            while loops and sleeps... why don't you simply use the asynchronous signals that QSerialPort provides ?

            Also connect the error signal to an output see if something is triggered there


            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
            4
            • K kuzulis

              What is OS, and what is Qt version?

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

              @kuzulis said in QSerialPort: problem with waitForReadyRead():

              What is OS, and what is Qt version?

              OS: Windows 10
              Qt Version: 5.13.1
              Building: MinGW 32-bit

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

                @kuzulis said in QSerialPort: problem with waitForReadyRead():

                What is OS, and what is Qt version?

                OS: Windows 10
                Qt Version: 5.13.1
                Building: MinGW 32-bit

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @oBOXPOH
                Like I & @J-Hilk have said, suggest you at least try it with readyRead signal instead of waits and see if that makes the difference?

                O 1 Reply Last reply
                1
                • O oBOXPOH

                  @kuzulis said in QSerialPort: problem with waitForReadyRead():

                  What is OS, and what is Qt version?

                  OS: Windows 10
                  Qt Version: 5.13.1
                  Building: MinGW 32-bit

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

                  @oBOXPOH
                  I would suggest to use a different Qt version QSerialPort has some serious issues in 5.13.1

                  see this bugreport
                  https://bugreports.qt.io/browse/QTBUG-78086


                  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 D 2 Replies Last reply
                  4
                  • J.HilkJ J.Hilk

                    @oBOXPOH
                    I would suggest to use a different Qt version QSerialPort has some serious issues in 5.13.1

                    see this bugreport
                    https://bugreports.qt.io/browse/QTBUG-78086

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

                    @J-Hilk said in QSerialPort: problem with waitForReadyRead():

                    @oBOXPOH
                    I would suggest to use a different Qt version QSerialPort has some serious issues in 5.13.1

                    see this bugreport
                    https://bugreports.qt.io/browse/QTBUG-78086

                    Tried Qt 5.9.8.

                    In this case waitForReadyRead() always returns false with accepted write command.

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @oBOXPOH
                      Like I & @J-Hilk have said, suggest you at least try it with readyRead signal instead of waits and see if that makes the difference?

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

                      @JonB said in QSerialPort: problem with waitForReadyRead():

                      @oBOXPOH
                      Like I & @J-Hilk have said, suggest you at least try it with readyRead signal instead of waits and see if that makes the difference?

                      I tried next (all is inside the class):

                      connect(deviceControl_SerialPort, &QSerialPort::readyRead, this, &Class::readData_slot);
                      
                      void Class::readData_slot()
                      {
                          qDebug() << "Ready Read" << endl;
                          deviceControl_readData.append(deviceControl_SerialPort->readAll());
                      }
                      

                      In this case I can't reach readData_slot() method.

                      Then I tried next:

                      connect(deviceControl_SerialPort, &QSerialPort::errorOccurred, this, &Class::serialPortError_slot);
                      
                      void Class::serialPortError_slot(QSerialPort::SerialPortError error)
                      {
                          qDebug() << error << endl;
                      }
                      
                      

                      and got some error (see log below):

                      WRITTEN  1 
                      
                      READING... 
                      
                      QSerialPort::TimeoutError 
                      
                      WRITTEN  2 
                      
                      READING... 
                      
                      QSerialPort::TimeoutError 
                      
                      WRITTEN  3 
                      
                      QSerialPort::TimeoutError 
                      
                      I AM NOT READY! 
                      
                      Ready Read 
                      
                      Ready Read 
                      

                      and, by the way, message for readyRead signal. Strangely, that I got it only after 2 real readings...

                      aha_1980A 1 Reply Last reply
                      0
                      • O oBOXPOH

                        @JonB said in QSerialPort: problem with waitForReadyRead():

                        @oBOXPOH
                        Like I & @J-Hilk have said, suggest you at least try it with readyRead signal instead of waits and see if that makes the difference?

                        I tried next (all is inside the class):

                        connect(deviceControl_SerialPort, &QSerialPort::readyRead, this, &Class::readData_slot);
                        
                        void Class::readData_slot()
                        {
                            qDebug() << "Ready Read" << endl;
                            deviceControl_readData.append(deviceControl_SerialPort->readAll());
                        }
                        

                        In this case I can't reach readData_slot() method.

                        Then I tried next:

                        connect(deviceControl_SerialPort, &QSerialPort::errorOccurred, this, &Class::serialPortError_slot);
                        
                        void Class::serialPortError_slot(QSerialPort::SerialPortError error)
                        {
                            qDebug() << error << endl;
                        }
                        
                        

                        and got some error (see log below):

                        WRITTEN  1 
                        
                        READING... 
                        
                        QSerialPort::TimeoutError 
                        
                        WRITTEN  2 
                        
                        READING... 
                        
                        QSerialPort::TimeoutError 
                        
                        WRITTEN  3 
                        
                        QSerialPort::TimeoutError 
                        
                        I AM NOT READY! 
                        
                        Ready Read 
                        
                        Ready Read 
                        

                        and, by the way, message for readyRead signal. Strangely, that I got it only after 2 real readings...

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

                        Hi @oBOXPOH,

                        Qt is an event-driven framework. By using endless loops, wait routines you create problems if you don't know what you are exactly doing.

                        You should use QSerialPort with signals and slots. An example is shown here: https://doc.qt.io/qt-5/qtserialport-terminal-example.html

                        Regards

                        Qt has to stay free or it will die.

                        O 1 Reply Last reply
                        5
                        • aha_1980A aha_1980

                          Hi @oBOXPOH,

                          Qt is an event-driven framework. By using endless loops, wait routines you create problems if you don't know what you are exactly doing.

                          You should use QSerialPort with signals and slots. An example is shown here: https://doc.qt.io/qt-5/qtserialport-terminal-example.html

                          Regards

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

                          @aha_1980 said in QSerialPort: problem with waitForReadyRead():

                          Hi @oBOXPOH,

                          Qt is an event-driven framework. By using endless loops, wait routines you create problems if you don't know what you are exactly doing.

                          You should use QSerialPort with signals and slots. An example is shown here: https://doc.qt.io/qt-5/qtserialport-terminal-example.html

                          Regards

                          Terminal Example also works badly -> https://forum.qt.io/topic/108848/terminal-example-for-qserialport-doesn-t-work-correctly

                          O 1 Reply Last reply
                          0
                          • O oBOXPOH

                            @aha_1980 said in QSerialPort: problem with waitForReadyRead():

                            Hi @oBOXPOH,

                            Qt is an event-driven framework. By using endless loops, wait routines you create problems if you don't know what you are exactly doing.

                            You should use QSerialPort with signals and slots. An example is shown here: https://doc.qt.io/qt-5/qtserialport-terminal-example.html

                            Regards

                            Terminal Example also works badly -> https://forum.qt.io/topic/108848/terminal-example-for-qserialport-doesn-t-work-correctly

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

                            @oBOXPOH said in QSerialPort: problem with waitForReadyRead():

                            @aha_1980 said in QSerialPort: problem with waitForReadyRead():

                            Hi @oBOXPOH,

                            Qt is an event-driven framework. By using endless loops, wait routines you create problems if you don't know what you are exactly doing.

                            You should use QSerialPort with signals and slots. An example is shown here: https://doc.qt.io/qt-5/qtserialport-terminal-example.html

                            Regards

                            Terminal Example also works badly -> https://forum.qt.io/topic/108848/terminal-example-for-qserialport-doesn-t-work-correctly

                            Solution is to install 5.13.2 or more version of Qt.

                            1 Reply Last reply
                            2
                            • D Offline
                              D Offline
                              DiaaQTdev
                              wrote on last edited by
                              #14

                              @oBOXPOH Hello i am having the same exact problem with QT, i don't receive any output by triggering the readyread signal or by using the return value of waitForReadyRead(), is it really issue related to QT version??
                              => I do have 5.12 version of QT, i want to make sure that that is the problem before deciding to do an upgrade..

                              Thank you in advance for your reply!

                              1 Reply Last reply
                              0
                              • J.HilkJ J.Hilk

                                @oBOXPOH
                                I would suggest to use a different Qt version QSerialPort has some serious issues in 5.13.1

                                see this bugreport
                                https://bugreports.qt.io/browse/QTBUG-78086

                                D Offline
                                D Offline
                                DiaaQTdev
                                wrote on last edited by
                                #15

                                @J-Hilk Hello, is it really a QT bug for versions under 5.13?

                                J.HilkJ 1 Reply Last reply
                                0
                                • D DiaaQTdev

                                  @J-Hilk Hello, is it really a QT bug for versions under 5.13?

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

                                  @DiaaQTdev

                                  iirc 5.12 should work fine, only 5.13 had the issues


                                  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
                                  1
                                  • D Offline
                                    D Offline
                                    DiaaQTdev
                                    wrote on last edited by
                                    #17

                                    @J-Hilk Thank you for your reply, i had upgraded my QT and i still had the same issue, when i send one byte i don't receive any feedback from my device(readyRead is not triggered), it has to be more than one byte in order to receive something. do you please have any idea why is this happening?

                                    J.HilkJ 1 Reply Last reply
                                    0
                                    • D DiaaQTdev

                                      @J-Hilk Thank you for your reply, i had upgraded my QT and i still had the same issue, when i send one byte i don't receive any feedback from my device(readyRead is not triggered), it has to be more than one byte in order to receive something. do you please have any idea why is this happening?

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

                                      @DiaaQTdev
                                      Can you show your code?


                                      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
                                      0

                                      • Login

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