CRC
A cyclic redundancy check (CRC) is an error-detecting code.
http://en.wikipedia.org/wiki/Cyclic_redundancy_check
http://en.wikipedia.org/wiki/Computation_of_cyclic_redundancy_checks
http://www.drdobbs.com/implementing-the-ccitt-cyclical-redundan/199904926
http://www.ietf.org/rfc/rfc1952.txt
http://www.feng.pucrs.br/~stemmer/processadores2/trab2-2012-2/crc.html
HDLC like framing
The http://www.ietf.org/rfc/rfc1662.txt has two examples of CRC calculations.
Polynom CRC-16: 0x8408 ( CRC-16-CCITT Reversed )
Polynom: x0 + x5 + x12 + x16
CRC-16-IBM
Bisync, Modbus, USB, ANSI X3.28, SIA DC-07, many others; also known as CRC-16 and CRC-16-ANSI
Polynom: x16 + x15 + x2 + 1
Normal |
Reversed |
Reversed reciprocal |
0x8005 |
0xA001 |
0xC002 |
1 def crc16ANSI(self,message):
2 crc = 0x0000
3 bitOne=0x0001
4 crcpoly=0xA001
5 idx = 0
6 while idx < len(message):
7 origByte = ord(message[idx])
8 bitCounter = 0
9
10 while bitCounter < 8:
11 crcFirstBit=(crc & bitOne)
12 origByteFirstBit=(origByte & bitOne)
13 crcShiftRight=crc >> 1
14 #check if first bit of crc is different than the first of the original byte
15 if(crcFirstBit ^ origByteFirstBit== 1):
16 crc = crcShiftRight ^ crcpoly
17 else:
18 crc = crcShiftRight
19
20 bitCounter = bitCounter + 1
21 origByte = origByte >> 1
22
23 idx = idx + 1
24
25 res=crc & (0xFFFF)
26 return res
27 '''
28 'A' 0x30C0
29 'ABC' 0x4521
30 'THE,QUICK,BROWN,FOX,0123456789' 0xB96E
31 '''