
5.2
Data check algorithm
Each character has 8 bits (bit7~bit0), each bit can be 0 or 1. The check bits A6,A5,A4 store the low 3
bit of sum of 1 of B,C,D 3 bytes, when check algorithm, first get the number of ‘1’in the 3 bytes of
B,C,D by looking up the table and then add them up, and then compare the lower 3 bits of the sum with
A6,A5,A4, if they are consistent, output the measurement results
unsigned char GetCrcPackage4Byte (unsigned char *buf)
{.
unsigned char B,C,D;
B = buf[1];
C = buf[2];
D = buf[3];
//cbit is a number table of 1corresponding to 0-255
static unsigned char cbit[256] = {
0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
};
return (cbit[B]+cbit[C]+cbit[D])&0x07; } return the low 3 digits the sum of 1 in the 3 bytes of
B,C,.D
5.3 Data analysis algorithm
//buf is a data package pointer, which stores 3 bytes as A, B, C in order.
//return distance value, if data check is not correct, return -1
//
int DecodeLaseData(unsigned char *buf)
{
int distance;
unsigned char crcdata = GetCrcPackage3Byte(buf);