Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTU: Raise exception for unexpected slave or function code in response #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions umodbus/client/serial/rtu.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
WriteSingleRegister, WriteMultipleCoils,
WriteMultipleRegisters)
from umodbus.utils import recv_exactly
from umodbus.exceptions import ModbusFrameError


def _create_request_adu(slave_id, req_pdu):
Expand Down Expand Up @@ -173,9 +174,9 @@ def write_multiple_registers(slave_id, starting_address, values):

def parse_response_adu(resp_adu, req_adu=None):
""" Parse response ADU and return response data. Some functions require
request ADU to fully understand request ADU.
request ADU to fully understand response ADU.

:param resp_adu: Resonse ADU.
:param resp_adu: Response ADU.
:param req_adu: Request ADU, default None.
:return: Response data.
"""
Expand Down Expand Up @@ -217,6 +218,10 @@ def send_message(adu, serial_port):
response_error_adu = recv_exactly(serial_port.read, exception_adu_size)
raise_for_exception_adu(response_error_adu)

if response_error_adu[0:2] != adu[0:2]:
# Mismatch in response's slave address or function code
raise ModbusFrameError(response_error_adu)

expected_response_size = \
expected_response_pdu_size_from_request_pdu(adu[1:-2]) + 3
response_remainder = recv_exactly(
Expand Down
6 changes: 6 additions & 0 deletions umodbus/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ class ModbusError(Exception):
pass


class ModbusFrameError(ModbusError):
"""Reply from an unexpected slave, or an error in the received frame."""

pass


class IllegalFunctionError(ModbusError):
""" The function code received in the request is not an allowable action for
the server.
Expand Down