Home AMX User Forum NetLinx Studio

Checksum Question

I haven't had to calculate a checksum since my Programmer classes because none of the devices I use ever need it, and now some of the syntax is eluding me.

I need to set up a loop and have it add each byte as the loop progresses. I can get started, but I can't remember how to capture each byte individually and then move to the next. If anyone can push me in the right direction with a simple example it would be great.

Comments

  • Spire_JeffSpire_Jeff Posts: 1,917
    define_function integer getChecksum(char cmd[]){
    	stack_var integer bTotal;
    	stack_var integer nLength;
            stack_var integer nPosition;
    	
    	nLength = LENGTH_STRING(cmd);
            nPosition = 1;
            while(nPosition <= nLength){
                    bTotal = bTotal + cmd[nPosition];
                    nPosition++;
            }	
    	bTotal = bTotal & $FF;
    
    	return bTotal;
    }
    
    I think this will get you pretty close.

    Jeff
  • That helps me with the general syntax - thanks - but, I still don't understand how that adds it up correctly. Wouldn't it just add up every number in the string, as opposed to taking each pair of hex characters and adding them?
  • viningvining Posts: 4,368
    Spire_Jeff wrote: »
    define_function integer getChecksum(char cmd[]){
    	stack_var integer bTotal;
    	stack_var integer nLength;
            stack_var integer nPosition;
    	
    	nLength = LENGTH_STRING(cmd);
            nPosition = 1;
            while(nPosition <= nLength){
                    bTotal = bTotal + cmd[nPosition];
            }	
    	bTotal = bTotal & $FF;
    
    	return bTotal;
    }
    
    I think this will get you pretty close.

    Jeff
    Don't forget
    nPosition++;
    Otherwise you won't increment your addition and you'll never escape your "while"
  • Spire_JeffSpire_Jeff Posts: 1,917
    vining wrote: »
    Don't forget
    nPosition++;
    Otherwise you won't increment your addition and you'll never escape your "while"

    Yes, I forgot that... I was sort of writing it on the fly while working on other code :) Good catch.
    That helps me with the general syntax - thanks - but, I still don't understand how that adds it up correctly. Wouldn't it just add up every number in the string, as opposed to taking each pair of hex characters and adding them?

    The hex characters are just a human representation of the data. The computer doesn't understand "$FF" any different than "255" because it is all just binary 11111111. The hex characters are often used because they break down into bytes nicely. Basically, the F is 15 decimal and 1111 in binary. If you ever do work with bitwise operations, or assembly language code, there is a lot of thinking in binary and hex makes it easy to write and convert (at least in my mind :) ). Before I go off any further on a tangent, I will try to sum this up.

    I think the concept you are missing is that each pair of hex characters translates into a single CHAR. CHARs are "An intrinsic data type representing an 8-bit unsigned integer. " If a single hex character requires 4 bits, 2 hex characters will use 8 bits. Another way to look at it is using currency. I could give you a ten dollar bill, I could give you 10 one dollar bills, or I could give you 1000 pennies. They are all worth the same.

    Last attempt, because I am not sure that I am making sense (even to myself right now :) ), would be to use a calculator. If you have a calculator that handles hex and can also convert to decimal, start by converting the hex codes to decimal values. Add up all of the decimal values, then convert to hex. Then, add up all of the hex values. it should be the same.

    Hopefully one of my explanations helps you on the path to understanding.

    Jeff
  • So, you're saying that it's not to be parsed like an actual hex string, but to assume that each command is coming in one CHAR at a time, and then to simply get one CHAR at a time and add it to the next. That certainly simplifies things.
  • Spire_JeffSpire_Jeff Posts: 1,917
    Sounds like you are on the right track... unless the unit is sending you hex codes in ASCII format. If that is the case, then you will most likely need to grab 2 chars at a time and use HEXTOI() before adding them together. Do you have a protocol document you can share? Or maybe a little bit of a serial port capture of data from the device?

    Jeff
  • It's not doing any of that stuff. The problem is really my understanding of the syntax here - I work in an environment where I don't have to utilize at least 70% of what Netlinx can do, and I forget things. The part of this that I would not have ever remembered is using (char cmd[]) and using the number in the brackets to isolate each byte (assuming I have that right). That's not the sort of thing that comes along when you program button_events all day long!
Sign In or Register to comment.