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

标签 python modbus modbus-tcp pymodbus

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

相关文章:

java - 尝试使用 Modbus 协议(protocol)连接设备时出现 net.wimpi.modbus.ModbusSlaveException : Error Code = 2,

python - 如何使用 modbus-tk RTU 设置简单的从站和主站 (Python)

python - 为 TCP 服务器寄存器赋值的 pymodbus 语法是什么?

java - 如何使用 jamod 库在 Java 中实例化 Register 对象?

c# - 一起编写 ModbusClient 和 ModbusServer 时出现问题

python - Pandas Pivot 创建 NaN

python - 与 +、/等相比,使用 np.add()、np.divide() 等有什么优势?

python - python中变量的范围

python - 将行格式化为向量,如何reduceByKey (list(n_1, m_1)....(n_k, m_k)) 到 (n_1...n_k) (m_1...m_k))

c# - 如何访问5xxxx Modbus地址