modbus.parse_tcp
modbus.parse_tcp
功能
解析 Modbus TCP 响应帧,校验 MBAP length 并提取常见响应字段。
语法
modbus.parse_tcp(data)
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| data | Bytes/Array/String | 是 | 无 | TCP 响应帧。 |
返回值
| 类型 | 说明 |
|---|---|
| Object | 包含 protocol、transaction_id、protocol_id、length、valid_length、unit_id、function_code、exception、data 等字段。读取寄存器响应会额外返回 registers。 |
代码示例
frame = bytes([0, 1, 0, 0, 0, 7, 1, 3, 4, 0, 10, 0, 20]) result = modbus.parse_tcp(frame) // 输出:20 print result.registers[1]
注意事项
-
valid_length表示输入长度是否和 MBAP length 精确一致;输入更长时仍按 MBAP length 解析当前帧。