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

Serial data received

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt c++serialportdatabuffer
19 Posts 5 Posters 2.5k 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.
  • M manel.sam

    @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.

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on 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

      @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.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on 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 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

        JonBJ 1 Reply Last reply
        0
        • M manel.sam

          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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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
          2
          • M Offline
            M Offline
            manel.sam
            wrote on last edited by manel.sam
            #9
            This post is deleted!
            1 Reply Last reply
            0
            • JonBJ JonB

              @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 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

              JonBJ 1 Reply Last reply
              0
              • M manel.sam

                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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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
                2
                • JonBJ JonB

                  @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 last edited by
                  #12

                  @JonB Sorry, I copied the wrong code

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

                  J.HilkJ JonBJ 2 Replies Last reply
                  0
                  • M manel.sam

                    @JonB Sorry, I copied the wrong code

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

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on 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
                    0
                    • J.HilkJ J.Hilk

                      @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 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
                      0
                      • M manel.sam

                        @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 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
                        3
                        • M manel.sam

                          @JonB Sorry, I copied the wrong code

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

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on 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
                          0
                          • M mpergand

                            @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 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
                            0
                            • M manel.sam

                              @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 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
                              • JonBJ JonB

                                @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 last edited by manel.sam
                                #19
                                This post is deleted!
                                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