Skip to content

Commit 0b7f216

Browse files
committed
SimData BITS do not mix int and bool.
1 parent b1d831d commit 0b7f216

4 files changed

Lines changed: 19 additions & 21 deletions

File tree

examples/contrib/test_datastores.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async def run_async(port, context, run_test):
4040

4141
Log.info("### Create client object")
4242
client = modbusClient.AsyncModbusTcpClient(
43-
"127.0.0.1",
43+
NULLMODEM_HOST,
4444
port=5020,
4545
framer=FramerType.SOCKET,
4646
)
@@ -56,23 +56,24 @@ async def run_sequential(port) -> None:
5656
"""Combine setup and run."""
5757
async def run_old_test(client):
5858
"""Run test."""
59+
Log.info("### read_coils")
5960
rr = await client.read_coils(32, count=1, device_id=1)
6061
assert rr.bits == [True] + [False] * 7
6162
rr = await client.read_coils(32, count=16, device_id=1)
6263
assert rr.bits == [True]*16
6364

64-
65-
pymodbus_apply_logging_config("info")
6665
context = ModbusServerContext(devices=ModbusDeviceContext(
6766
co=ModbusSequentialDataBlock(0x00, [17] * 100),
6867
),
6968
single=True
7069
)
70+
Log.info("Run sequential test.")
7171
await run_async(port, context, run_old_test)
7272

7373

7474
async def run_all(port):
7575
"""Run all tests."""
76+
pymodbus_apply_logging_config("info")
7677
await run_sequential(port)
7778

7879
if __name__ == "__main__":

pymodbus/simulator/simdata.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,10 @@ def __check_parameters(self):
143143
x_values = self.values if isinstance(self.values, list) else [self.values]
144144
x_datatype, _x_struct, _x_len = SimUtils.DATATYPE_STRUCT[self.datatype]
145145
if self.datatype == DataType.BITS:
146-
x_datatype = int if isinstance(x_values[0], int) else bool
146+
x_datatype = bool if isinstance(x_values[0], bool) else int
147147
for x_value in x_values:
148+
if self.datatype == DataType.BITS and x_datatype is int and isinstance(x_value, bool):
149+
raise TypeError(f"values= {x_value} int and bool cannot be mixed")
148150
if not isinstance(x_value, x_datatype):
149151
raise TypeError(f"values= {x_value} is not {x_datatype!s}")
150152
if x_datatype is str and not x_value:

test/simulator/test_simdata.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ def test_simdata_value_ok(self, value, value_type):
8080
([11, 12.0], DataType.REGISTERS),
8181
(1.0, DataType.BITS),
8282
([True, 1.0], DataType.BITS),
83+
([True, 1], DataType.BITS),
84+
([1, True], DataType.BITS),
8385
])
8486
def test_simdata_value_not_ok(self, value, value_type):
8587
"""Test simdata value."""
@@ -141,6 +143,7 @@ def test_simdata_build_string(self, value, code, expect):
141143

142144
@pytest.mark.parametrize(("value", "regs"), [
143145
([True, False, True], [True, False, True]),
146+
([True] + [False] * 14 + [True, False], [True] + [False] * 14 + [True, False]),
144147
(0x0100, [True] + [False]*15),
145148
([256, 1], [True] + [False]*23 + [True] + [False]*7),
146149
])

test/simulator/test_simdevice.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ async def test_simdevice_bit_addressing(self):
146146
([SimData(0, values=[0xffff, 0xffff], datatype=DataType.BITS)],
147147
(0, [65535, 65535, 0],
148148
[DataType.BITS, 0, DataType.INVALID])),
149+
# ([SimData(0, values=[True] * 2 + [False]*6 + [True] + [False]*6 + [True], datatype=DataType.BITS)],
150+
# (0, [129, 0],
151+
# [DataType.BITS, DataType.INVALID])),
149152
([SimData(1, values=123, datatype=DataType.INT16),
150153
SimData(3, values=456, datatype=DataType.INT16)],
151154
(1, [123, 0, 456, 0],
@@ -212,8 +215,8 @@ def test_simdevice_build_blocks(self):
212215
assert lists == result
213216

214217
@pytest.mark.parametrize(("block", "result", "addr"), [
215-
#([SimData(0, values=123, datatype=DataType.BITS)], [0x007b, 0x0000], 0),
216-
([SimData(0, values=True, datatype=DataType.BITS)], [0x0001, 0x0000], 0),
218+
([SimData(0, values=123, datatype=DataType.BITS)], [0x007b, 0x0000], 0),
219+
#([SimData(0, values=True, datatype=DataType.BITS)], [0x0001, 0x0000], 0),
217220
#([SimData(1, values=123, datatype=DataType.BITS)], [0x00F6, 0x0000, 0x0000], 0),
218221
])
219222
def test_simdevice_build_bits(self, block, result, addr):
@@ -222,21 +225,10 @@ def test_simdevice_build_bits(self, block, result, addr):
222225

223226
sd = SimDevice(id=1, simdata=(block, block, [sim123], [sim123]))
224227
result_block = cast(dict[str, SimRegs], sd.build_device())
225-
assert result_block["c"][0] == addr
226-
assert result_block["c"][1] == result
227-
assert result_block["d"][0] == addr
228-
assert result_block["d"][1] == result
229-
assert result_block["h"][1] == [123, 0]
230-
assert result_block["i"][1] == [123, 0]
231-
assert len(result_block["c"][1]) == len(result)
232-
#sd = SimDevice(id=1, simdata=(
233-
# [sim123,
234-
# SimData(17, values=True, datatype=DataType.BITS),
235-
# SimData(20, values=123, datatype=DataType.BITS)],
236-
# [sim123], [sim123], [sim123]
237-
#))
238-
#result_block = cast(dict[str, SimRegs], sd.build_device())
239-
#assert result_block["c"][1] == [0x7b, 0x01d8, 0x03, 0x00]
228+
assert result_block["c"][0:2] == (addr, result)
229+
assert result_block["d"][0:2] == (addr, result)
230+
assert result_block["h"][0:2] == (1, [123, 0])
231+
assert result_block["i"][0:2] == (1, [123, 0])
240232

241233
@pytest.mark.parametrize("count", range(1,4))
242234
@pytest.mark.parametrize("data_count", range(1,4))

0 commit comments

Comments
 (0)