I am a programmer and I need to send data to the machine, but I have to add checksum at the end of the data. Can someone show me an example how to do it so I could then write an program to calculate the checksum for me.
The rules: Bitwise inversion of the 1 byte sum of bytes beginning with the most significant address byte and ending with the byte preceding the checksum. (To perform a bitwise inversion, "exclusive OR" the one byte sum with FF hex.)
Thanks
Answer
The definition pretty well covers it.
Bitwise inversion of the 1 byte sum of bytes beginning with the most significant address byte and ending with the byte preceding the checksum. (To perform a bitwise inversion, "exclusive OR" the one byte sum with FF hex.)
Example
- Message "Hello" = Hex: 48 65 6C 6C 6F.
- Adding these up using my Windows Calc.exe in Programmer mode I get &h01F4.
- Take the last byte and ignore everything else: &hF4.
- &hF4 XOR &hFF = &h0B - your checksum.
To clarify the last bit:
&hF4 = 1111 0100
&hFF = 1111 1111
---------
XOR = 0000 1011 = &h0B
Update
For some reason, it does not work for me. Here I have CA 00 01 70 00, I add them up to get 13B, I take the last byte which is 3B and XOR with FF, then I get c4, which is different from 8E(an example I got from the manual). Do I miss something about the rule?
A web search for the rule threw up a document from NESLAB. On page B-2 we read:
Figure 1. The checksum region is the only part used in the checksum.
This should explain your confusion. The lead character is not included in the calculation. Now if we run a check on CA 00 01 70 00 we can drop CA and are left with a few zeros, 01 and 70 which sums to &h71.
&h71 XOR &FF = &h8E. Voila!
No comments:
Post a Comment