python获取modbus_tk数据解析为十进制可读数据

a = '0103a0000d000d000d001f00e500e600e50000000000000001000100000000000efffa002900010002000900002321000011470000000100000000000c000e001ffee8fee800000000000000000000000000000000000000351fd60734dec80734dec80734dcf60000000000000000000000010000000000030004000400005b6a8fd200000000000000000000000000000000000000000000000000000000000000008fd2'

def analysis(x):
    list1 =[]
    if x:
        if x[:4] == '0103':
            x = x[6:]
        elif x[-4:].isdigit():
            pass
        else:
            x = x[:-4]
        for i in range(len(x)):
            if i%4==0:
                list1.append(int(x[i:i + 4],16))
                i+=4
        print(list1)
        return list1
    else:
        print('传入为空')
        return None
x = analysis(a)

结果为

[13, 13, 13, 31, 229, 230, 229, 0, 0, 0, 1, 1, 0, 0, 14, 65530, 41, 1, 2, 9, 0, 8993, 0, 4423, 0, 1, 0, 0, 12, 14, 31, 65256, 65256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 8150, 1844, 57032, 1844, 57032, 1844, 56566, 0, 0, 0, 0, 0, 1, 0, 0, 3, 4, 4, 0, 23402, 36818, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36818]

你可能感兴趣的:(python获取modbus_tk数据解析为十进制可读数据)