我已经尝试了这个平台上存在的与pymodpoll相关的许多修复。我能够成功写入寄存器,但无法读取,我已确认我尝试读取的寄存器实际上是RW寄存器。Datasheet的产品。
用于阅读寄存器的mod poll命令是modpoll -m rtu -0 -1 -a 1 -b 9600 -p none -r 48 /dev/ttyUSB0
from pymodbus.client import ModbusSerialClient as modbus
#Connection to the modbus
def connect(self) -> None:
self.modbus = modbus(method='rtu',
port="/dev/ttyUSB0",
baudrate=9600,
stopbits=1,
bytesize=8,
timeout=1)
self.modbus.connect()
#function that works properly for setting the register value
def set_voltage(self, voltage:float =0) -> None:
if (voltage < self.min_volt) or (voltage > self.max_volt):
raise modbusError(
"Invalid voltage provided to the interface, voltage provided: {0}"
.format(voltage))
self.modbus.write_register(0x0030, int(voltage*100), unit=1)
#Read register function
def get_voltage_target(self) -> float:
reg = self.modbus.read_holding_registers(0x0030, 1, unit=1)
print(reg.registers)
输出:
AttributeError: 'ModbusIOException' object has no attribute 'registers'
我收到这个错误从reg.registers打印,我假设这只是从事实,我没有正确连接到modbus读取正确的寄存器。
这是我目前正在使用的代码,感谢您的帮助!
简化版编辑:
from pymodbus.client import ModbusSerialClient as RTUClient
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
PowerSupplyMB = RTUClient(method='rtu', port="/dev/ttyUSB0", baudrate=9600,stopbits=1,bytesize=8,timeout=1)
PowerSupplyMB.connect()
PowerSupplyMB.write_register(0x0030, int(10.2*100), unit=1)
mb_response = PowerSupplyMB.read_holding_registers(0x0030, 1, unit=1)
if not mb_response.isError():
'''isError() method implemented in pymodbus 1.4.0 and above'''
print(mb_response.registers) # Your problem is here.
else:
# Do stuff for error handling.
print('Error message: {}'.format(mb_response))
输出:
DEBUG:pymodbus.logging:Current transaction state - IDLE
DEBUG:pymodbus.logging:Running transaction 1
DEBUG:pymodbus.logging:SEND: 0x0 0x3 0x0 0x30 0x0 0x1 0x85 0xd4
DEBUG:pymodbus.logging:New Transaction state "SENDING"
DEBUG:pymodbus.logging:Changing transaction state from "SENDING" to "WAITING FOR REPLY"
DEBUG:pymodbus.logging:Transaction failed. (Modbus Error: [Invalid Message] No response received, expected at least 4 bytes (0 received))
DEBUG:pymodbus.logging:Frame - [b''] not ready
DEBUG:pymodbus.logging:Getting transaction 0
DEBUG:pymodbus.logging:Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
Error message: Modbus Error: [Input/Output] Modbus Error: [Invalid Message] No response received, expected at least 4 bytes (0 received)
进一步的调查表明,registers
包含错误-Modbus Error: [Input/Output] No Response received from the remote slave/Unable to decode response
。
1条答案
按热度按时间b09cbbtk1#
您的
modpoll
命令包含-a 1
(从机地址= 1),但查看调试信息,请求被发送到从机0(“0x 00x 3 0x 0 0x 30 0x 0 0x 1 0x 85 0xd 4”),因此可以解释没有响应的原因。根据文档,函数定义为
read_holding_registers(address: int, count: int = 1, slave: int = 0, **kwargs: Any) → ModbusResponse
;在你的代码中你说unit=1
而不是slave=1
...(我没有拿起这个,因为你说write_register
工作;它很可能会这样做,因为您正在向广播地址0
写入,但您无法使用该地址进行读取)。