Skip to content

Commit

Permalink
Merged in patch-fix-101-for-serial-modbus-version-bumped (pull request
Browse files Browse the repository at this point in the history
…#2)

version bumped to c93103
  • Loading branch information
Mark Evans committed Nov 20, 2015
2 parents 0caf27f + 612fc84 commit 3161897
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
50 changes: 39 additions & 11 deletions pymodbus/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pymodbus/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

#---------------------------------------------------------------------------#
Expand Down

0 comments on commit 3161897

Please sign in to comment.