diff --git a/pymodbus/framer/socket_framer.py b/pymodbus/framer/socket_framer.py index d0569cc6b..043d2b0d6 100644 --- a/pymodbus/framer/socket_framer.py +++ b/pymodbus/framer/socket_framer.py @@ -176,6 +176,7 @@ def _process(self, callback, tid, error=False): """Process incoming packets irrespective error condition.""" data = self.getRawFrame() if error else self.getFrame() if (result := self.decoder.decode(data)) is None: + self.resetFrame() raise ModbusIOException("Unable to decode request") if error and result.function_code < 0x80: raise InvalidMessageReceivedException(result) diff --git a/test/test_client_faulty_response.py b/test/test_client_faulty_response.py new file mode 100644 index 000000000..de15d4633 --- /dev/null +++ b/test/test_client_faulty_response.py @@ -0,0 +1,40 @@ +"""Test server working as slave on a multidrop RS485 line.""" +from unittest import mock + +import pytest + +from pymodbus.exceptions import ModbusIOException +from pymodbus.factory import ClientDecoder +from pymodbus.framer import ModbusSocketFramer + + +class TestFaultyResponses: + """Test that server works on a multidrop line.""" + + slaves = [0] + + good_frame = b"\x00\x01\x00\x00\x00\x05\x00\x03\x02\x00\x01" + + @pytest.fixture(name="framer") + def fixture_framer(self): + """Prepare framer.""" + return ModbusSocketFramer(ClientDecoder()) + + @pytest.fixture(name="callback") + def fixture_callback(self): + """Prepare dummy callback.""" + return mock.Mock() + + def test_ok_frame(self, framer, callback): + """Test ok frame.""" + framer.processIncomingPacket(self.good_frame, callback, self.slaves) + callback.assert_called_once() + + def test_faulty_frame1(self, framer, callback): + """Test ok frame.""" + faulty_frame = b"\x00\x04\x00\x00\x00\x05\x00\x03\x0a\x00\x04" + with pytest.raises(ModbusIOException): + framer.processIncomingPacket(faulty_frame, callback, self.slaves) + callback.assert_not_called() + framer.processIncomingPacket(self.good_frame, callback, self.slaves) + callback.assert_called_once() diff --git a/test/test_client_multidrop.py b/test/test_server_multidrop.py similarity index 100% rename from test/test_client_multidrop.py rename to test/test_server_multidrop.py