From 1d4aa0a8dd77eb1a7bdd051ff20a0194c31efa50 Mon Sep 17 00:00:00 2001 From: bashwork Date: Sun, 14 Jul 2013 21:30:00 -0700 Subject: [PATCH] Fix for issue #21 --- pymodbus/constants.py | 7 +++++++ pymodbus/transaction.py | 9 +++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pymodbus/constants.py b/pymodbus/constants.py index e1d986998..4a8f6fd9b 100644 --- a/pymodbus/constants.py +++ b/pymodbus/constants.py @@ -20,6 +20,12 @@ class Defaults(Singleton): The default number of times a client should retry the given request before failing (3) + .. attribute:: RetryOnEmpty + + A flag indicating if a transaction should be retried in the + case that an empty response is received. This is useful for + slow clients that may need more time to process a requst. + .. attribute:: Timeout The default amount of time a client should wait for a request @@ -73,6 +79,7 @@ class Defaults(Singleton): ''' Port = 502 Retries = 3 + RetryOnEmpty = False Timeout = 3 Reconnects = 0 TransactionId = 0 diff --git a/pymodbus/transaction.py b/pymodbus/transaction.py index 2ee284fdb..78ad0a051 100644 --- a/pymodbus/transaction.py +++ b/pymodbus/transaction.py @@ -38,13 +38,15 @@ class ModbusTransactionManager(object): This module helps to abstract this away from the framer and protocol. ''' - def __init__(self, client): + def __init__(self, client, **kwargs): ''' Initializes an instance of the ModbusTransactionManager :param client: The client socket wrapper + :param retry_on_empty: Should the client retry on empty ''' self.tid = Defaults.TransactionId self.client = client + self.retry_on_empty = kwargs.get('retry_on_empty', Defaults.RetryOnEmpty) def execute(self, request): ''' Starts the producer to send the next request to @@ -62,6 +64,9 @@ def execute(self, request): # as this may not read the full result set, but right now # it should be fine... result = self.client._recv(1024) + if not result and self.retry_on_empty: + retries -= 1 + continue self.client.framer.processIncomingPacket(result, self.addTransaction) break; except socket.error, msg: @@ -132,7 +137,7 @@ def __iter__(self): :returns: An iterator of the managed transactions ''' - return self.transactions.iterkeys() + return iter(self.transactions.keys()) def addTransaction(self, request, tid=None): ''' Adds a transaction to the handler