USB & RS232 Communication
Data Link Layer
EPOS4 Communication Guide
2-14
CCMC | 2019-11 | rel8759
2.2.3
Cyclic Redundancy Check (CRC)
CRC is used for verification of data integrity.
2.2.3.1
CRC Calculation
Note
• The 16-bit CRC checksum uses the algorithm CRC-CCITT.
• For calculation, the 16-bit generator polynomial “x
16
+x
12
+x
5
+x
0
” is used.
• The CRC is calculated before data stuffing and synchronization.
• Add a CRC value of “0” (zero) for CRC calculation.
• The data frame bytes must be calculated as a word.
2.2.3.2
CRC Algorithm
Figure 2-6
USB/RS232 communication – CRC algorithm
ArrayLength: Len + 2
WORD DataArray[ArrayLength]
Generator Polynom G(x):
10001000000100001 (= x
16
+x
12
+x
5
+x
0
)
DataArray[0]:
DataArray[1]:
DataArray[2]:
…
DataArray[ArrayLength-1]:
HighByte(Len) + LowByte(
OpCode
)
Data[0]
Data[1]
…
0x0000 (CrcValue)
WORD CalcFieldCRC(WORD* pDataArray, WORD ArrayLength)
{
WORD shifter, c;
WORD carry;
WORD CRC = 0;
//Calculate pDataArray Word by Word
while(ArrayLength
−−
)
{
shifter = 0x8000;
c = *pDa+;
do
{
//Initialize BitX to Bit15
//Copy next DataWord to c
carry = CRC & 0x8000;
CRC <<= 1;
if(c & shifter) CRC++;
if(carry) CRC ^= 0x1021;
shifter >>= 1;
//Check if Bit15 of CRC is set
//CRC = CRC * 2
//CRC = CRC + 1, if BitX is set in c
//CRC = CRC XOR G(x), if carry is true
//Set BitX to next lower Bit, shifter = shifter/2
} while(shifter);
}
return CRC
}