From bac8ee1036e5754a1c1274d4c5417685ebef1fb5 Mon Sep 17 00:00:00 2001 From: Hangyu Fan Date: Tue, 18 Jul 2023 11:33:27 +0800 Subject: [PATCH 1/2] bug fix for #1661 --- pymodbus/datastore/simulator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pymodbus/datastore/simulator.py b/pymodbus/datastore/simulator.py index 5275fa5a7..8d8560586 100644 --- a/pymodbus/datastore/simulator.py +++ b/pymodbus/datastore/simulator.py @@ -767,7 +767,7 @@ def build_registers_from_value(cls, value, is_int): if is_int: value_bytes = int.to_bytes(value, 4, "big") else: - value_bytes = struct.pack("f", value) + value_bytes = struct.pack(">f", value) regs[0] = int.from_bytes(value_bytes[:2], "big") regs[1] = int.from_bytes(value_bytes[-2:], "big") return regs @@ -781,5 +781,5 @@ def build_value_from_registers(cls, registers, is_int): if is_int: value = int.from_bytes(value_bytes, "big") else: - value = struct.unpack("f", value_bytes)[0] + value = struct.unpack(">f", value_bytes)[0] return value From ac5272621140390b2c0044f07632145bf0390708 Mon Sep 17 00:00:00 2001 From: Hangyu Fan Date: Tue, 18 Jul 2023 13:36:05 +0800 Subject: [PATCH 2/2] pass unit test --- test/test_simulator.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/test_simulator.py b/test/test_simulator.py index 5366a5605..89f86cdca 100644 --- a/test/test_simulator.py +++ b/test/test_simulator.py @@ -130,16 +130,16 @@ class TestSimulator: type=CellType.UINT32, action=2, action_kwargs={"minval": 10, "maxval": 80} ), Cell(type=CellType.NEXT, value=50), - Cell(type=CellType.FLOAT32, access=True, value=72), - Cell(type=CellType.NEXT, access=True, value=17221), - Cell(type=CellType.FLOAT32, access=True, value=34161), - Cell(type=CellType.NEXT, access=True, value=45381), - Cell(type=CellType.FLOAT32, access=True, value=34161), - Cell(type=CellType.NEXT, access=True, value=45381), - Cell(type=CellType.FLOAT32, value=1653, action=1), - Cell(type=CellType.NEXT, value=43080), # 40 - Cell(type=CellType.FLOAT32, value=1653, action=1), - Cell(type=CellType.NEXT, value=43080), + Cell(type=CellType.FLOAT32, access=True, value=17731), + Cell(type=CellType.NEXT, access=True, value=18432), + Cell(type=CellType.FLOAT32, access=True, value=17841), + Cell(type=CellType.NEXT, access=True, value=29061), + Cell(type=CellType.FLOAT32, access=True, value=17841), + Cell(type=CellType.NEXT, access=True, value=29061), + Cell(type=CellType.FLOAT32, value=18600, action=1), + Cell(type=CellType.NEXT, value=29958), # 40 + Cell(type=CellType.FLOAT32, value=18600, action=1), + Cell(type=CellType.NEXT, value=29958), Cell(type=CellType.STRING, value=int.from_bytes(bytes("St", "utf-8"), "big")), Cell(type=CellType.NEXT, value=int.from_bytes(bytes("r ", "utf-8"), "big")), Cell(type=CellType.STRING, value=int.from_bytes(bytes("St", "utf-8"), "big")),