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. Issue with QSerialPort: Raspberry Pi - Arduino
Forum Update on Monday, May 27th 2025

Issue with QSerialPort: Raspberry Pi - Arduino

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.9k 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.
  • R Offline
    R Offline
    Ryan R
    wrote on 22 Oct 2020, 10:16 last edited by
    #1

    Hey guys,

    I'm currently trying to make a bi-directional Serial communication protocol between a Raspberry Pi and a custom Arduino microcontroller. The Arduino uses C and the Pi uses a Qt GUI with C++/XML. I based my program from the QSerialPort example in Qt Creator, and now runs in a separate thread to the rest of the program.

    90% of the time it works perfectly, but every now and then, the Pi receives a "scrambled" command from the Arduino.

    Example of correct message as QByteArray:
    "<12,1,3,400,99,80,75>"

    Example of scrambled message as QByteArray:
    "<12,1,3,400,99,1\x98""b\x82\xF2\xFE"

    Example of scrambled message as QString:
    "<12,1,3,400,99,1�b���"

    Serial Port Init:

        QSerialPort serial;
        serial.setPortName("ttyS0"); // GPIO
    
        serial.setBaudRate(QSerialPort::Baud9600);
        serial.setDataBits(QSerialPort::Data8);
        serial.setFlowControl(QSerialPort::NoFlowControl);
        serial.setParity(QSerialPort::NoParity);
        serial.setStopBits(QSerialPort::OneStop); // Is this correct?
    
        if (!serial.open(QIODevice::ReadWrite))
        {
            qDebug() << "dispenseThread::run: ERROR: Cannot open serial port!";
            qDebug() << serial.errorString();
        }
    

    Serial Port Read:

            QByteArray responseData = serial.readAll();
            while (serial.waitForReadyRead(10))
            {
                responseData += serial.readAll();
            }
    
            const QString response = QString::fromUtf8(responseData);
            QString readBuff = response;
    
            qDebug() << "QByteArray Buffer: " << responseData;
            qDebug() << "Const QString Buffer: " << response;
            qDebug() << "QString Buffer: " << readBuff;
    

    Is this normal for Serial, in which case I need to verify each command has been sent successfully, or is there something wrong with the configuration or buffer?

    Any help would be appreciated!

    Thanks,
    Ryan

    J P 2 Replies Last reply 22 Oct 2020, 10:38
    0
    • R Ryan R
      22 Oct 2020, 10:16

      Hey guys,

      I'm currently trying to make a bi-directional Serial communication protocol between a Raspberry Pi and a custom Arduino microcontroller. The Arduino uses C and the Pi uses a Qt GUI with C++/XML. I based my program from the QSerialPort example in Qt Creator, and now runs in a separate thread to the rest of the program.

      90% of the time it works perfectly, but every now and then, the Pi receives a "scrambled" command from the Arduino.

      Example of correct message as QByteArray:
      "<12,1,3,400,99,80,75>"

      Example of scrambled message as QByteArray:
      "<12,1,3,400,99,1\x98""b\x82\xF2\xFE"

      Example of scrambled message as QString:
      "<12,1,3,400,99,1�b���"

      Serial Port Init:

          QSerialPort serial;
          serial.setPortName("ttyS0"); // GPIO
      
          serial.setBaudRate(QSerialPort::Baud9600);
          serial.setDataBits(QSerialPort::Data8);
          serial.setFlowControl(QSerialPort::NoFlowControl);
          serial.setParity(QSerialPort::NoParity);
          serial.setStopBits(QSerialPort::OneStop); // Is this correct?
      
          if (!serial.open(QIODevice::ReadWrite))
          {
              qDebug() << "dispenseThread::run: ERROR: Cannot open serial port!";
              qDebug() << serial.errorString();
          }
      

      Serial Port Read:

              QByteArray responseData = serial.readAll();
              while (serial.waitForReadyRead(10))
              {
                  responseData += serial.readAll();
              }
      
              const QString response = QString::fromUtf8(responseData);
              QString readBuff = response;
      
              qDebug() << "QByteArray Buffer: " << responseData;
              qDebug() << "Const QString Buffer: " << response;
              qDebug() << "QString Buffer: " << readBuff;
      

      Is this normal for Serial, in which case I need to verify each command has been sent successfully, or is there something wrong with the configuration or buffer?

      Any help would be appreciated!

      Thanks,
      Ryan

      J Offline
      J Offline
      JonB
      wrote on 22 Oct 2020, 10:38 last edited by JonB
      #2

      @Ryan-R
      I must admit I too find this area confusing after years of attempting to understand(!), but are the QByteArray contents you show really UTF-8??
      https://doc.qt.io/qt-5/qstring.html#fromUtf8 :

      UTF-8 is a Unicode codec and can represent all characters in a Unicode string like QString. However, invalid sequences are possible with UTF-8 and, if any such are found, they will be replaced with one or more "replacement characters", or suppressed. These include non-Unicode sequences, non-characters, overlong sequences or surrogate codepoints encoded into UTF-8.

      Separately, if some of these are "funny" characters I wouldn't rely on the visual output from qDebug() "looking" right. verify what is actually in the bad-looking positions in the strings?

      1 Reply Last reply
      1
      • R Ryan R
        22 Oct 2020, 10:16

        Hey guys,

        I'm currently trying to make a bi-directional Serial communication protocol between a Raspberry Pi and a custom Arduino microcontroller. The Arduino uses C and the Pi uses a Qt GUI with C++/XML. I based my program from the QSerialPort example in Qt Creator, and now runs in a separate thread to the rest of the program.

        90% of the time it works perfectly, but every now and then, the Pi receives a "scrambled" command from the Arduino.

        Example of correct message as QByteArray:
        "<12,1,3,400,99,80,75>"

        Example of scrambled message as QByteArray:
        "<12,1,3,400,99,1\x98""b\x82\xF2\xFE"

        Example of scrambled message as QString:
        "<12,1,3,400,99,1�b���"

        Serial Port Init:

            QSerialPort serial;
            serial.setPortName("ttyS0"); // GPIO
        
            serial.setBaudRate(QSerialPort::Baud9600);
            serial.setDataBits(QSerialPort::Data8);
            serial.setFlowControl(QSerialPort::NoFlowControl);
            serial.setParity(QSerialPort::NoParity);
            serial.setStopBits(QSerialPort::OneStop); // Is this correct?
        
            if (!serial.open(QIODevice::ReadWrite))
            {
                qDebug() << "dispenseThread::run: ERROR: Cannot open serial port!";
                qDebug() << serial.errorString();
            }
        

        Serial Port Read:

                QByteArray responseData = serial.readAll();
                while (serial.waitForReadyRead(10))
                {
                    responseData += serial.readAll();
                }
        
                const QString response = QString::fromUtf8(responseData);
                QString readBuff = response;
        
                qDebug() << "QByteArray Buffer: " << responseData;
                qDebug() << "Const QString Buffer: " << response;
                qDebug() << "QString Buffer: " << readBuff;
        

        Is this normal for Serial, in which case I need to verify each command has been sent successfully, or is there something wrong with the configuration or buffer?

        Any help would be appreciated!

        Thanks,
        Ryan

        P Offline
        P Offline
        Pl45m4
        wrote on 22 Oct 2020, 11:08 last edited by
        #3

        @Ryan-R said in Issue with QSerialPort: Raspberry Pi - Arduino:

        while (serial.waitForReadyRead(10))
        {
        responseData += serial.readAll();
        }

        This is probably the issue. You wait 10msecs before you add data to your output. If not all of the data sent, is transmitted, you get some garbage :)

        Do you use any of the signals QSerialPort provides?


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        J 1 Reply Last reply 22 Oct 2020, 11:23
        2
        • P Pl45m4
          22 Oct 2020, 11:08

          @Ryan-R said in Issue with QSerialPort: Raspberry Pi - Arduino:

          while (serial.waitForReadyRead(10))
          {
          responseData += serial.readAll();
          }

          This is probably the issue. You wait 10msecs before you add data to your output. If not all of the data sent, is transmitted, you get some garbage :)

          Do you use any of the signals QSerialPort provides?

          J Offline
          J Offline
          JonB
          wrote on 22 Oct 2020, 11:23 last edited by JonB
          #4

          @Pl45m4

          You wait 10msecs before you add data to your output. If not all of the data sent, is transmitted, you get some garbage :)

          How does that lead to "corruption"? responseData only holds bytes which have actually arrived, and knows the length, so how can that lead to "extra", rubbish characters? (Not that I know anything about how serial ports work, if that matters?!) I can see it might lead to not all characters having been received, but not extra characters?

          1 Reply Last reply
          1
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 22 Oct 2020, 11:46 last edited by
            #5

            Hi,

            The usual way to do serial communication is to establish a protocol so you can know when you have received a full frame of your data. Most of the time you have a start and end character or character sequence to determine the limits of your frame.

            On the receiver end you cumulate the data in a buffer that you check regularly so you know when all the data required have arrived. Then you can extract that frame, process it and go on with your logic.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            6
            • R Offline
              R Offline
              Ryan R
              wrote on 22 Oct 2020, 16:02 last edited by
              #6

              Thanks for the fast replies!

              Do you mean something like this, or do you mean read the Serial Port character-by-character, looking for the open and close commands?

              My open command is "<" and close command is ">".

              while (serial.waitForReadyRead(10) && (!responseData.contains("<") && !responseData.contains(">")))
                      {
                          responseData += serial.readAll();
                      }
              

              Thanks,
              Ryan

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 22 Oct 2020, 18:17 last edited by
                #7

                The cumulation is correct but not the loop. Once you got the data, check for the frame. If not there, let your application live and the next time readyRead is fired, it will do the same and you might have there the complete frame. No need for a tight loop.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                3

                6/7

                22 Oct 2020, 16:02

                • Login

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