Skip to content

Commit

Permalink
Clean datastore setValues. (#2145)
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Jun 18, 2024
1 parent 0f12581 commit 98fc477
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
11 changes: 6 additions & 5 deletions pymodbus/bit_write_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ def execute(self, context):
if not context.validate(self.function_code, self.address, 1):
return self.doException(merror.IllegalAddress)

result = context.setValues(self.function_code, self.address, [self.value])
if isinstance(result, ExceptionResponse):
return result
context.setValues(self.function_code, self.address, [self.value])
# result = context.setValues(self.function_code, self.address, [self.value])
# if isinstance(result, ExceptionResponse):
# return result
values = context.getValues(self.function_code, self.address, 1)
if isinstance(values, ExceptionResponse):
return values
# if isinstance(values, ExceptionResponse):
# return values
return WriteSingleCoilResponse(self.address, values[0])

def get_response_pdu_size(self):
Expand Down
5 changes: 2 additions & 3 deletions pymodbus/datastore/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ def setValues(self, fc_as_hex, address, values):
self.result = func_fc(address, values)
else:
self.result = func_fc(address, values[0])
if self.result.isError():
return self.result
return None
# if self.result.isError():
# return self.result

def __str__(self):
"""Return a string representation of the context.
Expand Down
4 changes: 3 additions & 1 deletion pymodbus/datastore/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from datetime import datetime
from typing import Any, Callable

from pymodbus.datastore.context import ModbusBaseSlaveContext


WORD_SIZE = 16

Expand Down Expand Up @@ -370,7 +372,7 @@ def setup(self, config, custom_actions) -> None:
raise RuntimeError(f"INVALID key in setup: {self.config}")


class ModbusSimulatorContext:
class ModbusSimulatorContext(ModbusBaseSlaveContext):
"""Modbus simulator.
:param config: A dict with structure as shown below.
Expand Down
7 changes: 4 additions & 3 deletions test/test_remote_datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ def test_remote_slave_set_values(self):

context = RemoteSlaveContext(client)
context.setValues(0x0F, 0, [1])
result = context.setValues(0x10, 1, [1])
assert result.exception_code == 0x02
assert result.function_code == 0x90
# result = context.setValues(0x10, 1, [1])
context.setValues(0x10, 1, [1])
# assert result.exception_code == 0x02
# assert result.function_code == 0x90

def test_remote_slave_get_values(self):
"""Test getting values from a remote slave context."""
Expand Down

0 comments on commit 98fc477

Please sign in to comment.