From 2e50928976cddb109bc388467de58e5ac8cc3693 Mon Sep 17 00:00:00 2001 From: Gianluigi Tiesi Date: Thu, 9 Jul 2020 16:41:42 +0200 Subject: [PATCH] better fix for rtu incomplete frames - avoid multiple calls to populateHeader() - trap IndexError in calculateRtuFrameSize() - more readable --- pymodbus/framer/rtu_framer.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/pymodbus/framer/rtu_framer.py b/pymodbus/framer/rtu_framer.py index 2cc8fe164..39af0a325 100644 --- a/pymodbus/framer/rtu_framer.py +++ b/pymodbus/framer/rtu_framer.py @@ -131,18 +131,17 @@ def isFrameReady(self): :returns: True if ready, False otherwise """ - if len(self._buffer) <= self._hsize: - return False - - try: - # Frame is ready only if populateHeader() successfully - # populates crc field which finishes RTU frame otherwise, - # if buffer is not yet long enough, populateHeader() raises IndexError - self.populateHeader() - except IndexError: - return False - - return True + size = self._header.get('len', 0) + if size == 0 and len(self._buffer) > self._hsize: + try: + # Frame is ready only if populateHeader() successfully + # populates crc field which finishes RTU frame otherwise, + # if buffer is not yet long enough, populateHeader() raises IndexError + size = self.populateHeader() + except IndexError: + return False + + return len(self._buffer) >= size if size > 0 else False def populateHeader(self, data=None): # pylint: disable=invalid-name """Try to set the headers `uid`, `len` and `crc`. @@ -164,6 +163,7 @@ def populateHeader(self, data=None): # pylint: disable=invalid-name # crc yet not available raise IndexError self._header["crc"] = data[size - 2 : size] + return size def addToFrame(self, message): """Add the received data to the buffer handle.