Checksum Fun (EDI TSC Lighting Controller)
matt95gsr
Posts: 165
Folks, I haven't had to work a whole lot with calculating checksums other than the ones that I've seen examples of floating around, and I've come across one that I'm just now getting at the moment. The example of the checksum from the manufacturer is below:
Now, I can actually come up with a checksum that equals 65427, or 0xFF93h, but I need to send the LSB first, and just can't seem to do that right. How can I get from 65427 to adding "$93,$FF" to my string? Like I said, I haven't done very much of this, so if anyone feels like showing me what would be a good way to do the calculating I'd appreciate that as well. Thanks!
The checksum equals 0x10000h minus the sum of ALL the data bytes not including the checksum. For example the checksum for the serial signal shown above would be calculated by: 0x10000h - (0x10h + 0x01h + 0x0ch + 0x00h + 0x01h + 0x010h + 0x3bh + 0x00h + 0x00h + 0x04h) = 0xFF93h |________________________________________________________| This is always the same Room1 Preset5 Checksum
Now, I can actually come up with a checksum that equals 65427, or 0xFF93h, but I need to send the LSB first, and just can't seem to do that right. How can I get from 65427 to adding "$93,$FF" to my string? Like I said, I haven't done very much of this, so if anyone feels like showing me what would be a good way to do the calculating I'd appreciate that as well. Thanks!
0
Comments
To get the LSB of your 2 byte checksum Bitwise AND the checksum with $FF. This will lop off the upper byte and leave the lower byte in tact.
LSB = checksum BAND $FF
To get the MSB of your 2 byte checksum you can shift to the right 8 times. This will shift the upper byte to the lower byte.
MSB = checksum >> 8
Then append LSB and MSB to your string:
outstring = ?outstring,LSB,MSB?
HTH
Thanks, Marc. However, when I try checksum/$FF it's not coming out right. As far as I can tell $FF93 / $FF evaluates to $100 instead of $FF. What if I divide by $100...that should provide the right answer, right?
:eek: you are right, sorry (normally I write ...%256 and .../256... When I wrote the codeblock, I still thought "well, this looks some kind of wrong" :rolleyes:)
If you want to use the Modulo operator (%) instead of the Bitwise AND operator (&) then you?ll have to Mod by $100
LSB = checksum & $FF // using bitwise AND
produces the same result as:
LSB = checksum % $100 //using Modulo
MSB = checksum >> 8
does indeed produce the same result as:
LSB = checksum / $FF
It?s all a matter of how you want to look at it.