Simple 1 Byte Checksum Method
Hello,
I need to encode/decode a simple 8Bit Checksum based on a transmitter which has no documentation.
Apparently, it is just using the last 2 Bytes of a 10 Byte packet to calculate it.
(Figured THAT out by dumb luck.)
Using a Logic Analyzer, it appears it is the simple sum of these two bytes.
Payload[8] + Payload[9] = CheckSum
5 + 2 = 7
245 + 2 = 247
53 + 1 = 54
54 + 1 = 55
21 + 2 = 23
etc, etc,
I have not been able to capture what happens over 255 but assume it must roll-over somehow?
Browsing all the "CheckSum" threads here, I think the code may be something like the following:
----------------------------------
Payload VAR BYTE[11]
(Payload elements...)
CheckWord VAR WORD
CheckSum VAR BYTE
CheckWord = Payload[8] + Payload[9]
CheckSum = CheckWord.byte0 ^ $FF '<--- This?
'CheckSum = CheckWord // 256 '<--- Or That?
-----------------------------------
Any ideas?
Thanks,
Re: Simple 1 Byte Checksum Method
would it not be easier to
if Payload[10] -( Payload[8] + Payload[9]) > 0 then goto badckecksum
or conversely
Payload[10] =Payload[8] + Payload[9] to create chksum
Re: Simple 1 Byte Checksum Method
Right. That's simple.
Problem is when they add up greater than 256 becoming a WORD.
Re: Simple 1 Byte Checksum Method
it can't become a word if the var is declare as a byte it just rolls over .
Payload var byte[11]
btw the transmitter its not a fineoffset weatherstation ?
Re: Simple 1 Byte Checksum Method
It can't be that simple?
What if there were more than 2 Bytes like 10?
No it's not a weatherstation, more like a entry door counter.
Re: Simple 1 Byte Checksum Method
a byte can hold a value between 0 and 255
255 + 1=0
255+10=9
255+255+255+1= 254
Re: Simple 1 Byte Checksum Method
Richard,
Wow!
That is so clean and easy.
(I do have a tendency of making things complicated.)
Thank you very much!
Jay Zebryk
Southbridge, Massachusetts
Re: Simple 1 Byte Checksum Method
This information may not pertain to your current goal since you are trying to decode an existing checksum method being used by your transmitter.
However, for a simple tried and true method of byte level checksums on a datastream, the Intel Hex8 checksum method works well and is very simple.
I use this method in many projects with good success.
The Intel Hex8 file (I8HEX) checksum is the 2s compliment of the sum of the payload bytes.
The beauty of using the 2s compliment of the sum of the payload bytes is that the validation check is simple.
To validate the payload at the receiving end, the receiver sums the received payload bytes including the received checksum byte.
The result should equal zero (0).
This works because of the use of unsigned byte variables and the rollover from 255 to 0 for overflows.
In this context a number plus the 2s compliment of itself will equal zero (0) as a 2s compliment of a number behaves like the negative of the original number.
Computing the 2s compliment of a byte can be done by first computing the 1s compliment of the number, then adding one to the result.
1scomp = (number ^ $FF) This computes the 1s compliment
2scomp = 1scomp + 1
For example:
number = 73
1scomp = (73 ^ $ff) = 182
2scomp = 182 + 1 = 183
To validate just add the number to its 2s compliment.
73 + 183 = 0
This checksum method can be used for just about any length of payload bytes.