2018-09-09
Author: 楚格
IDE: Pycharm2018.02 Python 3.7 第三方库 crcmod
KeyWord : CRC modbus cauCRC 和check
Explain:
1CRC原理
2CRC知识
3CRC代码
1------------------------------------------------------------------------------------------------------------------
2------------------------------------------------------------------------------------------------------------------
参考网址
--
>>>
>>> CRC(循环冗余校验)在线计算
--
>>> < http://crcmod.sourceforge.net/crcmod.predefined.html#predefined-crc-algorithms >
>>> crcmod.predefined – CRC calculation using predefined algorithms
--
3------------------------------------------------------------------------------------------------------------------
Code:
CRC校验二种方法:
1使用第三方库函数crcmod,但是不够完善,缺少输入输入数据反转处理。
2使用计算法,利用CalCRC16和CheckCRC函数校验输入的byte。先calcuation 后check。
--
__all__ = ["CalCRC16","CheckCRC"] # =============================================================== import crcmod # =============================================================== def CalCRC16(data, length): print(data, length) crc=0xFFFF if length == 0: length = 1 ## for j in data: ## crc ^= j j = 0 while length != 0: crc ^= list.__getitem__(data, j) #print('j=0x%02x, length=0x%02x, crc=0x%04x' %(j,length,crc)) for i in range(0,8): if crc & 1: crc >>= 1 crc ^= 0xA001 else: crc >>= 1 length -= 1 j += 1 ##if length == 0: ## break return crc # =============================================================== def CheckCRC(data, length, crctype): if length < 3: print('The data len(%d) is less than 3!!!', length) return 0 crc_res = 0 tmp=[0,0,0,0] if crctype == 0: crc_res = CalCRC16(data, length-2) tmp[0] = crc_res & 0xFF tmp[1] = (crc_res >> 8) & 0xFF if data[length-2] == tmp[0] and data[length-1] == tmp[1]: return 1 elif crctype == 1: print('CRC32 is not support...') return 0 # =============================================================== # =============================================================== ''' #=============================================================== # 测试专用 #=============================================================== ''' if __name__ == '__main__': ## Name Identifier-name, Poly Reverse Init-value XOR-out Check ## ['modbus','CrcModbus',0x18005,REVERSE,0xFFFF,0x0000,0x4B37] ## crc16 = crcmod.mkCrcFun(0x18005, rev=True, initCrc=0xFFFF, xorOut=0x0000) # rev=True,False crc16 = crcmod.mkCrcFun(0x18005, initCrc=0xFFFF,rev=True, xorOut=0x0000) # rev=True,False crc_array = b'0xFE 0xFD' crc_calc = crc16(crc_array) #计算得到的CRC a=hex(crc_calc) print(crc_calc,a) print('\n') # ========================================= crc_value = [0x01, 0x04, 0x13, 0x87, 0x00, 0x30] crc_transformation = CalCRC16(crc_value,len(crc_value)) crc_calculation = hex(crc_transformation) # print('crc_calculation:',crc_calculation) tasd = [0x00,0x00] tasd[0] = crc_transformation & 0xFF tasd[1] = (crc_transformation >> 8) & 0xFF H =hex(tasd[0]) L =hex(tasd[1]) H_value = int(H,16) L_value = int(L,16) crc_value.append(H_value) crc_value.append(L_value) print(crc_value) # calculation value CRC # ======================================================== print('\n') # crc_value2 = [0x01, 0x04, 0x13, 0x87, 0x00, 0x30,0x44,0xB3] # print('crc_value2:',crc_value2) # crc_cheak=CheckCRC(crc_value2,len(crc_value2),0) crc_check=CheckCRC(crc_value,len(crc_value),0) if crc_check == 1: print('Right') else: print('wrong') print(crc_check) # check calculation value
--
Run Result:
--
44877 0xaf4d
[1, 4, 19, 135, 0, 48] 6
[1, 4, 19, 135, 0, 48, 68, 179]
[1, 4, 19, 135, 0, 48, 68, 179] 6
Right
1
Process finished with exit code 0
--
4------------------------------------------------------------------------------------------------------------------
5------------------------------------------------------------------------------------------------------------------
6------------------------------------------------------------------------------------------------------------------
7------------------------------------------------------------------------------------------------------------------
8------------------------------------------------------------------------------------------------------------------
9------------------------------------------------------------------------------------------------------------------
10------------------------------------------------------------------------------------------------------------------