Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions examples/datastore_simulator_share.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@
"uint16": 1,
"uint32": 45000,
"float32": 127.4,
"float64": 505.14,
"string": "X",
},
"action": {
"bits": None,
"uint16": None,
"uint32": None,
"float32": None,
"float64": None,
"string": None,
},
},
Expand Down Expand Up @@ -102,11 +104,14 @@
{"addr": [33, 36], "value": 5678.19},
{"addr": [37, 40], "value": 345000.18, "action": "increment"},
],
"float64": [
{"addr": [41, 44], "value": -42478.53, "action": "random"},
],
"string": [
{"addr": [41, 42], "value": "Str"},
{"addr": [43, 44], "value": "Strx"},
{"addr": [45, 46], "value": "Str"},
{"addr": [47, 48], "value": "Strx"},
],
"repeat": [{"addr": [0, 45], "to": [46, 138]}],
"repeat": [{"addr": [0, 48], "to": [49, 147]}],
}


Expand Down
41 changes: 27 additions & 14 deletions examples/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,27 @@

from pymodbus import FramerType
from pymodbus.client import AsyncModbusTcpClient
from pymodbus.datastore import ModbusSimulatorContext
from pymodbus.datastore import CellType, ModbusSimulatorContext
from pymodbus.server import ModbusSimulatorServer, get_simulator_commandline


_logger = logging.getLogger(__file__)


async def read_registers(
client, addr, count, is_int, curval=None, minval=None, maxval=None
client, addr, count, celltype: CellType, curval=None, minval=None, maxval=None
):
"""Run modbus call."""
rr = await client.read_holding_registers(addr, count=count, device_id=1)
assert not rr.isError()
if count == 1:
value = rr.registers[0]
else:
value = ModbusSimulatorContext.build_value_from_registers(rr.registers, is_int)
if not is_int:
value = round(value, 1)
value = ModbusSimulatorContext.build_value_from_registers(
rr.registers, celltype
)
if not CellType.is_int(celltype):
value = round(value, 2)
if curval:
assert value == curval, f"{value} == {curval}"
else:
Expand All @@ -43,19 +45,30 @@ async def run_calls(client, count):
_logger.info("### Read fixed/increment/random value of different types.")
_logger.info("--> UINT16")
for count in range(1, 5):
await read_registers(client, 1148, 1, True, curval=32117)
await read_registers(client, 2305, 1, True, curval=50 + count)
await read_registers(client, 2306, 1, True, minval=45, maxval=55)
await read_registers(client, 1148, 1, CellType.UINT16, curval=32117)
await read_registers(client, 2305, 1, CellType.UINT16, curval=50 + count)
await read_registers(client, 2306, 1, CellType.UINT16, minval=45, maxval=55)

_logger.info("--> UINT32")
await read_registers(client, 3188, 2, True, curval=32514)
await read_registers(client, 3876, 2, True, curval=50000 + count)
await read_registers(client, 3878, 2, True, minval=45000, maxval=55000)
await read_registers(client, 3188, 2, CellType.UINT32, curval=32514)
await read_registers(client, 3876, 2, CellType.UINT32, curval=50000 + count)
await read_registers(
client, 3878, 2, CellType.UINT32, minval=45000, maxval=55000
)

_logger.info("--> FLOAT32")
await read_registers(client, 4188, 2, False, curval=32514.2)
await read_registers(client, 4876, 2, False, curval=50000.0 + count)
await read_registers(client, 4878, 2, False, minval=45000.0, maxval=55000.0)
await read_registers(client, 4188, 2, CellType.FLOAT32, curval=32514.2)
await read_registers(client, 4876, 2, CellType.FLOAT32, curval=50000.0 + count)
await read_registers(
client, 4878, 2, CellType.FLOAT32, minval=45000.0, maxval=55000.0
)

_logger.info("--> FLOAT64")
await read_registers(client, 5092, 4, CellType.FLOAT64, curval=-32514.2)
await read_registers(client, 5164, 4, CellType.FLOAT64, curval=-42.15 + count)
await read_registers(
client, 5244, 4, CellType.FLOAT64, minval=-4242, maxval=314.92
)


async def run_simulator():
Expand Down
3 changes: 2 additions & 1 deletion pymodbus/datastore/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Datastore."""

__all__ = [
"CellType",
"ModbusDeviceContext",
"ModbusSequentialDataBlock",
"ModbusServerContext",
Expand All @@ -13,5 +14,5 @@
ModbusServerContext,
)
from .sequential import ModbusSequentialDataBlock
from .simulator import ModbusSimulatorContext
from .simulator import CellType, ModbusSimulatorContext
from .sparse import ModbusSparseDataBlock
Loading