如何使用 PyModbus rtu 访问特定寄存器?

How to access particular registers using PyModbus rtu?

我是 Python 和 Modbus 的新手,在提出一个可能容易解决的问题之前,我花了大量时间尽可能多地进行研究、收集和试验。如果有人能指出正确的方向,我将不胜感激。

本质上,我正在尝试使用供应商提供给我的 Modbus 映射来读取设备的寄存器...我可以建立连接(我认为),但是在窥探我想读取的寄存器时遇到问题.

from pymodbus.client.sync import ModbusSerialClient

# Connection to device
client = ModbusSerialClient(
    port="COM7",
    startbit=1,
    databits=8,
    parity="N",
    stopbits=2,
    errorcheck="crc",
    baudrate=38400,
    method="RTU",
    timeout=3,
)
if client.connect(): # Connection to slave device
    print("Connection Successful")
    register = client.read_coils(54, 2)
    print(register)
    client.close()
else:
    print("Failed to connect to Modbus device")

并收到此结果。

Connection Successful
Modbus Error: [Input/Output] Modbus Error: [Invalid Message] No response received, expected at least 2 bytes (0 received)

寄存器地址=54,字数=1,数据类型=INT16。

我可能做错了,但是,如果能朝正确的方向推进,我们将不胜感激。

因此,通过更多的研究,我能够访问所需的数据。

from atexit import register
from pymodbus.client.sync import ModbusSerialClient
client = ModbusSerialClient(
port="COM7",
startbit=1,
databits=8,
parity="N",
stopbits=2,
errorcheck="crc",
baudrate=38400,
method="RTU",
timeout=3,
)

if client.connect():  # Trying for connect to Modbus slave
# Read holding register
print("Connection Successful")
res = client.read_holding_registers(address=53, count=1, unit=1)

# Where "address" is register address
# Where "count" is the number of registers to read
# Where "unit" is the slave address, found in vendor documentation

输出:

res = holding register value