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. Serial data received
Forum Update on Monday, May 27th 2025

Serial data received

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt c++serialportdatabuffer
19 Posts 5 Posters 2.6k 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.
  • J JonB
    26 Jul 2022, 09:51

    @manel-sam said in Serial data received:

    QByteArray hexData = d.append(serialPort->readAll().toHex());

    You show this as being called immediately. Are we supposed to guess this is actually in your pollSerialPort() slot, or what?

    QObject::connect(this->serialPort, &QSerialPort::readyRead, this, this {QTimer::singleShot(5,this,&Imu::pollSerialPort);});

    What are you trying to achieve by scheduling a read of the serial port in a delay after readyRead signal? Additionally this is not legal C++, so cannot be what you actually have....

    In a word: after readyRead signal readAll() will read whatever happens to have arrived when it is called, which could be anything from 1 byte to all bytes sent.

    M Offline
    M Offline
    manel.sam
    wrote on 26 Jul 2022, 10:00 last edited by
    #3

    @JonB Yes it's in my SerialPort

    Ok, I got it, so how to avoid receiving bytes data

    I would like to receive with data only.

    J J J 3 Replies Last reply 26 Jul 2022, 10:01
    0
    • M manel.sam
      26 Jul 2022, 10:00

      @JonB Yes it's in my SerialPort

      Ok, I got it, so how to avoid receiving bytes data

      I would like to receive with data only.

      J Offline
      J Offline
      JonB
      wrote on 26 Jul 2022, 10:01 last edited by
      #4

      @manel-sam said in Serial data received:

      Yes it's in my SerialPort

      In your SerialPort what?

      Ok, I got it, so how to avoid receiving bytes data

      I would like to receive with data only.

      Absolutely no idea what you mean by this.

      1 Reply Last reply
      0
      • M manel.sam
        26 Jul 2022, 10:00

        @JonB Yes it's in my SerialPort

        Ok, I got it, so how to avoid receiving bytes data

        I would like to receive with data only.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 26 Jul 2022, 10:02 last edited by
        #5

        @manel-sam said in Serial data received:

        so how to avoid receiving bytes data
        I would like to receive with data only

        What does this mean?!
        Everything consists of bytes in a computer...
        You probably want to know how to convert the bytes you get into some other types? Is that what you are asking?

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

        1 Reply Last reply
        0
        • M manel.sam
          26 Jul 2022, 10:00

          @JonB Yes it's in my SerialPort

          Ok, I got it, so how to avoid receiving bytes data

          I would like to receive with data only.

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 26 Jul 2022, 10:15 last edited by
          #6

          @manel-sam you have to accumulate the data each readRead signal and decide if the data arrived is complete or not.

          one usually has some kind of protocol/guideline for this.


          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
          • M Offline
            M Offline
            manel.sam
            wrote on 26 Jul 2022, 11:39 last edited by manel.sam
            #7

            then, I work on an IMU sensor.

            the received data are in hexadecimal form.

            the size of the received data is 37 bytes.
            I checked this to validate each data
            Serial received "555541321e7ff3029f61da00000000ffff00d2fffa0cbe24b124b124b1007b13d00000f4d8"
            headdd "5555"
            Serial received ""
            "data size is: " 37 bytes
            "Header is Ok"
            "the type of packet is ok"
            "data size is Ok"

            however I also receive data of 0 bytes, which are empty and this quite frequently, and I would like to receive only the data with 37 bytes, that is to say to regulate that my sensor receives only the good bytes

            Serial received "" "data size is
            "data size is: " 0 bytes
            "Header is not Ok , Ignore the packet
            "the type of packet is not ok , Ignore the packet"
            "Data size is not ok , Ignore the packet"

            @J-Hilk yes I tested with an accumulation of data and without
            @jsulm no I don't want to convert anything, just get the full data at each data reception

            Basically if my bytes are less than 0 then I don't receive valid data
            but I would like to make sure that I don't receive them

            I would like to receive each time the data in full form

            J 1 Reply Last reply 26 Jul 2022, 12:01
            0
            • M manel.sam
              26 Jul 2022, 11:39

              then, I work on an IMU sensor.

              the received data are in hexadecimal form.

              the size of the received data is 37 bytes.
              I checked this to validate each data
              Serial received "555541321e7ff3029f61da00000000ffff00d2fffa0cbe24b124b124b1007b13d00000f4d8"
              headdd "5555"
              Serial received ""
              "data size is: " 37 bytes
              "Header is Ok"
              "the type of packet is ok"
              "data size is Ok"

              however I also receive data of 0 bytes, which are empty and this quite frequently, and I would like to receive only the data with 37 bytes, that is to say to regulate that my sensor receives only the good bytes

              Serial received "" "data size is
              "data size is: " 0 bytes
              "Header is not Ok , Ignore the packet
              "the type of packet is not ok , Ignore the packet"
              "Data size is not ok , Ignore the packet"

              @J-Hilk yes I tested with an accumulation of data and without
              @jsulm no I don't want to convert anything, just get the full data at each data reception

              Basically if my bytes are less than 0 then I don't receive valid data
              but I would like to make sure that I don't receive them

              I would like to receive each time the data in full form

              J Offline
              J Offline
              JonB
              wrote on 26 Jul 2022, 12:01 last edited by JonB
              #8

              @manel-sam said in Serial data received:

              however I also receive data of 0 bytes, which are empty and this quite frequently

              • If you only call readAll() once in response to each readRead() signal you will never get 0 bytes.
              • If you get 0 bytes you are calling readAll() a second time/not in response to readyRead(). Find out where, and don't do it.

              I would like to receive each time the data in full form

              You cannot guarantee you will receive all bytes on any one particular readAll() call. Like @J-Hilk said you need to write code to accumulate the (potentially fragments) of data you receive.

              M 1 Reply Last reply 26 Jul 2022, 12:21
              2
              • M Offline
                M Offline
                manel.sam
                wrote on 26 Jul 2022, 12:11 last edited by manel.sam
                #9
                This post is deleted!
                1 Reply Last reply
                0
                • J JonB
                  26 Jul 2022, 12:01

                  @manel-sam said in Serial data received:

                  however I also receive data of 0 bytes, which are empty and this quite frequently

                  • If you only call readAll() once in response to each readRead() signal you will never get 0 bytes.
                  • If you get 0 bytes you are calling readAll() a second time/not in response to readyRead(). Find out where, and don't do it.

                  I would like to receive each time the data in full form

                  You cannot guarantee you will receive all bytes on any one particular readAll() call. Like @J-Hilk said you need to write code to accumulate the (potentially fragments) of data you receive.

                  M Offline
                  M Offline
                  manel.sam
                  wrote on 26 Jul 2022, 12:21 last edited by
                  #10

                  Imu::~Imu()
                  {
                  serialPort->close();
                  }

                  void Imu::handleError(QSerialPort::SerialPortError error)
                  {
                  if (error == QSerialPort::ResourceError){
                  qDebug()<<"Handle Error"<<error;
                  serialPort->close();
                  }
                  }

                  void Imu::pollSerialPort()
                  {

                   QByteArray d= serialPort->readAll().toHex();
                  
                     QByteArray hexData = d.append(serialPort->readAll().toHex());
                  
                  qDebug() << "Serial received " << hexData;
                  

                  i used it like this

                  J 1 Reply Last reply 26 Jul 2022, 12:32
                  0
                  • M manel.sam
                    26 Jul 2022, 12:21

                    Imu::~Imu()
                    {
                    serialPort->close();
                    }

                    void Imu::handleError(QSerialPort::SerialPortError error)
                    {
                    if (error == QSerialPort::ResourceError){
                    qDebug()<<"Handle Error"<<error;
                    serialPort->close();
                    }
                    }

                    void Imu::pollSerialPort()
                    {

                     QByteArray d= serialPort->readAll().toHex();
                    
                       QByteArray hexData = d.append(serialPort->readAll().toHex());
                    
                    qDebug() << "Serial received " << hexData;
                    

                    i used it like this

                    J Offline
                    J Offline
                    JonB
                    wrote on 26 Jul 2022, 12:32 last edited by JonB
                    #11

                    @manel-sam
                    This is wrong because you have two calls to serialPort->readAll(). How many more times can I say you cannot afford to do this? [In fact you kind of get away with this the way you write, but why are you calling it twice??]

                    M 1 Reply Last reply 26 Jul 2022, 12:53
                    2
                    • J JonB
                      26 Jul 2022, 12:32

                      @manel-sam
                      This is wrong because you have two calls to serialPort->readAll(). How many more times can I say you cannot afford to do this? [In fact you kind of get away with this the way you write, but why are you calling it twice??]

                      M Offline
                      M Offline
                      manel.sam
                      wrote on 26 Jul 2022, 12:53 last edited by
                      #12

                      @JonB Sorry, I copied the wrong code

                      I used the readAll once, but I still get the 0 Bytes

                      J J 2 Replies Last reply 26 Jul 2022, 12:57
                      0
                      • M manel.sam
                        26 Jul 2022, 12:53

                        @JonB Sorry, I copied the wrong code

                        I used the readAll once, but I still get the 0 Bytes

                        J Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 26 Jul 2022, 12:57 last edited by
                        #13

                        @manel-sam can you show more of your code, calling connect multiple times will lead to such a behaviour as well


                        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.

                        M 1 Reply Last reply 26 Jul 2022, 13:04
                        0
                        • J J.Hilk
                          26 Jul 2022, 12:57

                          @manel-sam can you show more of your code, calling connect multiple times will lead to such a behaviour as well

                          M Offline
                          M Offline
                          manel.sam
                          wrote on 26 Jul 2022, 13:04 last edited by
                          #14

                          @J-Hilk

                          Imu::Imu() :
                          moving(false)
                          {

                          serialPort = new QSerialPort("COM3",this);
                          
                          if (serialPort->open( QIODevice::ReadWrite))
                          {
                          
                              qDebug()<<"SerialPort"<<serialPort<<"Opened successufly";
                          }
                          else
                          {
                              qDebug()<<serialPort->error();
                              QSerialPortInfo::availablePorts();
                          }
                          
                          if (!serialPort->setBaudRate(57600))
                              log_error("imu","failed to set baudrate, error no %d",serialPort->error());
                          serialPort->setDataBits(QSerialPort::Data8);
                          serialPort->setParity(QSerialPort::NoParity);
                          serialPort->setStopBits(QSerialPort::OneStop); // One Stop bit
                          serialPort->setFlowControl(QSerialPort::NoFlowControl);
                          qDebug()<<"error"<<serialPort->errorString();
                          
                          QObject::connect(this->serialPort,SIGNAL(readyRead()),this,SLOT(pollSerialPort()));
                          QObject::connect(serialPort, &::QSerialPort::errorOccurred,this,&Imu::handleError);
                          
                          }
                          

                          Imu::~Imu()
                          {
                          serialPort->close();
                          }

                          void Imu::handleError(QSerialPort::SerialPortError error)
                          {
                          if (error == QSerialPort::ResourceError){
                          qDebug()<<"Handle Error"<<error;
                          serialPort->close();
                          }
                          }

                          void Imu::pollSerialPort()
                          {

                          QByteArray hexData;
                          hexData.append(serialPort->readAll().toHex());
                          
                          qDebug() << "Serial received " << hexData;
                          QByteArray data = QByteArray::fromHex(hexData);
                          
                          // data size
                          qInfo() << QStringLiteral("data size is : ") << data.size() << " octets";
                          
                          QDataStream stream(data);
                          
                          // Read The BigIndian value
                          stream.setByteOrder(QDataStream::BigEndian);
                          qint16 headerSignature;
                          stream >> headerSignature;
                          
                          M 1 Reply Last reply 26 Jul 2022, 13:19
                          0
                          • M manel.sam
                            26 Jul 2022, 13:04

                            @J-Hilk

                            Imu::Imu() :
                            moving(false)
                            {

                            serialPort = new QSerialPort("COM3",this);
                            
                            if (serialPort->open( QIODevice::ReadWrite))
                            {
                            
                                qDebug()<<"SerialPort"<<serialPort<<"Opened successufly";
                            }
                            else
                            {
                                qDebug()<<serialPort->error();
                                QSerialPortInfo::availablePorts();
                            }
                            
                            if (!serialPort->setBaudRate(57600))
                                log_error("imu","failed to set baudrate, error no %d",serialPort->error());
                            serialPort->setDataBits(QSerialPort::Data8);
                            serialPort->setParity(QSerialPort::NoParity);
                            serialPort->setStopBits(QSerialPort::OneStop); // One Stop bit
                            serialPort->setFlowControl(QSerialPort::NoFlowControl);
                            qDebug()<<"error"<<serialPort->errorString();
                            
                            QObject::connect(this->serialPort,SIGNAL(readyRead()),this,SLOT(pollSerialPort()));
                            QObject::connect(serialPort, &::QSerialPort::errorOccurred,this,&Imu::handleError);
                            
                            }
                            

                            Imu::~Imu()
                            {
                            serialPort->close();
                            }

                            void Imu::handleError(QSerialPort::SerialPortError error)
                            {
                            if (error == QSerialPort::ResourceError){
                            qDebug()<<"Handle Error"<<error;
                            serialPort->close();
                            }
                            }

                            void Imu::pollSerialPort()
                            {

                            QByteArray hexData;
                            hexData.append(serialPort->readAll().toHex());
                            
                            qDebug() << "Serial received " << hexData;
                            QByteArray data = QByteArray::fromHex(hexData);
                            
                            // data size
                            qInfo() << QStringLiteral("data size is : ") << data.size() << " octets";
                            
                            QDataStream stream(data);
                            
                            // Read The BigIndian value
                            stream.setByteOrder(QDataStream::BigEndian);
                            qint16 headerSignature;
                            stream >> headerSignature;
                            
                            M Offline
                            M Offline
                            mpergand
                            wrote on 26 Jul 2022, 13:19 last edited by mpergand
                            #15

                            @manel-sam said in Serial data received:

                            QByteArray hexData;
                            hexData.append(serialPort->readAll().toHex());

                            hexData is local, so you append nothing !

                            hexData.append(serialPort->readAll().toHex());
                            qDebug() << "Serial received " << hexData;
                            QByteArray data = QByteArray::fromHex(hexData);

                            you convert to ascii then back to binary !!??

                            You must check the packet is complete (37 bytes) before processing any further.

                            M 1 Reply Last reply 26 Jul 2022, 13:25
                            3
                            • M manel.sam
                              26 Jul 2022, 12:53

                              @JonB Sorry, I copied the wrong code

                              I used the readAll once, but I still get the 0 Bytes

                              J Offline
                              J Offline
                              JonB
                              wrote on 26 Jul 2022, 13:21 last edited by JonB
                              #16

                              @manel-sam said in Serial data received:

                              I used the readAll once, but I still get the 0 Bytes

                              And where in your code do you now get this?

                              Separately you need to follow @mpergand's comments.

                              In handleError() you only report anything if QSerialPort::ResourceError, and silently ignore if any other error, for whatever reason. Please don't write code like this, and certainly not while developing/debugging.

                              M 1 Reply Last reply 26 Jul 2022, 13:40
                              0
                              • M mpergand
                                26 Jul 2022, 13:19

                                @manel-sam said in Serial data received:

                                QByteArray hexData;
                                hexData.append(serialPort->readAll().toHex());

                                hexData is local, so you append nothing !

                                hexData.append(serialPort->readAll().toHex());
                                qDebug() << "Serial received " << hexData;
                                QByteArray data = QByteArray::fromHex(hexData);

                                you convert to ascii then back to binary !!??

                                You must check the packet is complete (37 bytes) before processing any further.

                                M Offline
                                M Offline
                                manel.sam
                                wrote on 26 Jul 2022, 13:25 last edited by manel.sam
                                #17

                                @mpergand Yes it was just to visualize the data in hexa as in the documentation of my sensor, then I preferred to work in binary.

                                So the append I should do it where§?

                                M 1 Reply Last reply 26 Jul 2022, 13:28
                                0
                                • M manel.sam
                                  26 Jul 2022, 13:25

                                  @mpergand Yes it was just to visualize the data in hexa as in the documentation of my sensor, then I preferred to work in binary.

                                  So the append I should do it where§?

                                  M Offline
                                  M Offline
                                  mpergand
                                  wrote on 26 Jul 2022, 13:28 last edited by mpergand
                                  #18

                                  @manel-sam said in Serial data received:

                                  So the append I should do it where§?

                                  Create a member variable in your imu class.

                                      // _packetData  member variable of imu
                                      _packetData.append(serialPort->readAll());
                                      
                                      if(_packetData.size()>=PACKET_LENGTH)
                                          {
                                          // packet complete
                                          qInfo() << QStringLiteral("data size is : ") << _packetData.size() << " octets";
                                          qDebug() << "Serial received " << _packetData.toHex();
                                          
                                          // go ahead with this packet
                                          QDataStream stream(_packetData);
                                          
                                          // Read The BigIndian value
                                          stream.setByteOrder(QDataStream::BigEndian);
                                          qint16 headerSignature;
                                          stream >> headerSignature;
                                          }
                                  

                                  You may also check the data received don't exceed PACKET_LENGTH, cause it means you have already received data from the next packet. (it may be irrelevant in your case, I don't kown)

                                  1 Reply Last reply
                                  0
                                  • J JonB
                                    26 Jul 2022, 13:21

                                    @manel-sam said in Serial data received:

                                    I used the readAll once, but I still get the 0 Bytes

                                    And where in your code do you now get this?

                                    Separately you need to follow @mpergand's comments.

                                    In handleError() you only report anything if QSerialPort::ResourceError, and silently ignore if any other error, for whatever reason. Please don't write code like this, and certainly not while developing/debugging.

                                    M Offline
                                    M Offline
                                    manel.sam
                                    wrote on 26 Jul 2022, 13:40 last edited by manel.sam
                                    #19
                                    This post is deleted!
                                    1 Reply Last reply
                                    0

                                    12/19

                                    26 Jul 2022, 12:53

                                    • Login

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