From 31dd581438f67f724286191fb0abc0e3b9da4d81 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Mon, 11 Nov 2024 16:04:30 +0100 Subject: [PATCH] RTU frame problem, when received split. (#2452) --- pymodbus/framer/rtu.py | 4 ++-- test/framer/test_framer.py | 8 ++++---- test/framer/test_multidrop.py | 18 ++++++++++++++++++ test/sub_client/test_client_faulty_response.py | 2 +- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/pymodbus/framer/rtu.py b/pymodbus/framer/rtu.py index 28ba1ff52..b19876d90 100644 --- a/pymodbus/framer/rtu.py +++ b/pymodbus/framer/rtu.py @@ -103,7 +103,7 @@ def decode(self, data: bytes) -> tuple[int, int, int, bytes]: for used_len in range(data_len): if data_len - used_len < self.MIN_SIZE: Log.debug("Short frame: {} wait for more data", data, ":hex") - return used_len, 0, 0, self.EMPTY + return 0, 0, 0, self.EMPTY dev_id = int(data[used_len]) if not (pdu_class := self.decoder.lookupPduClass(data[used_len:])): continue @@ -111,7 +111,7 @@ def decode(self, data: bytes) -> tuple[int, int, int, bytes]: size = data_len +1 if data_len < used_len +size: Log.debug("Frame - not ready") - return used_len, dev_id, 0, self.EMPTY + return 0, dev_id, 0, self.EMPTY for test_len in range(data_len, used_len + size - 1, -1): start_crc = test_len -2 crc = data[start_crc : start_crc + 2] diff --git a/test/framer/test_framer.py b/test/framer/test_framer.py index 20dde385b..5838e3d8d 100644 --- a/test/framer/test_framer.py +++ b/test/framer/test_framer.py @@ -327,19 +327,19 @@ async def test_decode_type(self, entry, test_framer, data, dev_id, tr_id, expect (9, b'\x83\x02'), ]), (FramerType.RTU, b'\x00\x83\x02\x91\x21', [ # bad crc - (1, b''), + (0, b''), (0, b''), ]), (FramerType.RTU, b'\x00\x83\x02\xf0\x91\x31', [ # dummy char in stream, bad crc - (1, b''), + (0, b''), (0, b''), ]), (FramerType.RTU, b'\x00\x83\x02\x91\x21\x00\x83\x02\x91\x31', [ # bad crc + good CRC - (1, b''), + (0, b''), (0, b''), ]), (FramerType.RTU, b'\x00\x83\x02\xf0\x91\x31\x00\x83\x02\x91\x31', [ # dummy char in stream, bad crc + good CRC - (1, b''), + (0, b''), (0, b''), ]), ] diff --git a/test/framer/test_multidrop.py b/test/framer/test_multidrop.py index 19a42bb43..736572935 100644 --- a/test/framer/test_multidrop.py +++ b/test/framer/test_multidrop.py @@ -165,3 +165,21 @@ def test_getFrameStart(self, framer): used_len, pdu = framer.processIncomingFrame(framer_2ok) assert pdu assert used_len == len(framer_2ok) + + def test_rtu_split_frame(self, framer): + """Test test_rtu_split_frame.""" + msg1 = (b'\x01\x08\x00\x00\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb' + b'\xcc\xdd\xee\xff\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99') + msg2 = (b'\xaa\xbb\xcc\xdd\xee\xff\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99' + b'\xaa\xbb\xcc\xdd\xee\xff\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99' + b'\xaa\xbb\xcc\xdd\xee\xff\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99' + b'\xaa\xbb\xcc\xdd\xee\xff\xe7\x65') + used_len, pdu = framer.processIncomingFrame(msg1+msg2) + assert pdu + assert used_len == len(msg1 + msg2) + used_len, pdu = framer.processIncomingFrame(msg1) + assert not pdu + assert not used_len + used_len, pdu = framer.processIncomingFrame(msg1+msg2) + assert pdu + assert used_len == len(msg1 + msg2) diff --git a/test/sub_client/test_client_faulty_response.py b/test/sub_client/test_client_faulty_response.py index 8beaf4c6d..2974f7773 100644 --- a/test/sub_client/test_client_faulty_response.py +++ b/test/sub_client/test_client_faulty_response.py @@ -29,7 +29,7 @@ def test_1917_frame(self): framer = FramerRTU(DecodePDU(False)) used_len, pdu = framer.processIncomingFrame(recv) assert not pdu - assert used_len + assert not used_len def test_faulty_frame1(self, framer): """Test ok frame."""