You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am facing some issues with the new method for converting registers to float. The documention for the device i am communicating with says the word order and byte order is in the little endian format.
This example has the two registers [0xDA77, 0xFB41] which is supposed to be read as 31.43
I have been using the following method to convert registers to floats (being deprecated)
from pymodbus.payload import BinaryPayloadDecoder, BinaryPayloadBuilder
from pymodbus.constants import Endian
temperature_registers = [0xDA77, 0xFB41] # DA 77 FB 41 (temperature)
values = {}
values["raw_value"] = [hex(val) for val in temperature_registers]
values["decoded_value"] = BinaryPayloadDecoder.fromRegisters(temperature_registers,byteorder=Endian.LITTLE, wordorder=Endian.LITTLE).decode_32bit_float()
This has been working fine and gives the expected output of
{'raw_value': ['0xda77', '0xfb41'], 'decoded_value': 31.433521270751953}
I get a message that this method will be deprecated after version 3.9.0 so i am trying to convert to the new method of using convert_from_registers()
My attempt is as follows
from pymodbus.client.mixin import ModbusClientMixin
DATATYPE = ModbusClientMixin.DATATYPE
temperature_registers = [0xDA77, 0xFB41] # DA 77 FB 41 (temperature)
values = {}
values["raw_value"] = [hex(val) for val in temperature_registers]
values["decoded_value"] = ModbusClientMixin.convert_from_registers(temperature_registers,data_type=DATATYPE.FLOAT32,word_order="little")
But this gives an unexpexted result
{'raw_value': ['0xda77', '0xfb41'], 'decoded_value': -1.006544287142157e+36}
Shuffling the bytes around to a reverse order (little endian byte order) and selecting word_order="big" in the following way
from pymodbus.client.mixin import ModbusClientMixin
DATATYPE = ModbusClientMixin.DATATYPE
#temperature_registers = [0xDA77, 0xFB41] # DA 77 FB 41 (temperature)
temperature_registers = [0x41FB,0x77DA] # reverse byte order
values = {}
values["raw_value"] = [hex(val) for val in temperature_registers]
values["decoded_value"] = ModbusClientMixin.convert_from_registers(temperature_registers,data_type=DATATYPE.FLOAT32,word_order="big")
This gives the expected result
{'raw_value': ['0x41fb', '0x77da'], 'decoded_value': 31.433521270751953}
From what i can tell from the documentation of the modbus device the order needs to be reversed from A B C D (DA 77 FB 41) to D C B A (41 FB 77 DA). To me this would require a little endian word_order and little endian byte order.
Somewhat confused by the stark difference in inputs and outputs in these two methods and that the convert_from_registers doesn't seem to have byteorder option.
I guess i can create a byte order swap function for myself though.
Regards
The text was updated successfully, but these errors were encountered:
Hi
I am facing some issues with the new method for converting registers to float. The documention for the device i am communicating with says the word order and byte order is in the little endian format.
This example has the two registers [0xDA77, 0xFB41] which is supposed to be read as 31.43
I have been using the following method to convert registers to floats (being deprecated)
This has been working fine and gives the expected output of
{'raw_value': ['0xda77', '0xfb41'], 'decoded_value': 31.433521270751953}
I get a message that this method will be deprecated after version 3.9.0 so i am trying to convert to the new method of using convert_from_registers()
My attempt is as follows
But this gives an unexpexted result
{'raw_value': ['0xda77', '0xfb41'], 'decoded_value': -1.006544287142157e+36}
Shuffling the bytes around to a reverse order (little endian byte order) and selecting word_order="big" in the following way
This gives the expected result
{'raw_value': ['0x41fb', '0x77da'], 'decoded_value': 31.433521270751953}
From what i can tell from the documentation of the modbus device the order needs to be reversed from A B C D (DA 77 FB 41) to D C B A (41 FB 77 DA). To me this would require a little endian word_order and little endian byte order.
Somewhat confused by the stark difference in inputs and outputs in these two methods and that the convert_from_registers doesn't seem to have byteorder option.
I guess i can create a byte order swap function for myself though.
Regards
The text was updated successfully, but these errors were encountered: