CRC校验

# CRC校验
def CRCconfirm():
string = con_entry1.get()
string = string.replace(' ', '')
data = bytearray.fromhex(string)
crc = 0xFFFF
for pos in data:
crc ^= pos
for i in range(8):
if ((crc & 1) != 0):
crc >>= 1
crc ^= 0xA001
else:
crc >>= 1
hexCRC = hex(((crc & 0xff) << 8) + (crc >> 8))
hexCRC = hexCRC.replace('x', 'x' + '0' * (6 - len(hexCRC))) if len(hexCRC) < 6 else hexCRC
# result.set(hexCRC[4:].upper() + ' ' + hexCRC[2:4].upper())
return [hexCRC[4:].upper(), hexCRC[2:4].upper()]
posted @ 2022-03-04 17:34  王大老爷  阅读(80)  评论(0编辑  收藏  举报