Home AMX User Forum NetLinx Studio

Checksum Fun (EDI TSC Lighting Controller)

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:
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!

Comments

  • Joe HebertJoe Hebert Posts: 2,159
    matt95gsr wrote:
    How can I get from 65427 to adding "$93,$FF" to my string?
    There are a few ways to accomplish this, here is one way:

    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
  • matt95gsrmatt95gsr Posts: 165
    Thanks a bunch, Joe. That's exactly what I needed. I completely forgot about the shift operators....
  • Other way:
    LSB = checksum % $FF // Modulo
    MSB = checksum / $FF
    
    outstring = "outstring,LSB,MSB"
    
  • matt95gsrmatt95gsr Posts: 165
    Other way:
    LSB = checksum % $FF // Modulo
    MSB = checksum / $FF
    
    outstring = "outstring,LSB,MSB"
    


    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?
  • matt95gsr wrote:
    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:)
  • Joe HebertJoe Hebert Posts: 2,159
    Half right

    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.
  • matt95gsrmatt95gsr Posts: 165
    not a problem...I'm just trying to get better at doing this kind of thing. Problem is, I just don't run into it very often.
  • Joe HebertJoe Hebert Posts: 2,159
    This thread is moving fast. I was in the middle of responding to Marc's first post and got sidetracked by a phone call. When I posted my response I was already two posts behind. :)
  • matt95gsrmatt95gsr Posts: 165
    and you jumped right in the middle of my last response to Marc. :) It is nice that there's been this much activity to help me out, though.
Sign In or Register to comment.