繁体   English   中英

使用pymodbus转换Modbus响应

[英]Converting Modbus response using pymodbus

我想将PC与允许Modbus TCP通讯的智能仪表进行通信,该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解决该问题吗?

您必须从响应中提取寄存器,请尝试以下操作:

print(result.registers)

或者,如果您想要它们一个接一个:

for reg in result:
    print(reg)

如果不确定服务器是否正在运行,其IP或端口是否在运行,则最好使用独立工具进行测试;如果您使用的是Windows,则可以使用QModMaster来确保设置和映射。

请注意,仅使用一个Modbus寄存器就不会产生浮点数,因为Modbus寄存器是16位整数。 如果要浮动,则需要读取两个寄存器。

另一方面,如果您只想验证客户端代码,则可以在计算机上同时运行此示例和客户端代码。 当然,您必须将脚本上的IP地址更改为localhost ,将端口更改为5020 ,并将要读取的寄存器号更改为较小的数字(或增加服务器数据存储区的大小)。 如果您使用的是Windows,则可能需要在防火墙上禁用或创建规则。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM