异常响应(131、3、非法地址)

Exception Responce (131, 3, IllegalAddress)

我知道这可能看起来像 duplicate question but the answers provided didn't solve the issue I am having. I am writing a program to read in analog channels from an ADAM 6017 using pymodbus。首先,我只是要求第一个保持寄存器 40000 或者因为我有它寄存器 0,我可以使用 Simply Modbus 获得读数但是当我 运行 我在 Python 中的代码时,我得到的只是异常响应(131、3、非法地址)

from pymodbus.client.sync import ModbusTcpClient as ModbusClient

client = ModbusClient("192.168.1.201", port=502, auto_open=True)

rr = client.read_holding_registers(0, 1, unit=0x00)
raw_value = client.read_holding_registers(0, 1, unit=0x00)
rr_response = client.execute(rr)
raw_value_response = client.execute(raw_value)
print(raw_value_response)
print (rr_response)

这是我使用虚拟 Modbus 服务器 运行 你的代码时得到的结果:

DEBUG:pymodbus.server.sync:Client Disconnected [127.0.0.1:33075]
DEBUG:pymodbus.server.sync:Started thread to serve client at ('127.0.0.1', 49439)
DEBUG:pymodbus.server.sync:Client Connected [127.0.0.1:49439]
DEBUG:pymodbus.server.sync:Handling data: 0x0 0x4 0x0 0x0 0x0 0x5 0x0 0x3 0x2 0x0 0xff
DEBUG:pymodbus.framer.socket_framer:Processing: 0x0 0x4 0x0 0x0 0x0 0x5 0x0 0x3 0x2 0x0 0xff
DEBUG:pymodbus.factory:Factory Request[ReadHoldingRegistersRequest: 3]
ERROR:pymodbus.server.sync:Socket exception occurred Traceback (most recent call /register_read_message.py", line 40, in decode
    self.address, self.count = struct.unpack('>HH', data)
error: unpack requires a string argument of length 4

DEBUG:pymodbus.server.sync:Client Disconnected [127.0.0.1:49439]

所以你那里有问题。为了让它工作,我只是将其更改为:

from pymodbus.client.sync import ModbusTcpClient as ModbusClient

client = ModbusClient("192.168.1.201", port=502, auto_open=True)
client.connect()
rr = client.read_holding_registers(0, 1, unit=0x00)

print (rr.registers)

并且我得到了一个包含 1 个元素的列表,其中包含我服务器上寄存器的正确值。

您可以查看pymodbus的同步客户端示例以获取更多详细信息:https://pymodbus.readthedocs.io/en/v1.3.2/examples/synchronous-client.html