Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pymodbus/client/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ def __init__(self, method='ascii', **kwargs):
self.parity = kwargs.get('parity', Defaults.Parity)
self.baudrate = kwargs.get('baudrate', Defaults.Baudrate)
self.timeout = kwargs.get('timeout', Defaults.Timeout)

# Set a relatively fast timeout on self.socket.
self.socket_poll_rate = min(0.1, self.timeout)

@staticmethod
def __implementation(method):
Expand All @@ -325,9 +328,10 @@ def connect(self):
'''
if self.socket: return True
try:
self.socket = serial.Serial(port=self.port, timeout=self.timeout,
bytesize=self.bytesize, stopbits=self.stopbits,
baudrate=self.baudrate, parity=self.parity)
self.socket = serial.Serial(port=self.port,
timeout=self.socket_poll_rate, bytesize=self.bytesize,
stopbits=self.stopbits, baudrate=self.baudrate,
parity=self.parity)
except serial.SerialException, msg:
_logger.error(msg)
self.close()
Expand Down
13 changes: 10 additions & 3 deletions pymodbus/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import sys
import struct
import socket
import time
from binascii import b2a_hex, a2b_hex

from pymodbus.exceptions import ModbusIOException
from pymodbus.exceptions import ModbusIOException, NotImplementedException
from pymodbus.constants import Defaults
from pymodbus.interfaces import IModbusFramer
from pymodbus.utilities import checkCRC, computeCRC
Expand Down Expand Up @@ -58,21 +59,27 @@ def execute(self, request):
request.transaction_id = self.getNextTID()
_logger.debug("Running transaction %d" % request.transaction_id)


while retries > 0:
start_time = time.time()
try:
self.client.connect()
self.client._send(self.client.framer.buildPacket(request))
# I need to fix this to read the header and the result size,
# as this may not read the full result set, but right now
# it should be fine...
result = self.client._recv(1024)
result = ''
while result == '' and time.time() < start_time + self.client.timeout:
# Keep retrying until timeout elapses
result = self.client._recv(1024)
if not result and self.retry_on_empty:
_logger.debug("No data received within timeout. Querying again")
retries -= 1
continue
if _logger.isEnabledFor(logging.DEBUG):
_logger.debug("recv: " + " ".join([hex(ord(x)) for x in result]))
self.client.framer.processIncomingPacket(result, self.addTransaction)
break;
break
except socket.error, msg:
self.client.close()
_logger.debug("Transaction failed. (%s) " % msg)
Expand Down