Python ascii十六进制转换

时间:2017-03-12 23:36:30

标签: python hex ascii extended-ascii

我有一个用python3编写的脚本,它从USB连接设备读取数据。

设备的输出读取以下内容。

(240.0 08.3 240.0 50.0 08.3 2000 2000 24.0 25.0 21.0 29.2 27.0 1 10 030 0 2 1 1 01 0 0 25.0 0 1ÖZ

我需要用crc验证这些数据。 crc验证是,所有数据的crc都要求最后2个字节应与最后两个字节匹配。

示例:crc(data[0:-2]) == data[-2:]

我有以下代码。

def validateResult(result):
    firstPart = result[0:-2].encode('utf-8')
    lastPart = result[-2:].encode('utf-8')
    crc = crc16.crc16xmodem(firstPart).to_bytes(2, 'big')
    print (result)                     # pints the input
    print (binascii.hexlify(crc))      # prints b'd65a' -> d6 is not in ascii table. While its in extended ascii
    print (binascii.hexlify(lastPart)) #prints b'c3965a'
    if(crc == lastPart):
        return True
    print ("CRC failure")
    return False

读取数据的代码:

res += "".join([chr(i) for i in dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, timeout) if i != 0x00])

如何更正此方法以便正确检查CRC?

0 个答案:

没有答案
相关问题