U-Camera User Manual - Communication Protocol
5.3
5.3
Checksum Calculation
Checksum Calculation
As described in the previous section, the UCSP packets have two checksum bytes to
determine the integrity of the packet.
This checksum is the 8-bit Fletcher algorithm, which is used in the TCP standard (RFC
1145).
Checksum A (CK_A) and Checksum B (CK_B) must be initialized to zero (0x00).
For each byte of the payload the byte is added to CK_A and then CK_B is the result of
adding the previous CK_B value to CK_A.
When adding to CK_A and CK_B, values overflowing the 8 bits should be trimmed to 8 bit
with a 0xFF mask if the data type is wider than 8 bits.
The checksum involves all the packet bytes except for the checksum bytes themselves.
The packet is valid if the calculated CK_A and CK_B values are identical to the last two
bytes of the packet.
A pseudo-code for checksum calculation is provided in the following figure:
For testing purposes, the following test packet is provided:
13
0
1
2
3
4
5
6
7
8
9
10
11
12
CK_A
CK_B
0xCC
0x01
0x02
0x09
0x01
0x02
0x03
0x04
0x05
0x06
0x07
0x08
0x09
0xFC
0x74
Figure 5:Example Packet
Figure 4: Pseudo-code for checksum calculation
unsigned char CK_A, CK_B
CK_A = 0,CK_B = 0
For ( I = 0; I<N; I++)
{
CK_A = CK_A + Buffer[I]
CK_B = CK_B + CK_A
}
Pseudo-code for check sum calculation