Skip to content

Commit 022a81b

Browse files
committed
Make connection timeout configurable
Why: * the hard-coded value of 0.1 was making it impossible to connect with OSX, and possibly in other cases. This change addresses the need by: * Adding `conn_timeout` kwarg to OBD and Async, which defaults to 0.1, but can be configured to suit the user's neeeds. Other observations: * There were a few failing tests locally (python3.6 on OSX 10.12.6) when I cloned the repo, these tests are still failing. I may look into these next and submit another PR.
1 parent cf88cbd commit 022a81b

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

obd/async.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ class Async(OBD):
4545
Specialized for asynchronous value reporting.
4646
"""
4747

48-
def __init__(self, portstr=None, baudrate=None, protocol=None, fast=True):
49-
super(Async, self).__init__(portstr, baudrate, protocol, fast)
48+
def __init__(self, portstr=None, baudrate=None, protocol=None, fast=True,
49+
conn_timeout=0.1):
50+
super(Async, self).__init__(portstr, baudrate, protocol, fast,
51+
conn_timeout)
5052
self.__commands = {} # key = OBDCommand, value = Response
5153
self.__callbacks = {} # key = OBDCommand, value = list of Functions
5254
self.__thread = None

obd/elm327.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class ELM327:
103103

104104

105105

106-
def __init__(self, portname, baudrate, protocol):
106+
def __init__(self, portname, baudrate, protocol, conn_timeout):
107107
"""Initializes port by resetting device and gettings supported PIDs. """
108108

109109
logger.info("Initializing ELM327: PORT=%s BAUD=%s PROTOCOL=%s" %
@@ -116,6 +116,7 @@ def __init__(self, portname, baudrate, protocol):
116116
self.__status = OBDStatus.NOT_CONNECTED
117117
self.__port = None
118118
self.__protocol = UnknownProtocol([])
119+
self.conn_timeout = conn_timeout
119120

120121

121122
# ------------- open port -------------
@@ -276,7 +277,7 @@ def auto_baudrate(self):
276277

277278
# before we change the timout, save the "normal" value
278279
timeout = self.__port.timeout
279-
self.__port.timeout = 0.1 # we're only talking with the ELM, so things should go quickly
280+
self.__port.timeout = self.conn_timeout # we're only talking with the ELM, so things should go quickly
280281

281282
for baud in self._TRY_BAUDS:
282283
self.__port.baudrate = baud

obd/obd.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ class OBD(object):
4848
with it's assorted commands/sensors.
4949
"""
5050

51-
def __init__(self, portstr=None, baudrate=None, protocol=None, fast=True):
51+
def __init__(self, portstr=None, baudrate=None, protocol=None, fast=True,
52+
conn_timeout=0.1):
5253
self.interface = None
5354
self.supported_commands = set(commands.base_commands())
5455
self.fast = fast # global switch for disabling optimizations
56+
self.conn_timeout = conn_timeout
5557
self.__last_command = b"" # used for running the previous command with a CR
5658
self.__frame_counts = {} # keeps track of the number of return frames for each command
5759

@@ -77,13 +79,15 @@ def __connect(self, portstr, baudrate, protocol):
7779

7880
for port in portnames:
7981
logger.info("Attempting to use port: " + str(port))
80-
self.interface = ELM327(port, baudrate, protocol)
82+
self.interface = ELM327(port, baudrate, protocol,
83+
self.conn_timeout)
8184

8285
if self.interface.status() >= OBDStatus.ELM_CONNECTED:
8386
break # success! stop searching for serial
8487
else:
8588
logger.info("Explicit port defined")
86-
self.interface = ELM327(portstr, baudrate, protocol)
89+
self.interface = ELM327(portstr, baudrate, protocol,
90+
self.conn_timeout)
8791

8892
# if the connection failed, close it
8993
if self.interface.status() == OBDStatus.NOT_CONNECTED:

0 commit comments

Comments
 (0)