From 144cea2cecdd3a3ba3ec5024c389e45f569dc883 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Thu, 28 Mar 2024 11:46:06 +0100 Subject: [PATCH 1/3] Integrate message decode. --- .../{to_do_server_multidrop.py => server_multidrop_tbd.py} | 0 test/{sub_messages => sub_function_codes}/test_all_messages.py | 0 .../test_bit_read_messages.py | 0 .../test_bit_write_messages.py | 0 test/{sub_messages => sub_function_codes}/test_diag_messages.py | 0 test/{sub_messages => sub_function_codes}/test_mei_messages.py | 0 test/{sub_messages => sub_function_codes}/test_other_messages.py | 0 .../test_register_read_messages.py | 0 .../test_register_write_messages.py | 0 9 files changed, 0 insertions(+), 0 deletions(-) rename test/message/{to_do_server_multidrop.py => server_multidrop_tbd.py} (100%) rename test/{sub_messages => sub_function_codes}/test_all_messages.py (100%) rename test/{sub_messages => sub_function_codes}/test_bit_read_messages.py (100%) rename test/{sub_messages => sub_function_codes}/test_bit_write_messages.py (100%) rename test/{sub_messages => sub_function_codes}/test_diag_messages.py (100%) rename test/{sub_messages => sub_function_codes}/test_mei_messages.py (100%) rename test/{sub_messages => sub_function_codes}/test_other_messages.py (100%) rename test/{sub_messages => sub_function_codes}/test_register_read_messages.py (100%) rename test/{sub_messages => sub_function_codes}/test_register_write_messages.py (100%) diff --git a/test/message/to_do_server_multidrop.py b/test/message/server_multidrop_tbd.py similarity index 100% rename from test/message/to_do_server_multidrop.py rename to test/message/server_multidrop_tbd.py diff --git a/test/sub_messages/test_all_messages.py b/test/sub_function_codes/test_all_messages.py similarity index 100% rename from test/sub_messages/test_all_messages.py rename to test/sub_function_codes/test_all_messages.py diff --git a/test/sub_messages/test_bit_read_messages.py b/test/sub_function_codes/test_bit_read_messages.py similarity index 100% rename from test/sub_messages/test_bit_read_messages.py rename to test/sub_function_codes/test_bit_read_messages.py diff --git a/test/sub_messages/test_bit_write_messages.py b/test/sub_function_codes/test_bit_write_messages.py similarity index 100% rename from test/sub_messages/test_bit_write_messages.py rename to test/sub_function_codes/test_bit_write_messages.py diff --git a/test/sub_messages/test_diag_messages.py b/test/sub_function_codes/test_diag_messages.py similarity index 100% rename from test/sub_messages/test_diag_messages.py rename to test/sub_function_codes/test_diag_messages.py diff --git a/test/sub_messages/test_mei_messages.py b/test/sub_function_codes/test_mei_messages.py similarity index 100% rename from test/sub_messages/test_mei_messages.py rename to test/sub_function_codes/test_mei_messages.py diff --git a/test/sub_messages/test_other_messages.py b/test/sub_function_codes/test_other_messages.py similarity index 100% rename from test/sub_messages/test_other_messages.py rename to test/sub_function_codes/test_other_messages.py diff --git a/test/sub_messages/test_register_read_messages.py b/test/sub_function_codes/test_register_read_messages.py similarity index 100% rename from test/sub_messages/test_register_read_messages.py rename to test/sub_function_codes/test_register_read_messages.py diff --git a/test/sub_messages/test_register_write_messages.py b/test/sub_function_codes/test_register_write_messages.py similarity index 100% rename from test/sub_messages/test_register_write_messages.py rename to test/sub_function_codes/test_register_write_messages.py From 63de33ada5e8c15822194d2041a75e90464b74c2 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Thu, 28 Mar 2024 11:59:26 +0100 Subject: [PATCH 2/3] transport 100% coverage. --- test/transport/test_protocol.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/transport/test_protocol.py b/test/transport/test_protocol.py index e976c811a..7aafc3777 100644 --- a/test/transport/test_protocol.py +++ b/test/transport/test_protocol.py @@ -196,6 +196,12 @@ async def test_handle_local_echo_partial(self, client): assert client.recv_buffer == b"response" assert not client.sent_buffer + async def test_handle_no_transport(self, client): + """Test send().""" + client.transport = None + client.send(b"partial") + assert not client.sent_buffer + class TestTransportProtocol2: """Test transport module.""" From 84f71801f5361d5dca9fb5d5bcd0da75746fa1e6 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Thu, 28 Mar 2024 13:28:48 +0100 Subject: [PATCH 3/3] Ascii message decoder. --- pymodbus/framer/ascii_framer.py | 47 +++++++++------------------------ 1 file changed, 13 insertions(+), 34 deletions(-) diff --git a/pymodbus/framer/ascii_framer.py b/pymodbus/framer/ascii_framer.py index c564085e5..8ce7a67b4 100644 --- a/pymodbus/framer/ascii_framer.py +++ b/pymodbus/framer/ascii_framer.py @@ -1,6 +1,5 @@ """Ascii_framer.""" # pylint: disable=missing-type-doc -from binascii import a2b_hex from pymodbus.exceptions import ModbusIOException from pymodbus.framer.base import BYTE_ORDER, FRAME_HEADER, ModbusFramer @@ -52,44 +51,24 @@ def decode_data(self, data): def frameProcessIncomingPacket(self, single, callback, slave, _tid=None, **kwargs): """Process new packet pattern.""" - def check_frame(self): - """Check and decode the next frame.""" - start = self._buffer.find(self._start) - if start == -1: - return False - if start > 0: # go ahead and skip old bad data - self._buffer = self._buffer[start:] - start = 0 - - if (end := self._buffer.find(self._end)) != -1: - self._header["len"] = end - self._header["uid"] = int(self._buffer[1:3], 16) - self._header["lrc"] = int(self._buffer[end - 2 : end], 16) - data = a2b_hex(self._buffer[start + 1 : end - 2]) - return MessageAscii.check_LRC(data, self._header["lrc"]) - return False - - while len(self._buffer) > 1: - if not check_frame(self): - break + while len(self._buffer): + used_len, _tid, dev_id, data = self.message_handler.decode(self._buffer) + if not data: + if not used_len: + return + self._buffer = self._buffer[used_len :] + continue + self._header["uid"] = dev_id if not self._validate_slave_id(slave, single): - header_txt = self._header["uid"] - Log.error("Not a valid slave id - {}, ignoring!!", header_txt) + Log.error("Not a valid slave id - {}, ignoring!!", dev_id) self.resetFrame() - continue + return - start = self._hsize + 1 - end = self._header["len"] - 2 - buffer = self._buffer[start:end] - if end > 0: - frame = a2b_hex(buffer) - else: - frame = b"" - if (result := self.decoder.decode(frame)) is None: + if (result := self.decoder.decode(data)) is None: raise ModbusIOException("Unable to decode response") self.populateResult(result) - self._buffer = self._buffer[self._header["len"] + 2 :] - self._header = {"lrc": "0000", "len": 0, "uid": 0x00} + self._buffer = self._buffer[used_len :] + self._header = {"uid": 0x00} callback(result) # defer this def buildPacket(self, message):