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
Qt 6.11 is out! See what's new in the release blog

Serial data received

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt c++serialportdatabuffer
19 Posts 5 Posters 5.0k Views 1 Watching
  • 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.
  • 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