简体   繁体   English

使用pymodbus转换Modbus响应

[英]Converting Modbus response using pymodbus

I want to comunicate a PC with a smart meter which allows Modbus TCP communication, the PC will be the master and I just need read holding registers and show them in float format. 我想将PC与允许Modbus TCP通讯的智能仪表进行通信,该PC将成为主机,而我只需要读取保持寄存器并以浮点格式显示它们即可。 I'm using python with pymodbus 2.2.0 My code is: 我在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()

what I get is: 我得到的是:

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)

Do you know how to resolve it using pymodbus 2.2.0? 您知道如何使用pymodbus 2.2.0解决该问题吗?

You have to extract the registers from your response, try this: 您必须从响应中提取寄存器,请尝试以下操作:

print(result.registers)

Or, if you want them one by one: 或者,如果您想要它们一个接一个:

for reg in result:
    print(reg)

If you're not sure if your server is running, its IP or port it's a good idea to use a standalone tool to test, if you are on Windows you can use QModMaster to make sure your setting and mapping. 如果不确定服务器是否正在运行,其IP或端口是否在运行,则最好使用独立工具进行测试;如果您使用的是Windows,则可以使用QModMaster来确保设置和映射。

And note that with just one Modbus register you cannot get a float because Modbus register are 16 bits integers. 请注意,仅使用一个Modbus寄存器就不会产生浮点数,因为Modbus寄存器是16位整数。 If you want to get a float you need to read two registers. 如果要浮动,则需要读取两个寄存器。

If, on the other hand, you only want to verify your client-side code you can run this example on your computer simultaneously with your client code. 另一方面,如果您只想验证客户端代码,则可以在计算机上同时运行此示例和客户端代码。 Of course, you have to change the IP address on your script to localhost , the port to 5020 and the register number you're reading to a lower number (or increase the size of the server datastore). 当然,您必须将脚本上的IP地址更改为localhost ,将端口更改为5020 ,并将要读取的寄存器号更改为较小的数字(或增加服务器数据存储区的大小)。 If you're on Windows you might need to disable or create a rule on your firewall. 如果您使用的是Windows,则可能需要在防火墙上禁用或创建规则。

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

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