diff --git a/pymodbus/framer/old_framer_ascii.py b/pymodbus/framer/old_framer_ascii.py index 4854e1aff0..1dd12aa830 100644 --- a/pymodbus/framer/old_framer_ascii.py +++ b/pymodbus/framer/old_framer_ascii.py @@ -38,25 +38,25 @@ def __init__(self, decoder, client=None): self._end = b"\r\n" self.message_handler = FramerAscii(decoder, [0]) - def frameProcessIncomingPacket(self, single, callback, slave, tid=None): + def frameProcessIncomingPacket(self, single, callback, slave, _tid): """Process new packet pattern.""" - while len(self._buffer): - used_len, data = self.message_handler.decode(self._buffer) - if not data: - if not used_len: - return - self._buffer = self._buffer[used_len :] - continue - self.dev_id = self.message_handler.incoming_dev_id - if not self._validate_slave_id(slave, single): - Log.error("Not a valid slave id - {}, ignoring!!", self.message_handler.incoming_dev_id) - self.resetFrame() - return - - if (result := self.decoder.decode(data)) is None: - raise ModbusIOException("Unable to decode response") - result.slave_id = self.dev_id - result.transaction_id = self.tid + used_len, data = self.message_handler.decode(self._buffer) + if not data: + if not used_len: + return False self._buffer = self._buffer[used_len :] - self.dev_id = 0 - callback(result) # defer this + return True + self.dev_id = self.message_handler.incoming_dev_id + if not self._validate_slave_id(slave, single): + Log.error("Not a valid slave id - {}, ignoring!!", self.message_handler.incoming_dev_id) + self.resetFrame() + return False + + if (result := self.decoder.decode(data)) is None: + raise ModbusIOException("Unable to decode response") + result.slave_id = self.dev_id + result.transaction_id = self.tid + self._buffer = self._buffer[used_len :] + self.dev_id = 0 + callback(result) # defer this + return True diff --git a/pymodbus/framer/old_framer_base.py b/pymodbus/framer/old_framer_base.py index 87e7e2b5e8..4544514ef4 100644 --- a/pymodbus/framer/old_framer_base.py +++ b/pymodbus/framer/old_framer_base.py @@ -121,12 +121,16 @@ def processIncomingPacket(self, data: bytes, callback, slave, single=False, tid= return if not isinstance(slave, (list, tuple)): slave = [slave] - self.frameProcessIncomingPacket(single, callback, slave, tid=tid) + while True: + if self._buffer == b'': + return + if not self.frameProcessIncomingPacket(single, callback, slave, tid): + return def frameProcessIncomingPacket( - self, _single, _callback, _slave, tid=None - ) -> None: + self, _single, _callback, _slave, _tid) -> bool: """Process new packet pattern.""" + return True def buildPacket(self, message: ModbusRequest | ModbusResponse) -> bytes: """Create a ready to send modbus packet. diff --git a/pymodbus/framer/old_framer_rtu.py b/pymodbus/framer/old_framer_rtu.py index c7c65a0ea2..edefdb4c3b 100644 --- a/pymodbus/framer/old_framer_rtu.py +++ b/pymodbus/framer/old_framer_rtu.py @@ -55,23 +55,21 @@ def __init__(self, decoder, client=None): super().__init__(decoder, client) self.message_handler: FramerRTU = FramerRTU(self.decoder, [0]) - def frameProcessIncomingPacket(self, _single, callback, _slave, tid=None): + def frameProcessIncomingPacket(self, _single, callback, _slave, _tid): """Process new packet pattern.""" - while True: - if self._buffer == b'': - break - used_len, data = self.message_handler.decode(self._buffer) - self.dev_id = self.message_handler.incoming_dev_id - if used_len: - self._buffer = self._buffer[used_len:] - if not data: - break - if (result := self.decoder.decode(data)) is None: - raise ModbusIOException("Unable to decode request") - result.slave_id = self.dev_id - result.transaction_id = 0 - Log.debug("Frame advanced, resetting header!!") - callback(result) # defer or push to a thread? + used_len, data = self.message_handler.decode(self._buffer) + self.dev_id = self.message_handler.incoming_dev_id + if used_len: + self._buffer = self._buffer[used_len:] + if not data: + return False + if (result := self.decoder.decode(data)) is None: + raise ModbusIOException("Unable to decode request") + result.slave_id = self.dev_id + result.transaction_id = 0 + Log.debug("Frame advanced, resetting header!!") + callback(result) # defer or push to a thread? + return True def sendPacket(self, message: bytes) -> int: """Send packets on the bus with 3.5char delay between frames. diff --git a/pymodbus/framer/old_framer_socket.py b/pymodbus/framer/old_framer_socket.py index eae99b517e..bae2a996ca 100644 --- a/pymodbus/framer/old_framer_socket.py +++ b/pymodbus/framer/old_framer_socket.py @@ -41,7 +41,7 @@ def __init__(self, decoder, client=None): self._hsize = 0x07 self.message_handler = FramerSocket(decoder, [0]) - def frameProcessIncomingPacket(self, single, callback, slave, tid=None): + def frameProcessIncomingPacket(self, single, callback, slave, tid): """Process new packet pattern. This takes in a new request packet, adds it to the current @@ -53,25 +53,23 @@ def frameProcessIncomingPacket(self, single, callback, slave, tid=None): The processed and decoded messages are pushed to the callback function to process and send. """ - while True: - if self._buffer == b'': - return - used_len, data = self.message_handler.decode(self._buffer) - if not data: - return - self.dev_id = self.message_handler.incoming_dev_id - self.tid = self.message_handler.incoming_tid - if not self._validate_slave_id(slave, single): - Log.debug("Not a valid slave id - {}, ignoring!!", self.message_handler.incoming_dev_id) - self.resetFrame() - return - if (result := self.decoder.decode(data)) is None: - self.resetFrame() - raise ModbusIOException("Unable to decode request") - result.slave_id = self.dev_id - result.transaction_id = self.tid - self._buffer: bytes = self._buffer[used_len:] - if tid and tid != result.transaction_id: - self.resetFrame() - else: - callback(result) # defer or push to a thread? + used_len, data = self.message_handler.decode(self._buffer) + if not data: + return False + self.dev_id = self.message_handler.incoming_dev_id + self.tid = self.message_handler.incoming_tid + if not self._validate_slave_id(slave, single): + Log.debug("Not a valid slave id - {}, ignoring!!", self.message_handler.incoming_dev_id) + self.resetFrame() + return True + if (result := self.decoder.decode(data)) is None: + self.resetFrame() + raise ModbusIOException("Unable to decode request") + result.slave_id = self.dev_id + result.transaction_id = self.tid + self._buffer: bytes = self._buffer[used_len:] + if tid and tid != result.transaction_id: + self.resetFrame() + else: + callback(result) # defer or push to a thread? + return True diff --git a/pymodbus/framer/old_framer_tls.py b/pymodbus/framer/old_framer_tls.py index a013609929..b7e3ae0129 100644 --- a/pymodbus/framer/old_framer_tls.py +++ b/pymodbus/framer/old_framer_tls.py @@ -32,21 +32,21 @@ def __init__(self, decoder, client=None): self._hsize = 0x0 self.message_handler = FramerTLS(decoder, [0]) - def frameProcessIncomingPacket(self, _single, callback, _slave, tid=None): + def frameProcessIncomingPacket(self, _single, callback, _slave, _tid): """Process new packet pattern.""" # no slave id for Modbus Security Application Protocol - while True: - used_len, data = self.message_handler.decode(self._buffer) - if not data: - return - self.dev_id = self.message_handler.incoming_dev_id - self.tid = self.message_handler.incoming_tid - - if (result := self.decoder.decode(data)) is None: - self.resetFrame() - raise ModbusIOException("Unable to decode request") - result.slave_id = self.dev_id - result.transaction_id = self.tid - self._buffer: bytes = self._buffer[used_len:] - callback(result) # defer or push to a thread? + used_len, data = self.message_handler.decode(self._buffer) + if not data: + return False + self.dev_id = self.message_handler.incoming_dev_id + self.tid = self.message_handler.incoming_tid + + if (result := self.decoder.decode(data)) is None: + self.resetFrame() + raise ModbusIOException("Unable to decode request") + result.slave_id = self.dev_id + result.transaction_id = self.tid + self._buffer: bytes = self._buffer[used_len:] + callback(result) # defer or push to a thread? + return True