谁能解释使用pymodbus通过Modbus TCP / IP以正确的方式创建请求并获得响应的方法吗?
我拥有要用作从站和PC-作为主站的PLC。
我试图以这种方式做到这一点:
from pymodbus.client.sync import ModbusTcpClient
host = '192.168.56.9'
port = 502
client = ModbusTcpClient(host, port)
client.connect()
#Register address 0x102A (4138dec) with a word count of 1
#Value - MODBUS/TCP Connections
#Access - Read
#Description - Number of TCP connections
request = client.read_holding_registers(4138, 1)
response = client.execute(request)
print response
>>> ReadRegisterResponse (1)
最佳答案
您可以执行dir(response)
来检查响应的组成,但是如果pymodbus TCP master与RTU串行master实现类似,则该数据在Registers字段中可用,因此请尝试打印response.Registers而不是response。 response.Registers
应该是一个单元素数组,其中包含您请求读取的寄存器值。