Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions pymodbus/repl/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ def run(
help="Randomize every `r` reads. 0=never, 1=always,2=every-second-read"
", and so on. Applicable IR and DI.",
),
change_rate: int = typer.Option(
0,
"--change-rate",
"-c",
help="Rate in % registers to change. 0=none, 100=all, 12=12% of registers"
", and so on. Applicable IR and DI.",
),
):
"""Run Reactive Modbus server.

Expand Down Expand Up @@ -195,6 +202,7 @@ def run(

modbus_config["handler"] = handler
modbus_config["randomize"] = randomize
modbus_config["change_rate"] = change_rate
app = ReactiveServer.factory(
modbus_server,
framer,
Expand Down
30 changes: 29 additions & 1 deletion pymodbus/server/reactive/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def __init__(
holding_registers: BaseModbusDataBlock = None,
zero_mode: bool = False,
randomize: int = 0,
change_rate: int = 0,
**kwargs,
):
"""Reactive Modbus slave context supporting simulating data.
Expand All @@ -124,6 +125,8 @@ def __init__(
:param zero_mode: Enable zero mode for data blocks
:param randomize: Randomize reads every <n> reads for DI and IR,
default is disabled (0)
:param change_rate: Rate in % of registers to change for DI and IR,
default is disabled (0)
:param min_binary_value: Minimum value for coils and discrete inputs
:param max_binary_value: Max value for discrete inputs
:param min_register_value: Minimum value for input registers
Expand All @@ -142,6 +145,9 @@ def __init__(
min_register_value = kwargs.get("min_register_value", 0)
max_register_value = kwargs.get("max_register_value", 65535)
self._randomize = randomize
self._change_rate = change_rate
if self._randomize > 0:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will potentially confuse a user, please add a logger message so the user knows why -r did not work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the new commit, do you have any suggestion on how to handle the case that user provide both flags ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My personal preference is to terminate with an error message, but at the very least the user needs to be aware that the -c is voided.

self._change_rate = 0
self._lock = threading.Lock()
self._read_counter = {"d": 0, "i": 0}
self._min_binary_value = min_binary_value
Expand Down Expand Up @@ -177,6 +183,20 @@ def getValues(self, fc_as_hex, address, count=1):
# "'%s'", _block_type, values, address)
self.store[_block_type].setValues(address, values)
self._read_counter[_block_type] += 1
elif self._change_rate > 0 and _block_type in {"d", "i"}:
for offset in range(count):
if random.randint(1, 100) <= self._change_rate:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will work but not with the effect you described. E.g. -r 50, and 100 registers….randint will be called 100 times but potentially deliver no value below 50.

Please either correct or correct help.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I push a new commit per @dhoomakethu's suggestion and I think that solved the problem

with self._lock:
# Update values
if _block_type == "d":
min_val = self._min_binary_value
max_val = self._max_binary_value
else:
min_val = self._min_register_value
max_val = self._max_register_value
self.store[_block_type].setValues(
address + offset, random.randint(min_val, max_val)
)
values = self.store[_block_type].getValues(address, count)
return values

Expand Down Expand Up @@ -385,13 +405,15 @@ def create_context(
unit: list[int] = [1],
single: bool = False,
randomize: int = 0,
change_rate: int = 0,
): # pylint: disable=dangerous-default-value
"""Create Modbus context.

:param data_block_settings: Datablock (dict) Refer DEFAULT_DATA_BLOCK
:param unit: Unit id for the slave
:param single: To run as a single slave
:param randomize: Randomize every <n> reads for DI and IR.
:param change_rate: Rate in % of registers to change for DI and IR.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code do not work in % but random, that is every run will change a different number of registers some will change more registers than -r % and others fewer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I push a new commit per @dhoomakethu's suggestion and I think that solved the problem

:return: ModbusServerContext object
"""
data_block = data_block_settings.pop("data_block", DEFAULT_DATA_BLOCK)
Expand Down Expand Up @@ -422,7 +444,11 @@ def create_context(
block[modbus_entity] = db(start_address, default_values)

slave_context = ReactiveModbusSlaveContext(
**block, randomize=randomize, zero_mode=True, **data_block_settings
**block,
randomize=randomize,
change_rate=change_rate,
zero_mode=True,
**data_block_settings,
)
if not single:
slaves[i] = slave_context
Expand Down Expand Up @@ -471,6 +497,7 @@ def factory( # pylint: disable=dangerous-default-value,too-many-arguments
sys.exit(1)
server = SERVER_MAPPER.get(server)
randomize = kwargs.pop("randomize", 0)
change_rate = kwargs.pop("change_rate", 0)
if not framer:
framer = DEFAULT_FRAMER.get(server)
if not context:
Expand All @@ -479,6 +506,7 @@ def factory( # pylint: disable=dangerous-default-value,too-many-arguments
unit=unit,
single=single,
randomize=randomize,
change_rate=change_rate,
)
if not identity:
identity = cls.create_identity()
Expand Down