Skip to content

Bug or misunderstanding of word_order in convert methods #2675

@igorbga

Description

@igorbga

Look at this code:

from pymodbus.client.mixin import ModbusClientMixin


original_values = [1.0, 2.0, 3.0]
registers = ModbusClientMixin.convert_to_registers(
    original_values, ModbusClientMixin.DATATYPE.FLOAT32, word_order="little"
)

converted_values = ModbusClientMixin.convert_from_registers(
    registers, ModbusClientMixin.DATATYPE.FLOAT32, word_order="little"
)
print("Original values:", original_values)
print("Converted registers:", registers)
print("Converted back values:", converted_values)

I expected this result:

Original values: [1.0, 2.0, 3.0]
Converted registers: [0, 16448, 0, 16384, 0, 16256]
Converted back values: [1.0, 2.0, 3.0]

But got:

Original values: [1.0, 2.0, 3.0]
Converted registers: [0, 16448, 0, 16384, 0, 16256]
Converted back values: [3.0, 2.0, 1.0]

I thought the "word_order" was meant to be used in order to specify how words are ordered when decomposing datatypes with more bytes. For example in a 4 byte float that will be transmited as 2 words I expected "word_order" to determine if the first word has the least significant portion (little) or the most significant part (big).

But checking at the code I can see all it does is reverse all the registers or something like that.

I guess I'm just misunderstanding something...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions