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. how to read the data bytes of a packet?

how to read the data bytes of a packet?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt c++bytesbytearray
7 Posts 4 Posters 720 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 Offline
    M Offline
    manel.sam
    wrote on last edited by
    #1

    I would like to extract each time the data bytes of my IMU data which are in the following form:

    "5555 41321e7fea02a2efb400010005fffe00d6fff90cbb261926192619006692340000bf5f"

    I would like to make sure that the data received header 0x5555 , because The packet header is always the bit pattern 0x5555.

    I tried with this code but it doesn't give anything, I don't really know how to read the bytes of a packet ( sorry I am a beginner ) .

    void Imu::pollSerialPort()
    {
    static const unsigned char START_BYTES[2] = {0x55};

    static const QByteArray START_WORD((char*)START_BYTES,2);
    int numToPop=0;

    static QTime startTime = QTime::currentTime();
    QByteArray data= serialPort->readAll().toHex();
    qDebug() << "Serial received " << data;

    // find header
    for(numToPop=0; numToPop+1<data.size() ;numToPop+=1)
    {
    if(0x55==data.indexOf(START_WORD))

    break;
    

    else
    log_warning("imu","dropping %d bytes before header recovery");
    /* header was not found */

    }
    
    M JonBJ 2 Replies Last reply
    0
    • M manel.sam

      I would like to extract each time the data bytes of my IMU data which are in the following form:

      "5555 41321e7fea02a2efb400010005fffe00d6fff90cbb261926192619006692340000bf5f"

      I would like to make sure that the data received header 0x5555 , because The packet header is always the bit pattern 0x5555.

      I tried with this code but it doesn't give anything, I don't really know how to read the bytes of a packet ( sorry I am a beginner ) .

      void Imu::pollSerialPort()
      {
      static const unsigned char START_BYTES[2] = {0x55};

      static const QByteArray START_WORD((char*)START_BYTES,2);
      int numToPop=0;

      static QTime startTime = QTime::currentTime();
      QByteArray data= serialPort->readAll().toHex();
      qDebug() << "Serial received " << data;

      // find header
      for(numToPop=0; numToPop+1<data.size() ;numToPop+=1)
      {
      if(0x55==data.indexOf(START_WORD))

      break;
      

      else
      log_warning("imu","dropping %d bytes before header recovery");
      /* header was not found */

      }
      
      M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      @manel-sam said in how to read the data bytes of a packet?:

      QByteArray data= serialPort->readAll().toHex();

      No need to convert to string.

      Seems to work:

          char hd[]={0x55,0x55};
          QByteArray dataHD=data.left(2);
      
          if(dataHD.compare(hd))
              {
              qDebug()<<"OK";
              }
      
      1 Reply Last reply
      2
      • M manel.sam

        I would like to extract each time the data bytes of my IMU data which are in the following form:

        "5555 41321e7fea02a2efb400010005fffe00d6fff90cbb261926192619006692340000bf5f"

        I would like to make sure that the data received header 0x5555 , because The packet header is always the bit pattern 0x5555.

        I tried with this code but it doesn't give anything, I don't really know how to read the bytes of a packet ( sorry I am a beginner ) .

        void Imu::pollSerialPort()
        {
        static const unsigned char START_BYTES[2] = {0x55};

        static const QByteArray START_WORD((char*)START_BYTES,2);
        int numToPop=0;

        static QTime startTime = QTime::currentTime();
        QByteArray data= serialPort->readAll().toHex();
        qDebug() << "Serial received " << data;

        // find header
        for(numToPop=0; numToPop+1<data.size() ;numToPop+=1)
        {
        if(0x55==data.indexOf(START_WORD))

        break;
        

        else
        log_warning("imu","dropping %d bytes before header recovery");
        /* header was not found */

        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @manel-sam
        In addition to what @mpergand has written, you should not write your code the way you have to assume serialPort->readAll() will return all the bytes sent to you. It only retrieves whatever is available at the instant it is called. Sooner or later your code approach will go wrong. You will need to rewrite to save/buffer the bytes received.

        1 Reply Last reply
        2
        • M Offline
          M Offline
          manel.sam
          wrote on last edited by
          #4

          @mpergand it gives me the Ok but when the header is different from 0*5555

          Christian EhrlicherC JonBJ 2 Replies Last reply
          0
          • M manel.sam

            @mpergand it gives me the Ok but when the header is different from 0*5555

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @manel-sam said in how to read the data bytes of a packet?:

            but when the header is different from 0*5555

            Then 'OK' is not printed to your debug console.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            M 1 Reply Last reply
            0
            • M manel.sam

              @mpergand it gives me the Ok but when the header is different from 0*5555

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

              @manel-sam
              Please take your own time to read docs at int QByteArray::compare(). @mpergand made a slip, it should be if (dataHD.compare(hd) == 0) qDebug()<<"OK"; but you can look that up yourself.

              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @manel-sam said in how to read the data bytes of a packet?:

                but when the header is different from 0*5555

                Then 'OK' is not printed to your debug console.

                M Offline
                M Offline
                manel.sam
                wrote on last edited by
                #7

                @Christian-Ehrlicher it's printed

                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