Files
ailai/EMV/code_statue_test.py

20 lines
755 B
Python
Raw Normal View History

2025-08-18 16:54:29 +08:00
def parse_coil_status_response(response_hex):
"""
解析 Modbus 功能码 0x01 的响应读线圈状态
:param response_hex: 十六进制字符串 '00000000000401010101'
:return: dict表示每个 bit 的状态True/False
"""
if len(response_hex) < 18:
return {"error": "响应数据过短"}
data_part = response_hex[14:18] # 提取 0101 部分
status_byte = int(data_part[2:4], 16) # 取出 01 → 十进制 1
# 每个 bit 对应一个设备bit0 是最低位
status = {f"device_{i}": (status_byte >> i) & 0x01 == 1 for i in range(8)}
return status
# 示例使用
response = "00000000000401010103"
status = parse_coil_status_response(response)
print("设备状态:", status)