Checksum error calculation - solved

Dear All,

I'm trying to calculate the check sum in this array , the result should be D3 but when i'm getting different values .

the sum will be calculated from the whole package's single bytes in the array, the checksum value in position array[3], and by checking the value i will know if i have correct or wrong message.

char array[36]= {00,0x24,00,0xD3,01,00,00,00,01,00,00,00,01,00,00,00,01,00,00,00,01,00,00,00,01,00,00,00,01,00,00,00,01,00,00,00};
char checksum ; 

void setup() {
  // put your setup code here, to run once:

    Serial.begin(115200);
  Serial.println();

}

void loop() {

  char sum = 0;
    for (int i = 0; i < 36; i++) {
        sum += array[i];
    }
       
        checksum = 0xff- sum ; 
        Serial.println(checksum);

}

Why are you using char ?

because i thought it should be the same type with the array, am i wrong :frowning: ?

Your calculations will be incorrect because value of char is between 0 and 255 only.

And next - where are you find that checksum algorithm? I doubt it is correct - you calculate the checksum over the entire array, while usually the checksum value itself is not involved in the calculation,

I tried integer instead also it gave me wrong answer, the algorithm is defined like following:

'' If the message is shorter than 100 byte, the sum s0 will be calculated from the whole package's single bytes. If the message contains 100 bytes or more, s0 will be calculated from the message's first and the last 50 bytes.

In both cases the checksum byte must be initialized with 0. The checksum is calculated to

checksum = 0xff - s0 ''

How can you calculate a checksum over a range of data, when the checksum itself is within the range?

1 Like

It will be add as zero to the sum.

so your code in first post is wrong, because you use actual value of checksum rather than zero for this byte

1 Like

Perhaps you quite misunderstood this. Could you provide the link to the original document?

Yes sure, because i couldn't follow to be honest, there is no link but it is a document on my pc for educational robot called robotino , i will copy past the whole page if that is okay.

If the message is shorter than 100 byte, the sum s0 will be calculated from the whole package's single bytes. If the message contains 100 bytes or more, s0 will be calculated from the message's first and the last 50 bytes.

In both cases the checksum byte must be initialized with 0. The checksum is calculated to

checksum = 0xff - s0



unsigned char checksum( const unsigned char* payload, unsigned int payloadLength ) const

{

 unsigned char s0 = 0;

 

 if( payloadLength < 100 )

 {

         for( int i = 0; i < payloadLength; ++i )

         {

                 s0 += payload[i];

         }

 }

 else

 {

         for( int i = 0; i < 50; ++i )

         {

                 s0 += payload[i];

         }

         for( int i = payloadLength-1; i >= payloadLength - 50; --i )

         {

                 s0 += payload[i];

         }

 }

 

 return ( 0xFF - s0 );

}

To check if the package has been transmitted correctly, the whole message's single bytes will be accumulated to the byte sum s1 if the message is shorter than 100 byte. If it contains 100 bytes or more, s1 is calculated from the message's first and last 50 bytes.

The package is correct if

s1 = 0xFF

byte array[36] = {00, 0x24, 00, 0x00, 01, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00};
byte checksum, sum = 0;

void setup()
{
  Serial.begin(115200);

  for (int i = 0; i < 36; i++)
    sum += array[i];

  checksum = 0xff - sum ;

  Serial.print("checksum 0x");
  Serial.println(checksum, HEX);
}

void loop()
{
  
}
1 Like

Thank you so much, it is working now.

As I said, you misunderstood the method.
I think that the correct schema is : when you calculate checksum on the received packet, you should replace the array[3] value by zero and than calculate. The calculated checksum should be equal to actual value of array[3]

Thank you so much, it is working now thanks to you.

please mark the thread as "solved"

The original message array of the OP is the following of which array[3] is the checksum.

char array[36]= {00,0x24,00,0xD3,01,00,00,00,01,00,00,00,01,00,00,00,01,00,00,00,01,00,00,00,01,00,00,00,01,00,00,00,01,00,00,00};

Why have you changed that one to the following which does not contain the checksum item?

byte array[36] = {00, 0x24, 00, 0x00, 01, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00};

Sure, i didn't know how to do it before, sorry .

Because I suspect the OP did not understand what the original problem.

The checksum is stored in byte[3]... but also in post #5 they noted...

So I believe the checksum should be extracted from the data, and replaced with 0x00. The checksum calculated, and compared to the extracted value.

There is no way to calculate a checksum if the checksum is in the data as per post #6.

The OP should tell how the transmitter calculated the checksum? Is it "simple addition" of the information bytes (excluding the checksum byte) or "addition and then inverted" or "2's complement code"?

I have found that that the checksum is simple addition of the information bytes (excluding the checksum). Now, if all the received bytes are added (including the checksum), the result should be -1 to stand in favor of a valid message.