From b21aa1b0759bfc6a33db4b6e13a905f5a4bc6763 Mon Sep 17 00:00:00 2001 From: sanjay Date: Fri, 20 Nov 2015 12:40:12 -0800 Subject: [PATCH] version bumped to c93103 --- pymodbus/transaction.py | 50 ++++++++++++++++++++++++++++++++--------- pymodbus/version.py | 2 +- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/pymodbus/transaction.py b/pymodbus/transaction.py index 2c5b393b4..71cc79b3d 100644 --- a/pymodbus/transaction.py +++ b/pymodbus/transaction.py @@ -343,7 +343,6 @@ def processIncomingPacket(self, data, callback): :param data: The new packet data :param callback: The function to send results to ''' - _logger.debug(" ".join([hex(ord(x)) for x in data])) self.addToFrame(data) while True: if self.isFrameReady(): @@ -474,7 +473,11 @@ def advanceFrame(self): it or determined that it contains an error. It also has to reset the current frame header handle ''' - self.__buffer = self.__buffer[self.__header['len']:] + try: + self.__buffer = self.__buffer[self.__header['len']:] + except KeyError: + # Error response, no header len found + self.resetFrame() self.__header = {} def resetFrame(self): @@ -563,15 +566,21 @@ def processIncomingPacket(self, data, callback): :param callback: The function to send results to ''' self.addToFrame(data) - while self.isFrameReady(): - if self.checkFrame(): - result = self.decoder.decode(self.getFrame()) - if result is None: - raise ModbusIOException("Unable to decode response") - self.populateResult(result) - self.advanceFrame() - callback(result) # defer or push to a thread? - else: self.resetFrame() # clear possible errors + while True: + if self.isFrameReady(): + if self.checkFrame(): + self._process(callback) + else: + # Could be an error response + if len(self.__buffer): + # Possible error ??? + self._process(callback, error=True) + else: + if len(self.__buffer): + # Possible error ??? + if self.__header['len'] < 2: + self._process(callback, error=True) + break def buildPacket(self, message): ''' Creates a ready to send modbus packet @@ -585,6 +594,25 @@ def buildPacket(self, message): packet += struct.pack(">H", computeCRC(packet)) return packet + def _process(self, callback, error=False): + """ + Process incoming packets irrespective error condition + """ + data = self.getRawFrame() if error else self.getFrame() + result = self.decoder.decode(data) + if result is None: + raise ModbusIOException("Unable to decode request") + self.populateResult(result) + self.advanceFrame() + callback(result) # defer or push to a thread? + + def getRawFrame(self): + """ + Returns the complete buffer + """ + return self.__buffer + + #---------------------------------------------------------------------------# # Modbus ASCII Message diff --git a/pymodbus/version.py b/pymodbus/version.py index 421863b0a..6b93b6977 100644 --- a/pymodbus/version.py +++ b/pymodbus/version.py @@ -36,7 +36,7 @@ def __str__(self): ''' return '[%s, version %s]' % (self.package, self.short()) -version = Version('pymodbus', 1, 3, 0, "rc93102") +version = Version('pymodbus', 1, 3, 0, "rc93103") version.__name__ = 'pymodbus' # fix epydoc error #---------------------------------------------------------------------------#