简体   繁体   中英

How do I interpert the output of registers using pymodbus?

I am using pymodbus to get the values of a few registers via modbus. I have a connection and results, but I am confused on how to interpret the results. My code is:

from pymodbus.client.sync import ModbusTCPClient

client = ModbusTcpClient(host ="192.168.0.42", port= 502)
client.conect()
rr = client.read_input_registers(0,2, unit=42)
print(rr.registers)

The result I get is [37139,16190]. The documentation of the instrument I'm working with says "These registers are 16 bits each...All of the values are reported as 32-bit IEEE standard 754 floating point format. This uses 2 sequential registers, least significant 16 bits first." Would that mean the resulting floating point number is 1619037139? The result should be in the range of 0.2-0.4 (rounded). I appreciate any help!

IEEE 754 is a means of encoding floating point numbers and "least significant 16 bits first" means little endian encoding; pymodbus can help you decode this format:

from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder

registers = [37139,16190]
decoder = BinaryPayloadDecoder.fromRegisters(registers, wordorder=Endian.Little)
print ("Result: " + str(decoder.decode_32bit_float()))

( try it - result is 0.18659807741641998 which seems in line with your expectations).

Note that this is a useful tool for taking the raw (hex) results from a modbus query and decoding them in a number of ways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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