From 50153744f2e36abd80dfd220b8cd45c8d6141b27 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Tue, 24 Dec 2024 15:32:27 +0100 Subject: [PATCH 1/2] Add combined to/from registers. --- pymodbus/client/mixin.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/pymodbus/client/mixin.py b/pymodbus/client/mixin.py index ca5897983..449314bb8 100644 --- a/pymodbus/client/mixin.py +++ b/pymodbus/client/mixin.py @@ -771,3 +771,38 @@ def convert_to_registers( if word_order == "little": regs.reverse() return regs + + @classmethod + def convert_combined_from_registers( + cls, registers: list[int], data_types: list[tuple[DATATYPE, int]], word_order: Literal["big", "little"] = "big" + ) -> list[bool | int | float | str]: + """Convert registers to int/float/str. + + :param registers: list of registers received from e.g. read_holding_registers() + :param data_types: list of (data type, count) to convert to + :param word_order: "big"/"little" order of words/registers + :returns: scalar or array of "data_type" + :raises ModbusException: when size of registers is not a multiple of data_type + """ + _ = registers + _ = data_types + _ = word_order + return [32] + + + @classmethod + def convert_combined_to_registers( + cls, values: list[bool | int | float | str] , data_types: list[DATATYPE], word_order: Literal["big", "little"] = "big" + ) -> list[int]: + """Convert int/float/str to registers (16/32/64 bit). + + :param values: list of values to be converted + :param data_types: list of data types to convert from + :param word_order: "big"/"little" order of words/registers + :returns: List of registers, can be used directly in e.g. write_registers() + :raises TypeError: when there is a mismatch between data_type and value + """ + _ = values + _ = data_types + _ = word_order + return [12] From 3db8d844da4878cf8e09f0f36e15a9e085608bf1 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Wed, 1 Jan 2025 09:45:38 +0100 Subject: [PATCH 2/2] temp. --- pymodbus/client/mixin.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pymodbus/client/mixin.py b/pymodbus/client/mixin.py index 449314bb8..8a9f09727 100644 --- a/pymodbus/client/mixin.py +++ b/pymodbus/client/mixin.py @@ -701,7 +701,7 @@ def convert_from_registers( :param registers: list of registers received from e.g. read_holding_registers() :param data_type: data type to convert to - :param word_order: "big"/"little" order of words/registers + :param word_order: "big"/"little" order of words/registers (Only used for special devices) :returns: scalar or array of "data_type" :raises ModbusException: when size of registers is not a multiple of data_type """ @@ -742,7 +742,7 @@ def convert_to_registers( :param value: value to be converted :param data_type: data type to convert from - :param word_order: "big"/"little" order of words/registers + :param word_order: "big"/"little" order of words/registers (Only used for special devices) :returns: List of registers, can be used directly in e.g. write_registers() :raises TypeError: when there is a mismatch between data_type and value """ @@ -780,7 +780,7 @@ def convert_combined_from_registers( :param registers: list of registers received from e.g. read_holding_registers() :param data_types: list of (data type, count) to convert to - :param word_order: "big"/"little" order of words/registers + :param word_order: "big"/"little" order of words/registers (Only used for special devices) :returns: scalar or array of "data_type" :raises ModbusException: when size of registers is not a multiple of data_type """ @@ -797,7 +797,7 @@ def convert_combined_to_registers( """Convert int/float/str to registers (16/32/64 bit). :param values: list of values to be converted - :param data_types: list of data types to convert from + :param data_types: list of data types to convert from (Only used for special devices) :param word_order: "big"/"little" order of words/registers :returns: List of registers, can be used directly in e.g. write_registers() :raises TypeError: when there is a mismatch between data_type and value