Skip to content

Commit d8d9d70

Browse files
sumguythojaniversen
authored andcommitted
fixed kwargs not being expanded for actions on bit registers, adjusted tests to catch this issue (#2161)
1 parent 98fc477 commit d8d9d70

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

pymodbus/datastore/simulator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ class ModbusSimulatorContext(ModbusBaseSlaveContext):
434434
"write": [ --> allow write, efault is ReadOnly
435435
[5, 5] --> start, end bytes, repeated as needed
436436
],
437-
"bits": [ --> Define bits (1 register == 1 byte)
437+
"bits": [ --> Define bits (1 register == 2 bytes)
438438
[30, 31], --> start, end registers, repeated as needed
439439
{"addr": [32, 34], "value": 0xF1}, --> with value
440440
{"addr": [35, 36], "action": "increment"}, --> with action
@@ -601,8 +601,9 @@ def getValues(self, func_code, address, count=1):
601601
for i in range(real_address, real_address + reg_count):
602602
reg = self.registers[i]
603603
if reg.action:
604+
kwargs = reg.action_kwargs or {}
604605
self.action_methods[reg.action](
605-
self.registers, i, reg, reg.action_kwargs
606+
self.registers, i, reg, **kwargs
606607
)
607608
self.registers[i].count_read += 1
608609
while count and bit_index < 16:

test/sub_server/test_simulator.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,12 @@ def test_simulator_action_increment(
528528
exc_simulator.registers[30].value = regs[0]
529529
exc_simulator.registers[31].value = regs[1]
530530
for expect_value in expected:
531-
regs = exc_simulator.getValues(FX_READ_REG, 30, reg_count)
531+
if celltype != CellType.BITS:
532+
regs = exc_simulator.getValues(FX_READ_REG, 30, reg_count)
533+
else:
534+
reg_bits = exc_simulator.getValues(FX_READ_BIT, 30 * 16, 16)
535+
reg_value = sum([ bit * 2 ** i for i, bit in enumerate(reg_bits)])
536+
regs = [reg_value]
532537
if reg_count == 1:
533538
assert expect_value == regs[0], f"type({celltype})"
534539
else:
@@ -563,7 +568,12 @@ def test_simulator_action_random(self, celltype, minval, maxval):
563568
is_int = celltype != CellType.FLOAT32
564569
reg_count = 1 if celltype in (CellType.BITS, CellType.UINT16) else 2
565570
for _i in range(100):
566-
regs = exc_simulator.getValues(FX_READ_REG, 30, reg_count)
571+
if celltype != CellType.BITS:
572+
regs = exc_simulator.getValues(FX_READ_REG, 30, reg_count)
573+
else:
574+
reg_bits = exc_simulator.getValues(FX_READ_BIT, 30 * 16, 16)
575+
reg_value = sum([ bit * 2 ** i for i, bit in enumerate(reg_bits)])
576+
regs = [reg_value]
567577
if reg_count == 1:
568578
new_value = regs[0]
569579
else:

0 commit comments

Comments
 (0)