Electronic – Calculate sum check

rs232serial

I'm trying to send some serial commands.

In the manual of the hardware it says:

Sum check byte is the summation of all bytes. Higher order bytes are ignored and and the final result is modified. Bit 7 (highest) is forced high.

Let's say the serial command is: (where 0x30 = 30)

30 30 70 7E 61 62 63 7E 30

… what would be the checksum and how can I calculate it?

What are "higher order bytes"? What is a "modified" final result? What does "is forced high" mean?

Best Answer

You should include a link or picture of the manual page or so to be sure.

I could hazard a guess, though, and you could see if it works for you.

Start with an empty 8 bit variable.

For each byte in the command, add the byte to the variable.

Since the sum may end up being larger than 8 bit, the 'correct' sum would include a 'high byte' also. However, using an 8-bit variable will cause the sum to rollover. You can also use a 16-bit variable and mask it with 0x00FF or a 32 bit variable and mask it with 0x000000FF. In most cases, apply the mask after adding all the bytes.

Finally, force the highest bit (bit 7) to 1.

For your example, the checksum should be calculated as:

30 + 30 + 70 + 7E + 61 + 62 + 63 + 7E + 30 = 322

Strip out the high byte :

22

Force the MSB to 1:

A2