Skip to content

Commit

Permalink
Workaround for bug 101 #101
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoomakethu committed Oct 16, 2015
1 parent eb7bbaa commit 69639ac
Showing 1 changed file with 41 additions and 9 deletions.
50 changes: 41 additions & 9 deletions pymodbus/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,48 @@ 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 self.isFrameReady():
if self.checkFrame():
result = self.decoder.decode(self.getFrame())
if result is None:
raise ModbusIOException("Unable to decode request")
self.populateResult(result)
self.advanceFrame()
callback(result) # defer or push to a thread?
else: break
while True:
if self.isFrameReady():
if self.checkFrame():
self._process(callback)
else: self.resetFrame()
else:
if len(self.__buffer):
# Possible error ???
if self.__header['len'] < 2:
self._process(callback, error=True)
break

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 resetFrame(self):
''' Reset the entire message frame.
This allows us to skip ovver errors that may be in the stream.
It is hard to know if we are simply out of sync or if there is
an error in the stream as we have no way to check the start or
end of the message (python just doesn't have the resolution to
check for millisecond delays).
'''
self.__buffer = ''
self.__header = {}

def getRawFrame(self):
"""
Returns the complete buffer
"""
return self.__buffer

def buildPacket(self, message):
''' Creates a ready to send modbus packet
Expand Down

0 comments on commit 69639ac

Please sign in to comment.