Skip to content
Merged
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
3 changes: 2 additions & 1 deletion pymodbus/framer/rtu.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def decode(self, data: bytes) -> tuple[int, int, int, bytes]:
if not (pdu_class := self.decoder.lookupPduClass(data[used_len:])):
continue
if not (size := pdu_class.calculateRtuFrameSize(data[used_len:])):
size = data_len +1
Log.debug("Frame - rtu_byte_count_pos wrong")
return 0, dev_id, 0, self.EMPTY
if data_len < used_len +size:
Log.debug("Frame - not ready")
return 0, dev_id, 0, self.EMPTY
Expand Down
12 changes: 12 additions & 0 deletions pymodbus/pdu/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
"""Framer."""
__all__ = [
"DecodePDU",
"DiagnosticBase",
"ExceptionResponse",
"FileRecord",
"ModbusPDU",
"ReadCoilsRequest",
"ReadDeviceInformationRequest",
"ReadExceptionStatusRequest",
"ReadHoldingRegistersRequest",
]

from .bit_message import ReadCoilsRequest
from .decoders import DecodePDU
from .diag_message import DiagnosticBase
from .exceptionresponse import ExceptionResponse
from .file_message import FileRecord
from .mei_message import ReadDeviceInformationRequest
from .other_message import ReadExceptionStatusRequest
from .pdu import ModbusPDU
from .register_message import ReadHoldingRegistersRequest


52 changes: 51 additions & 1 deletion test/framer/test_extras.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""Test transaction."""
from unittest.mock import patch

import pytest

from pymodbus.exceptions import ModbusIOException
from pymodbus.framer import (
FramerAscii,
FramerRTU,
Expand All @@ -12,7 +16,7 @@
TEST_MESSAGE = b"\x7b\x01\x03\x00\x00\x00\x05\x85\xC9\x7d"


class TestExtas:
class TestExtras:
"""Test for the framer module."""

client = None
Expand Down Expand Up @@ -73,6 +77,26 @@ def test_tcp_framer_transaction_short(self):
assert used_len == len(msg1) + len(msg2)
assert pdu.function_code.to_bytes(1,'big') + pdu.encode() == msg2[7:]

def test_tcp_framer_transaction_wrong_id(self):
"""Test a half completed tcp frame transaction."""
msg = b"\x00\x01\x12\x34\x00\x06\xff\x02\x01\x02\x00\x08"
used_len, pdu = self._tcp.handleFrame(msg, 1, 0)
assert not pdu
assert used_len == len(msg)

def test_tcp_framer_transaction_wrong_tid(self):
"""Test a half completed tcp frame transaction."""
msg = b"\x00\x01\x12\x34\x00\x06\xff\x02\x01\x02\x00\x08"
used_len, pdu = self._tcp.handleFrame(msg, 0, 10)
assert not pdu
assert used_len == len(msg)

def test_tcp_framer_transaction_wrong_fc(self):
"""Test a half completed tcp frame transaction."""
msg = b"\x00\x01\x12\x34\x00\x06\xff\x70\x01\x02\x00\x08"
with pytest.raises(ModbusIOException):
self._tcp.handleFrame(msg, 0, 0)

def test_tls_incoming_packet(self):
"""Framer tls incoming packet."""
msg = b"\x00\x01\x12\x34\x00\x06\xff\x02\x01\x02\x00\x08"
Expand All @@ -85,6 +109,32 @@ def test_rtu_process_incoming_packets(self):
_, pdu = self._rtu.handleFrame(msg, 0, 0)
assert pdu

def test_rtu_short_packets(self):
"""Test rtu process incoming packets."""
msg1 = b"\x00\x01"
msg2 = b"\x00\x00\x00\x01\xfc\x1b"
used_len, pdu = self._rtu.handleFrame(msg1, 0, 0)
assert not used_len
assert not pdu
used_len, pdu = self._rtu.handleFrame(msg1+msg2, 0, 0)
assert used_len == len(msg1) + len(msg2)
assert pdu

def test_rtu_calculate(self):
"""Test rtu process incoming packets."""
msg = b"\x00\x01\x00\x00\x00\x01\xfc\x1b"
with patch("pymodbus.pdu.ReadCoilsRequest.calculateRtuFrameSize", return_value=0):
used_len, pdu = self._rtu.handleFrame(msg, 0, 0)
assert not used_len
assert not pdu

def test_rtu_wrong_fc(self):
"""Test rtu process incoming packets."""
msg = b"\x00\x70\x00\x00\x00\x71\xfc\x1b"
used_len, pdu = self._rtu.handleFrame(msg, 0, 0)
assert not pdu
assert not used_len

def test_ascii_process_incoming_packets(self):
"""Test ascii process incoming packet."""
msg = b":F7031389000A60\r\n"
Expand Down
4 changes: 0 additions & 4 deletions test/framer/test_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,3 @@ def test_framer_encode(self, test_framer, msg):

actual = test_framer.buildFrame(message)
assert msg == actual



# @pytest.mark.parametrize(("entry"), list(FramerType))