3

谁能解释如何通过 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)
4

2 回答 2

3

设置unit参数并使用print(request.registers)代替print(request)

这是一个例子:

request = client.read_holding_registers(4138, 1, unit=1)  # Set unit argument.

if not request.isError():
    '''isError() method implemented in pymodbus 1.4.0 and above'''
    print(request.registers)  # Your problem is here.

else:
    # Do stuff for error handling.
    print('Error message: {}'.format(request))
于 2018-08-27T18:29:00.993 回答
2

您可以执行dir(response)检查响应的组成,但如果 pymodbus TCP 主机类似于 RTU 串行主机实现,则数据在寄存器字段中可用,因此请尝试打印 response.Registers 而不是响应。response.Registers应该是一个包含您请求读取的寄存器值的单元素数组。

于 2014-02-12T21:23:42.840 回答