Chapter 4 ________________________________________________________________ Operation
VAISALA _______________________________________________________________________ 69
Checksum Calculation
The checksum of a data or a command telegram is calculated by
computing the sum of all signs (alphanumerical signs and control
codes such as STX, EOT CR, LF, except the checksum bytes itself),
build the two's complement and take the lower byte of this result.
The higher half-byte and the lower half-byte - converted to a visible
ASCII character - is the checksum.
See the following example with the
polling
command:
Telegram:
STX H0C!X1P----------83 EOT
1.
Compute the sum:
Sum = STX + 'H' + '0' + 'C' + !'' + 'X' + '1' + 'P'
+ 10 x '-' + EOT
Sum = 0x02 + 0x48 + 0x30 + 0x43 + 0x21 + 0x58
+ 0x31 + 0x50 + 10 x 0x2D + 0x04
Sum = 0x037D HEX = 893 DEZ
2.
Building the two's complement:
The two's complement is built by inverting the binary
representation of the sum and adding 1:
2Com =
¬
Sum + 1=
¬
0x01 = 0x0C83 HEX
= 3203 DEZ .
3.
Take lower byte and build ASCII-character:
The lower byte of 2Com is 0x83 HEX, so the high byte of the
checksum is 8 and the low byte is 3:
Checksum = 83