You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pymodbus读取保持寄存器失败,求排查与调试方法

用pymodbus连接台式电源时Modbus RTU通信失败排查

我正尝试用pymodbus与一款廉价台式电源建立通信,运行初始脚本以识别其功能。原厂已提供包含寄存器地址的Modbus文档。但运行以下Python脚本时,始终无法读取寄存器,所有读取操作均返回IO类错误。我是Modbus新手,想请教问题出在哪里,以及如何更好地调试?

运行的Python代码

import time
from pymodbus.client import ModbusSerialClient
from pymodbus.exceptions import ModbusIOException

def run():
        print("Creating client")
        client = ModbusSerialClient("/dev/ttyUSB0", method='rtu', baudrate=9600, stopbits=1, parity='N', bytesize=8, timeout=1)

        print("Connecting to client")
        if not client.connect():
                print("Failed to connect to client")
                return

        # List of register addresses to read from
        register_addresses = [0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0010, 0x0011, 0x0012, 0x0013, 0x0030, 0x0031, 0x0020, 0x0021, 0x0022, 0x0023, 0x9999]

        # Read from each register
        for address in register_addresses:
                try:
                        response = client.read_holding_registers(address, 1)
                        if response.isError():
                                print(f"Error reading register {address}: {response}")
                        else:
                                print(f"Register {address}: {response.registers}")
                        time.sleep(0.035)
                except ModbusIOException as e:
                        print(f"Modbus IO Exception reading register {address}: {e}")

        print("Closing client")
        client.close()

run()

代码运行输出

└─$ python3 modbus4.py
Creating client
Connecting to client
Error reading register 1: Modbus Error: [Input/Output] Modbus Error: [Invalid Message] No response received, expected at least 4 bytes (0 received)
Error reading register 2: Modbus Error: [Input/Output] No Response received from the remote slave/Unable to decode response
...
Error reading register 39321: Modbus Error: [Input/Output] No Response received from the remote slave/Unable to decode response

问题排查与调试建议

核心问题点

  1. 未指定从站地址(Slave ID)
    Modbus RTU通信必须明确目标设备的从站ID,你的脚本完全没设置这个参数,pymodbus默认值可能和设备实际ID不匹配。修改read_holding_registers调用,加上slave参数,比如:

    response = client.read_holding_registers(address, 1, slave=1)
    

    常见的从站ID是1,可查看设备文档确认具体数值。

  2. 寄存器地址偏移问题
    很多Modbus文档用1-based地址标注寄存器,但pymodbus默认采用0-based偏移量。比如文档里的0x0001,实际要读的地址是0x0000,尝试把所有寄存器地址减1后再读取。

  3. 串口参数不匹配
    确认设备实际的串口参数(波特率、奇偶校验、停止位)是否和脚本一致。廉价设备常出现文档参数错误的情况,比如波特率可能是19200而非9600,或者奇偶校验为偶校验('E')。

硬件与连接检查

  • 确认USB转串口线正常工作,用dmesg | grep ttyUSB命令检查系统是否识别设备。
  • 接线必须正确:TX(电脑)接设备RX,RX(电脑)接设备TX,GND共地,严禁接设备VCC引脚,避免烧坏设备。
  • 检查设备是否切换到Modbus RTU模式,部分设备需要通过面板按键或菜单开启通信功能。

调试工具辅助

  • 用minicom或screen工具直接连接串口,手动发送Modbus RTU帧测试响应。比如发送读取从站1、寄存器0x0000的帧(十六进制:01 03 00 00 00 01 84 0A),看是否有返回。
  • 用串口抓包工具(如Wireshark配合USB串口抓包插件),查看发送的帧是否正确,以及设备是否有返回数据。

脚本优化建议

  • 延长超时时间:把timeout=1改成timeout=3,给设备足够的响应时间。
  • 先测试单个寄存器:不要一次性读多个,先挑文档里明确标注的有效寄存器(比如电压电流读取寄存器)单独测试,排除无效地址的干扰。

内容的提问来源于stack exchange,提问作者joeyda3rd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 22:15:34