我正在尝试与使用 Modbus RTU 协议的 Modbus 从设备通信,RPi 在 python 3.8 中具有 pymodbus lib。这是这个库的链接 http://riptideio.github.io/pymodbus/
我担心的是打印持有寄存器响应时。我已经阅读了有关它的其他帖子,人们建议将“.registers”与打印命令一起使用。
有时,我会打印出该值,但有时我会看到此错误
Traceback (most recent call last):
File "/home/user123/VsCode/Modbus_Rtu_sample.py", line 65, in <module>
value = result.registers
AttributeError: 'ModbusIOException' object has no attribute 'registers'
首先,我想知道为什么这有时有效,有时无效。正如我在 pymodbus 文档中看到的那样,没有名为 registers 的属性,那么它第一次是如何工作的?这是我的代码:
with ModbusClient(method='rtu', port='/dev/ttyUSB0', stopbits=1, bytesize=8 , parity='N', baudrate=9600, timeout=2) as client:
if not client.connect():
connection = client.connect()
connection = client.connect()
print("Your connection is set to",connection)
if client.connect():
read=client.read_holding_registers(address = 0x1000,count =4,unit=1)
value = read.registers
print(value)
#print(read.registers)
else:
print("Cannot connect to modbus slave")
client.close()
有没有更好的库/方法/方法来克服这个问题?我想要的只是为寄存器提供稳定的读/写输出。稍后我想调用一个由此创建的类,所以不希望出现有时我确实得到输出而其他时候抛出错误的情况。