Skip to content

Commit

Permalink
Fix #387 Transactions failing on 2.2.0rc2.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoomakethu committed Feb 16, 2019
1 parent f66f464 commit 7650421
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ from pymodbus.client.asynchronous import ModbusTcpClient
* Support broadcasting in Modbus Client and Servers (sync).
* Fix asyncio examples.
* Improved logging in Modbus Server .
* Fix regression introduced in 2.2.0rc2 (Modbus sync client transaction failing)
* Minor update in factory.py, now server logs prints received request instead of only function code

```
Expand Down
13 changes: 10 additions & 3 deletions pymodbus/server/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,21 @@ def handle(self):
reset_frame = False
while self.running:
try:
units = self.server.context.slaves()
data = self.request.recv(1024)
if not data:
self.running = False
else:
if not isinstance(units, (list, tuple)):
units = [units]
# if broadcast is enabled make sure to
# process requests to address 0
if self.server.broadcast_enable:
if 0 not in units:
units.append(0)

if _logger.isEnabledFor(logging.DEBUG):
_logger.debug('Handling data: ' + hexlify_packets(data))
# if not self.server.control.ListenOnly:

units = self.server.context.slaves()
single = self.server.context.single
self.framer.processIncomingPacket(data, self.execute, units,
single=single)
Expand Down
45 changes: 25 additions & 20 deletions pymodbus/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,17 @@ def execute(self, request):
)
retries = self.retries
request.transaction_id = self.getNextTID()
_logger.debug("Running transaction %d" % request.transaction_id)
_logger.debug("Running transaction "
"{}".format(request.transaction_id))
_buffer = hexlify_packets(self.client.framer._buffer)
if _buffer:
_logger.debug("Clearing current Frame : - {}".format(_buffer))
_logger.debug("Clearing current Frame "
": - {}".format(_buffer))
self.client.framer.resetFrame()

if request.unit_id == 0 and self.client.broadcast_enable:
response, last_exception = self._transact(request, None)
broadcast = (self.client.broadcast_enable
and request.unit_id == 0)
if broadcast:
self._transact(request, None, broadcast=True)
response = b'Broadcast write sent - no response expected'
else:
expected_response_length = None
Expand All @@ -139,10 +142,12 @@ def execute(self, request):
full = True
if not expected_response_length:
expected_response_length = Defaults.ReadSize
response, last_exception = self._transact(request,
expected_response_length,
full=full
)
response, last_exception = self._transact(
request,
expected_response_length,
full=full,
broadcast=broadcast
)
if not response and (
request.unit_id not in self._no_response_devices):
self._no_response_devices.append(request.unit_id)
Expand Down Expand Up @@ -193,7 +198,7 @@ def execute(self, request):
self.client.state = ModbusTransactionState.TRANSACTION_COMPLETE
return ex

def _transact(self, packet, response_length, full=False):
def _transact(self, packet, response_length, full=False, broadcast=False):
"""
Does a Write and Read transaction
:param packet: packet to be sent
Expand All @@ -209,20 +214,20 @@ def _transact(self, packet, response_length, full=False):
if _logger.isEnabledFor(logging.DEBUG):
_logger.debug("SEND: " + hexlify_packets(packet))
size = self._send(packet)
if response_length is not None:
if size:
_logger.debug("Changing transaction state from 'SENDING' "
"to 'WAITING FOR REPLY'")
self.client.state = ModbusTransactionState.WAITING_FOR_REPLY
result = self._recv(response_length, full)
if _logger.isEnabledFor(logging.DEBUG):
_logger.debug("RECV: " + hexlify_packets(result))
else:
if broadcast:
if size:
_logger.debug("Changing transaction state from 'SENDING' "
"to 'TRANSACTION_COMPLETE'")
self.client.state = ModbusTransactionState.TRANSACTION_COMPLETE
result = b''
return b'', None
if size:
_logger.debug("Changing transaction state from 'SENDING' "
"to 'WAITING FOR REPLY'")
self.client.state = ModbusTransactionState.WAITING_FOR_REPLY
result = self._recv(response_length, full)
if _logger.isEnabledFor(logging.DEBUG):
_logger.debug("RECV: " + hexlify_packets(result))

except (socket.error, ModbusIOException,
InvalidMessageReceivedException) as msg:
self.client.close()
Expand Down
2 changes: 1 addition & 1 deletion pymodbus/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __str__(self):
return '[%s, version %s]' % (self.package, self.short())


version = Version('pymodbus', 2, 2, 0, "rc2")
version = Version('pymodbus', 2, 2, 0, "rc3")


version.__name__ = 'pymodbus' # fix epydoc error
Expand Down

0 comments on commit 7650421

Please sign in to comment.