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. Calculating checksum from QByteArray data
Forum Updated to NodeBB v4.3 + New Features

Calculating checksum from QByteArray data

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 7.4k 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.
  • ZgemboZ Offline
    ZgemboZ Offline
    Zgembo
    wrote on last edited by
    #1

    Hi all,

    I am working on a project where I need to receive data from a device that is connected to PC via serial port. I am trying to create a function to calculate checksum of received data.
    Device manual says that checksum is calculated like this:
    0_1564670379909_be4777a1-a230-465f-b8dc-92931f25865a-image.png

    I have created I function that has one parameter QByteArray datas

    qint16 sumOfBytes = 0;

    for (int i = 0; i < datas.length() - 3; i++) {
    	sumOfBytes += static_cast<quint8>(datas.at(i));
    }
     //negate a sumOfBytes
    sumOfBytes *= -1;
    
    bool ok;
    //take last for characters
    QString hexvalue = (QString::number(sumOfBytes, 16)).right(4);
    //convert to binary 4 byte data 
    //QString binaryVal = QString("%1").arg(hexvalue.toULongLong(&ok, 16), 16, 2, QChar('0'));
    QString Lbyte = hexvalue.left(2);
    QString Mbyte = hexvalue.right(4);
    

    but I am not quite sure how to move from here. Any suggestion is welcome.

    Tnx,
    Zgembo

    aha_1980A 1 Reply Last reply
    0
    • ZgemboZ Zgembo

      Hi all,

      I am working on a project where I need to receive data from a device that is connected to PC via serial port. I am trying to create a function to calculate checksum of received data.
      Device manual says that checksum is calculated like this:
      0_1564670379909_be4777a1-a230-465f-b8dc-92931f25865a-image.png

      I have created I function that has one parameter QByteArray datas

      qint16 sumOfBytes = 0;

      for (int i = 0; i < datas.length() - 3; i++) {
      	sumOfBytes += static_cast<quint8>(datas.at(i));
      }
       //negate a sumOfBytes
      sumOfBytes *= -1;
      
      bool ok;
      //take last for characters
      QString hexvalue = (QString::number(sumOfBytes, 16)).right(4);
      //convert to binary 4 byte data 
      //QString binaryVal = QString("%1").arg(hexvalue.toULongLong(&ok, 16), 16, 2, QChar('0'));
      QString Lbyte = hexvalue.left(2);
      QString Mbyte = hexvalue.right(4);
      

      but I am not quite sure how to move from here. Any suggestion is welcome.

      Tnx,
      Zgembo

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @zgembo said in Calculating checksum from QByteArray data:

      but I am not quite sure how to move from here. Any suggestion is welcome.

      You should tell us in which direction you want to move.

      I assume you want to append the checksum to the data array, but that's not clear from your description.

      //negate a sumOfBytes
      sumOfBytes *= -1;

      Where is that stated?

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      1
      • ZgemboZ Offline
        ZgemboZ Offline
        Zgembo
        wrote on last edited by
        #3

        The data is send in a block of 24 bytes. First 21 byte is data, 22 and 23 is checksum and 24 is zero or end byte.
        The datasheet says that in order to calculate check sum one need to sum first 21 byte as I did in a for loop and I have stored that sum in sumOfBytes variable.
        Next you need to substract 0x0000 - sumOfBytes, I did that by multiplying sumOfBytes with negative one.
        From the result I have extracted the last 4 characters that should be hex values and I have split them into first two and last two hex values.
        Now I am not sure how to implement second, third and fourth section from the Calculations part of datasheet.

        aha_1980A 1 Reply Last reply
        0
        • ZgemboZ Zgembo

          The data is send in a block of 24 bytes. First 21 byte is data, 22 and 23 is checksum and 24 is zero or end byte.
          The datasheet says that in order to calculate check sum one need to sum first 21 byte as I did in a for loop and I have stored that sum in sumOfBytes variable.
          Next you need to substract 0x0000 - sumOfBytes, I did that by multiplying sumOfBytes with negative one.
          From the result I have extracted the last 4 characters that should be hex values and I have split them into first two and last two hex values.
          Now I am not sure how to implement second, third and fourth section from the Calculations part of datasheet.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @zgembo I can follow this example up to the intermediate sum 0xf55b.

          #include <QDebug>
          
          int main(int argc, char *argv[])
          {
              const uint8_t rawData[] = {
                  0x42, 0x8f, 0x80, 0x80, 0x80, 0xb0, 0x81, 0x80, 0x80, 0xA0, 0x83, 0x80,
                  0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00
              };
          
              auto data = QByteArray::fromRawData(reinterpret_cast<const char *>(rawData), sizeof(rawData));
          
              quint16 sum = 0;
              int i = 0;
              int len = data.length() - 3;
              for (i = 0; i < len; ++i)
                  sum += quint8(data.at(i));
          
              sum = -sum;
          
              qDebug("sum = %04x", sum);
          
              quint8 msb = sum >> 8;
              quint8 lsb = sum & 0xFF;
          
              qDebug("MSB = %02x, LSB = %02x", msb, lsb);
          }
          

          Output:

          sum = f55b
          MSB = f5, LSB = 5b
          

          you will need to provide some more information about that TData format. It seems the upper 9 bit are rotated, but that's a guess.

          Regards

          Qt has to stay free or it will die.

          ZgemboZ 1 Reply Last reply
          2
          • aha_1980A aha_1980

            @zgembo I can follow this example up to the intermediate sum 0xf55b.

            #include <QDebug>
            
            int main(int argc, char *argv[])
            {
                const uint8_t rawData[] = {
                    0x42, 0x8f, 0x80, 0x80, 0x80, 0xb0, 0x81, 0x80, 0x80, 0xA0, 0x83, 0x80,
                    0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00
                };
            
                auto data = QByteArray::fromRawData(reinterpret_cast<const char *>(rawData), sizeof(rawData));
            
                quint16 sum = 0;
                int i = 0;
                int len = data.length() - 3;
                for (i = 0; i < len; ++i)
                    sum += quint8(data.at(i));
            
                sum = -sum;
            
                qDebug("sum = %04x", sum);
            
                quint8 msb = sum >> 8;
                quint8 lsb = sum & 0xFF;
            
                qDebug("MSB = %02x, LSB = %02x", msb, lsb);
            }
            

            Output:

            sum = f55b
            MSB = f5, LSB = 5b
            

            you will need to provide some more information about that TData format. It seems the upper 9 bit are rotated, but that's a guess.

            Regards

            ZgemboZ Offline
            ZgemboZ Offline
            Zgembo
            wrote on last edited by
            #5

            @aha_1980 Thank you this helps a lot.

            aha_1980A J.HilkJ 2 Replies Last reply
            0
            • ZgemboZ Zgembo

              @aha_1980 Thank you this helps a lot.

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by aha_1980
              #6

              @zgembo but note it is not complete yet ;)

              Qt has to stay free or it will die.

              1 Reply Last reply
              0
              • ZgemboZ Zgembo

                @aha_1980 Thank you this helps a lot.

                J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #7

                @zgembo are you aware, that there is a related non-member function for checksum calculation on a QByteArray?

                https://doc.qt.io/qt-5/qbytearray.html#qChecksum

                Maybe that's already all you need?


                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

                • Login

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