任何人都可以通过Modbus TCP / IP使用pymodbus以正确的方式解释如何创建请求并获得响应吗?
我有PLC想要用作奴隶和PC - 作为主人。
我试图这样做:
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)
答案 0 :(得分:0)
您可以执行dir(response)
来检查响应的组成,但如果pymodbus TCP master与RTU串行主站实现类似,则数据在Registers字段中可用,因此请尝试打印response.Registers而不是响应。 response.Registers
应该是一个单元素数组,其中包含您要求阅读的注册值。
答案 1 :(得分:0)
设置unit
参数,并使用print(request.registers)
代替print(request)
。
这是一个例子:
request = client.read_holding_registers(4138, 1, unit=1) # Notice: Set the unit argument.
if not request.isError():
'''isError() method implemented in pymodbus 1.4.0 and above'''
result = request.registers # Your problem is here.
print(result)
else:
# Handle Error.
print('Unable to read or there is the connection problem.')