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: Check CRC in exception ADU #118

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: 8 additions & 1 deletion umodbus/client/serial/rtu.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import struct

from umodbus.client.serial.redundancy_check import get_crc, validate_crc
from umodbus.client.serial.redundancy_check import CRCError
from umodbus.functions import (create_function_from_response_pdu,
expected_response_pdu_size_from_request_pdu,
pdu_to_function_code_or_raise_error, ReadCoils,
Expand Down Expand Up @@ -199,7 +200,13 @@ def raise_for_exception_adu(resp_adu):
:raises ModbusError: When a response contains an error code.
"""
resp_pdu = resp_adu[1:-2]
pdu_to_function_code_or_raise_error(resp_pdu)
try:
validate_crc(resp_adu)
pdu_to_function_code_or_raise_error(resp_pdu)
except CRCError:
# The response cannot be trusted at all, so ignore any
# possibly invalid function code.
pass


def send_message(adu, serial_port):
Expand Down
5 changes: 2 additions & 3 deletions umodbus/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,10 @@


def pdu_to_function_code_or_raise_error(resp_pdu):
""" Parse response PDU and return of :class:`ModbusFunction` or
raise error.
"""Parse response PDU and return function code or raise error.

:param resp_pdu: PDU of response.
:return: Subclass of :class:`ModbusFunction` matching the response.
:return: Function code contained in the response.
:raises ModbusError: When response contains error code.
"""
function_code = struct.unpack('>B', resp_pdu[0:1])[0]
Expand Down