Link to home
Start Free TrialLog in
Avatar of Roger Alcindor
Roger Alcindor

asked on

LIN bus classic checksum calculation

I have coded the calculation of the classic checksum of 8 bytes of data 0x01,0x06,0xB2,0x22,0x31,0x00,0x53,0x4F which should result in 0x50 as the result. My code yields an answer of 0x51 however, where am I going wrong ?

      unsigned char a[] = {0x01,0x06,0xB2,0x22,0x31,0x00,0x53,0x4F};
      unsigned char cksum = 0;

      for(int i=0;i<8;i++)
            cksum+=a[i];
      cksum = ~cksum;
Avatar of Roger Alcindor
Roger Alcindor

ASKER

Solved the issue myself
Avatar of phoffric
Hi,
Glad you solved the problem.
In order for you post to be the accepted answer, there has to be information in it to help others understand the solution.
Please post the solution to help others.
Regards,
Phoffric
Topic advisor
The error you had is where you provide code that has [ i]. If you paste your code within the code tags (that is by hitting the code button) then you will not have an error . The [ i] represents a marker that will italicize your words. But then to complete the italization, you need a closing tag, [/i]. These special markers are ignored when you put your code within code tags .

If you could take a moment to paste your code within the code tags by first hitting the code button (you'll see the code tags set up for you) , then your code will be properly displayed . Another advantage in presenting your code within code tags is that the lines are numbered, which enables experts to help you more clearly by referring to specific line numbers.
fyi: if you want to post i within [] but not using a code block, you may add spaces left (and right) like a[ i ].

Sara
ASKER CERTIFIED SOLUTION
Avatar of Roger Alcindor
Roger Alcindor

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
     if(cksum >=256)
                  cksum -=255;

i could verify that LIN Bus "classic checksum" was built by using the the above correction.

but i couldn't find that the final result gets the bits inverted as you did by applying the ~ operator. actually this also was equivalent to subtracting 255 from the cksum (for example if cksum was 0 after loop it finally is hex 0xff or signed char -1 or unsigned char 255).

can you post a reference for this?

Sara
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
New code posted by author.