我知道这看起来像是duplicate question,但是提供的答案并不能解决我遇到的问题。我正在编写一个程序,以使用ADAM 6017从pymodbus读取模拟通道。首先,我只是要求第一个保持寄存器40000或因为它拥有寄存器0,所以我可以使用Simply Modbus读取数据,但是当我在Python中运行代码时,我得到的只是异常响应(131,3,IllegalAddress)
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)
答案 0 :(得分:2)
这是我用虚拟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