Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Calculating checksum from QByteArray data



  • 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


  • Lifetime Qt Champion

    @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



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


  • Lifetime Qt Champion

    @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



  • @aha_1980 Thank you this helps a lot.


  • Lifetime Qt Champion

    @zgembo but note it is not complete yet ;)


  • Moderators

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


Log in to reply