Skip to content

Commit

Permalink
Task Cancellation and CRC Errors
Browse files Browse the repository at this point in the history
Alternate solution for pymodbus-dev#356 and pymodbus-dev#360.

Changes the RTU to make the transaction ID as the unit ID instead of an ever incrementing number.

Previously this transaction ID was always 0 on the receiving end but was the unique transaction ID on sending.

As such the FIFO buffer made the most sense. By tying it to the unit ID, we can recover from failure modes such as: -
- Asyncio task cancellations (eg. timeouts) pymodbus-dev#360
- Skipped responses from slaves. (hangs on master pymodbus-dev#360)
- CRC Errors pymodbus-dev#356
- Busy response
  • Loading branch information
pazzarpj committed Dec 12, 2018
1 parent aef3e0a commit 9a9c91b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 1 addition & 4 deletions pymodbus/client/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ def __init__(self, framer, **kwargs):
:param framer: The modbus framer implementation to use
"""
self.framer = framer
if isinstance(self.framer, ModbusSocketFramer):
self.transaction = DictTransactionManager(self, **kwargs)
else:
self.transaction = FifoTransactionManager(self, **kwargs)
self.transaction = DictTransactionManager(self, **kwargs)
self._debug = False
self._debugfd = None

Expand Down
5 changes: 5 additions & 0 deletions pymodbus/framer/rtu_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def populateResult(self, result):
:param result: The response packet
"""
result.unit_id = self._header['uid']
result.transaction_id = self._header['uid']

# ----------------------------------------------------------------------- #
# Public Member Functions
Expand Down Expand Up @@ -221,6 +222,9 @@ def processIncomingPacket(self, data, callback, unit, **kwargs):
_logger.debug("Not a valid unit id - {}, "
"ignoring!!".format(self._header['uid']))
self.resetFrame()
else:
_logger.debug("Frame check failed, ignoring!!")
self.resetFrame()
else:
_logger.debug("Frame - [{}] not ready".format(data))

Expand All @@ -235,6 +239,7 @@ def buildPacket(self, message):
message.unit_id,
message.function_code) + data
packet += struct.pack(">H", computeCRC(packet))
message.transaction_id = message.unit_id # Ensure that transaction is actually the unit id for serial comms
return packet

def sendPacket(self, message):
Expand Down

0 comments on commit 9a9c91b

Please sign in to comment.