我正在尝试使用pyModbus库中的诊断子功能,但在解码响应时遇到错误。这是由pyModbus库而不是我的代码处理的,因此很难找出问题出在哪里。
我参考了文档并尝试了此处给出的示例: https://pymodbus.readthedocs.io/en/latest/source/example/synchronous_client_ext.html
下面的示例使用该函数来获取NAK计数,但是我已经在diag_messages库中使用多个函数对其进行了尝试,包括DiagnosticStatusSimpleRequest和ReturnQueryDataResponse。
from pymodbus.client.sync import ModbusTcpClient, ModbusSerialClient
from pymodbus import diag_message
ip = '10.130.14.176'
with ModbusTcpClient(host=ip) as client:
testRequest = client.read_holding_registers(3000, 1, unit=1) # test request to make sure the connection is working and the device is communicating.
print(testRequest.registers) # returns expected value
rq = diag_message.ReturnSlaveNAKCountRequest(unit=1)
rr = client.execute(rq)
print(rr.isError) # get "Unable to decode request" error