Skip to content

Commit

Permalink
#36 Call routes with function_code parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
OrangeTux committed May 4, 2016
1 parent 00e58a2 commit 2131456
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.rst
Expand Up @@ -51,13 +51,13 @@ Creating a Modbus server is easy:
@app.route(slave_ids=[1], function_codes=[3, 4], addresses=list(range(0, 10)))
def read_data_store(slave_id, address):
def read_data_store(slave_id, function_code, address):
"""" Return value of address. """
return data_store[address]
@app.route(slave_ids=[1], function_codes=[6, 16], addresses=list(range(0, 10)))
def write_data_store(slave_id, address, value):
def write_data_store(slave_id, function_code, address, value):
"""" Set value for address. """
data_store[address] = value
Expand Down Expand Up @@ -94,7 +94,7 @@ Doing a Modbus request requires even less code:
message = tcp.write_multiple_coils(slave_id=1, starting_address=1, values=[1, 0, 1, 1])
# Response depends on Modbus function code. This particular returns the
# amount of coils written, in this case it isr3.
# amount of coils written, in this case it is.
response = tcp.send_message(message, sock)
sock.close()
Expand Down
2 changes: 1 addition & 1 deletion scripts/examples/simple_client.py
Expand Up @@ -16,7 +16,7 @@
message = tcp.write_multiple_coils(slave_id=1, starting_address=1, values=[1, 0, 1, 1])

# Response depends on Modbus function code. This particular returns the
# amount of coils written, in this case it isr3.
# amount of coils written, in this case it is.
response = tcp.send_message(message, sock)

sock.close()
4 changes: 2 additions & 2 deletions scripts/examples/simple_data_store.py
Expand Up @@ -21,13 +21,13 @@


@app.route(slave_ids=[1], function_codes=[1, 2], addresses=list(range(0, 10)))
def read_data_store(slave_id, address):
def read_data_store(slave_id, function_code, address):
"""" Return value of address. """
return data_store[address]


@app.route(slave_ids=[1], function_codes=[5, 15], addresses=list(range(0, 10)))
def write_data_store(slave_id, address, value):
def write_data_store(slave_id, function_code, address, value):
"""" Set value for address. """
data_store[address] = value

Expand Down
8 changes: 4 additions & 4 deletions tests/system/server.py
Expand Up @@ -11,22 +11,22 @@


@app.route(slave_ids=[1], function_codes=[1, 2], addresses=list(range(0, 10)))
def read_status(slave_id, address):
def read_status(slave_id, function_code, address):
return address % 2


@app.route(slave_ids=[1], function_codes=[3, 4], addresses=list(range(0, 10)))
def read_register(slave_id, address):
def read_register(slave_id, function_code, address):
return -address


@app.route(slave_ids=[1], function_codes=[5, 15], addresses=list(range(0, 10)))
def write_status(slave_id, address, value):
def write_status(slave_id, function_code, address, value):
pass


@app.route(slave_ids=[1], function_codes=[6, 16], addresses=list(range(0, 10)))
def write_register(slave_id, address, value):
def write_register(slave_id, function_code, address, value):
pass


Expand Down
26 changes: 15 additions & 11 deletions tests/unit/test_functions.py
Expand Up @@ -140,7 +140,7 @@ def test_execute(self, read_coils, route_map, monkeypatch):
correct result.
"""
def match_mock(*args, **kwargs):
return lambda slave_id, address: address % 2
return lambda slave_id, function_code, address: address % 2

monkeypatch.setattr(route_map, 'match', match_mock)
assert read_coils.execute(1, route_map) == [0, 1, 0]
Expand All @@ -167,6 +167,7 @@ def match_mock(*args, **kwargs):
write_single_coil.execute(1, route_map)

endpoint_mock.assert_called_once_with(slave_id=1,
function_code=write_single_coil.function_code, # NOQA
address=write_single_coil.address, # NOQA
value=write_single_coil.value)

Expand Down Expand Up @@ -208,18 +209,21 @@ def match_mock(*args, **kwargs):
monkeypatch.setattr(route_map, 'match', match_mock)
write_multiple_coils.execute(1, route_map)

# Short cut to prevent following lines growing longer than 80
# characters.
fn_code = write_multiple_coils.function_code
assert endpoint_mock.call_count == 10
calls = [
call(slave_id=1, address=100, value=0),
call(slave_id=1, address=101, value=1),
call(slave_id=1, address=102, value=0),
call(slave_id=1, address=103, value=0),
call(slave_id=1, address=104, value=0),
call(slave_id=1, address=105, value=0),
call(slave_id=1, address=106, value=0),
call(slave_id=1, address=107, value=0),
call(slave_id=1, address=108, value=1),
call(slave_id=1, address=109, value=0),
call(slave_id=1, function_code=fn_code, address=100, value=0),
call(slave_id=1, function_code=fn_code, address=101, value=1),
call(slave_id=1, function_code=fn_code, address=102, value=0),
call(slave_id=1, function_code=fn_code, address=103, value=0),
call(slave_id=1, function_code=fn_code, address=104, value=0),
call(slave_id=1, function_code=fn_code, address=105, value=0),
call(slave_id=1, function_code=fn_code, address=106, value=0),
call(slave_id=1, function_code=fn_code, address=107, value=0),
call(slave_id=1, function_code=fn_code, address=108, value=1),
call(slave_id=1, function_code=fn_code, address=109, value=0),
]
endpoint_mock.assert_has_calls(calls)

Expand Down
9 changes: 6 additions & 3 deletions umodbus/functions.py
Expand Up @@ -87,7 +87,8 @@ def execute(self, slave_id, route_map):
self.starting_address + self.quantity):
endpoint = route_map.match(slave_id, self.function_code,
address)
values.append(endpoint(slave_id=slave_id, address=address))
values.append(endpoint(slave_id=slave_id, address=address,
function_code=self.function_code))

return values
# route_map.match() returns None if no match is found. Calling None
Expand All @@ -110,7 +111,8 @@ def execute(self, slave_id, route_map):
"""
endpoint = route_map.match(slave_id, self.function_code, self.address)
try:
endpoint(slave_id=slave_id, address=self.address, value=self.value)
endpoint(slave_id=slave_id, address=self.address, value=self.value,
function_code=self.function_code)
# route_map.match() returns None if no match is found. Calling None
# results in TypeError.
except TypeError:
Expand Down Expand Up @@ -146,7 +148,8 @@ def execute(self, slave_id, route_map):
endpoint = route_map.match(slave_id, self.function_code, address)

try:
endpoint(slave_id=slave_id, address=address, value=value)
endpoint(slave_id=slave_id, address=address, value=value,
function_code=self.function_code)
# route_map.match() returns None if no match is found. Calling None
# results in TypeError.
except TypeError:
Expand Down

0 comments on commit 2131456

Please sign in to comment.