我想用允许Modbus TCP通信的smart meter与PC通信,PC将成为主机,我只需要读取保持寄存器并以浮点格式显示它们。我在pymodbus 2.2.0中使用python,我的代码是:
from pymodbus.client.sync import ModbusTcpClient
client = ModbusTcpClient('169.254.00.10')
result = client.read_holding_registers(1845,1,unit=0x01)
print('**************************************************************')
print(result)
client.close()
我得到的是:
DEBUG:pymodbus.transaction:Current transaction state - IDLE
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x3 0x7 0x35 0x0 0x1
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV: 0x0 0x1 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x6 0xc
DEBUG:pymodbus.framer.socket_framer:Processing: 0x0 0x1 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x6 0xc
DEBUG:pymodbus.factory:Factory Response[ReadHoldingRegistersResponse: 3]
DEBUG:pymodbus.transaction:Adding transaction 1
DEBUG:pymodbus.transaction:Getting transaction 1
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
**************************************************************
ReadRegisterResponse (1)
你知道如何使用pymodbus 2.2.0解决这个问题吗?
发布于 2019-06-13 17:01:29
你必须从你的响应中提取寄存器,试试这个:
print(result.registers)
或者,如果您想一个接一个地使用它们:
for reg in result:
print(reg)
如果你不确定你的服务器是否正在运行,它的IP或端口是一个好主意使用一个独立的工具来测试,如果你是在Windows上,你可以使用QModMaster来确保你的设置和映射。
请注意,仅使用一个Modbus寄存器,您无法获得浮点数,因为Modbus寄存器是16位整数。如果你想得到一个浮点数,你需要读取两个寄存器。
另一方面,如果您只想验证客户端代码,则可以在计算机上与客户端代码同时运行this example。当然,您必须将脚本中的IP地址更改为localhost
,将端口更改为5020
,并将正在读取的寄存器编号更改为较小的数字(或者增加服务器数据存储的大小)。如果您使用的是Windows,则可能需要在防火墙上禁用或创建规则。
https://stackoverflow.com/questions/56573167
复制相似问题