RS485 Checksum

Can anyone help with this?

I have the following hex string 40 50 4C 41 59 0D I am transmitting to a piece of equipment, I need to calculate a checksum value to add to the string as the last byte to be sent. The checksum byte is generated in such a manner that the result of addition of all bytes of a message will become “11111111”(255). the code that you see here does not work can anyone tell/show me why? the code is in visual basic only because I wrote a quick app to test the checksum calc. Whats important is the checksum calculation.

Thanks,

Dave.

Dim checksum
Dim checksumAdd

RS485comm.InBufferCount = 0
RS485comm.OutBufferCount = 0

Command_string(0) = &H9 'ADDRESS 00001001
Command_string(6) = &HD 'CHARACTER RETURN

'&H40 = @ Play_string(1)

checksumAdd = &H40 + Command_string(2) + Command_string(3) + Command_string(4) + Command_string(5) + Command_string(6)

checksum = (checksum + checksumAdd) And 255

Hello, Dave.

You posted this question in the Orangutan Robot Controllers section of the forum. Are you using an Oranugtan?

There are several problems with your code: You are not including Command_string(0) or Command_string(1) in your sum. The variable named “checksum” is not initialized before it is used. I’m not sure if Basic automatically initialized variables to zero or not, so that could cause you problems. If you want all the bytes to add up to 255, then you are going to have to do subtraction at some point. What we want is:

(Sum of all command bytes) + Checksum = 255

A little bit of algebra applied to the equation above tells us that:

Checksum = 255 - (Sum of all command bytes)

You could use that formula to compute the checksum and it would work fine, or you could remember that we are using 8-bit numbers, so:

Checksum = (Sum of all command bytes) with all bits toggled = (Sum of all command bytes) XOR 255

–David