gpt4 book ai didi

python - pymodbus:请求创建和响应接收

转载 作者:太空宇宙 更新时间:2023-11-04 03:55:32 26 4
gpt4 key购买 nike

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

最佳答案

设置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))

关于python - pymodbus:请求创建和响应接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18784931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com