gpt4 book ai didi

python - 使用 pymodbus 转换 Modbus 响应

转载 作者:太空宇宙 更新时间:2023-11-03 20:46:22 30 4
gpt4 key购买 nike

我想将 PC 与 smart meter 进行通信它允许Modbus TCP通信,PC将成为主机,我只需要读取保持寄存器并以浮点格式显示它们。我正在使用 python 和 pymodbus 2.2.0 我的代码是:

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 寄存器您无法获得 float ,因为 Modbus 寄存器是 16 位整数。如果你想获得一个 float ,你需要读取两个寄存器。

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

关于python - 使用 pymodbus 转换 Modbus 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56573167/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com