Home AMX User Forum NetLinx Studio

Basic Checksum Question

After three years of programming in AMX, I'm working with Checksum for the first time. It seems like a cumbersome ordeal. Anyway, I need someone to tell me if I've got the concept down.

If (for example) it wants me to turn the device ON with the command '$01,$01,CHECKSUM', would I send '$01,$01,$02'?

I know it's a simple question, but it's just one of those things I've never had to work with in any programming language I've worked with. Don't ask me how I avoided it. Thanks for your advice.

Comments

  • mpullinmpullin Posts: 949
    After three years of programming in AMX, I'm working with Checksum for the first time. It seems like a cumbersome ordeal. Anyway, I need someone to tell me if I've got the concept down.

    If (for example) it wants me to turn the device ON with the command '$01,$01,CHECKSUM', would I send '$01,$01,$02'?

    I know it's a simple question, but it's just one of those things I've never had to work with in any programming language I've worked with. Don't ask me how I avoided it. Thanks for your advice.
    Usually a protocol's documentation will tell you how the checksum is to be calculated (some even provide a sample C program that does it). In general, a checksum is found by adding up some or all of the bytes before the checksum and BANDing with $FF (that is, discarding the most significant bits if the sum is more than a byte.) Your query is awfully abstract - if you want more advice you should post what the protocol says regarding checksum.
  • I can understand how it wants me to calculate the checksum - that part is self-evident. But I'm unfamiliar with the syntax. I suppose it just wants another hexadecimal byte with the sum, but I've never seen it in code so I'm just unsure.
  • moty66moty66 Posts: 31
    DEFINE_VARIABLE
    char stringsToSend[]
    char stringWithChecksum = []
    integer counter
    integer checksum = 0
    DEFINE_START
    
    stringsToSend[1] = $01
    stringsToSend[2] = $F1
    //... etc
    FOR(counter = 1; counter <= LENGTH_STRING(stringsToSend); i++)
    {
        checksum = checksum + stringsToSend[i]
        stringWithChecksum = "stringWithChecksum, ITOA(stringsToSend[i]),','"
    }
    
    stringWithChecksum = "stringWithChecksum, ITOA(checksum)"
    
    send_string   myDevice, stringWithChecksum
    

    I wrote this without compiling it, this can be an idea that you can start from

    regards
    m.


    [/code]
  • Thanks, I think this will help. I really don't care for the Checksum process, but I'm sure there's a good reason for it. Since most devices don't use it, I'm not sure why I'm bothering. Thanks, again!
  • moty66moty66 Posts: 31
    Thanks, I think this will help. I really don't care for the Checksum process, but I'm sure there's a good reason for it. Since most devices don't use it, I'm not sure why I'm bothering. Thanks, again!


    With Checksum you are sure that the messages which you receive from the device are correct and complete, and the same when you send to the device, so they are important !
  • moty66 wrote: »
    With Checksum you are sure that the messages which you receive from the device are correct and complete, and the same when you send to the device, so they are important !

    That's what I've read, but I've never needed it for anything else. I suppose the other devices I've worked with didn't have as much potential for problems with that sort of thing.
  • jweatherjweather Posts: 320
    I can understand how it wants me to calculate the checksum - that part is self-evident. But I'm unfamiliar with the syntax. I suppose it just wants another hexadecimal byte with the sum, but I've never seen it in code so I'm just unsure.

    It still depends on the device's protocol, some of them put it in as another byte in the command, some ASCII-fy it and split it into high and low nibbles across two bytes, and some require escaping it if the checksum comes out to certain values! There is no standard for calculating or including checksums, that's why we're asking for the protocol manual in order to answer your question.
  • moty66moty66 Posts: 31
    I did the BatiBus checksum in this way

    Message from the device : $40$07$03$03$4E$02$FA$A2

    Last to bytes are the check sum $FA$A2

    So I had to BXOR the data $40$07$03$03$4E$02 -> $BF$F8$FC$FC$B1$FD
    and then I sum the data -> $05$5D
    then I BXOR the sum $05$5D->$FA$A2
  • TurnipTruckTurnipTruck Posts: 1,485
    Thanks, I think this will help. I really don't care for the Checksum process, but I'm sure there's a good reason for it. Since most devices don't use it, I'm not sure why I'm bothering. Thanks, again!

    With two communicating devices that are connected by a short serial cable, I would debate the necessity of a checksum.

    However, with communication paths such as RF or powerline carriers, checksums are valuable.
Sign In or Register to comment.