From f69011cc62834831f91f534040b2644ed683ad37 Mon Sep 17 00:00:00 2001 From: Quoing Date: Thu, 9 Jan 2020 19:30:06 +0000 Subject: [PATCH] Allow to create obd without connection, and add explicit connect method --- obd/asynchronous.py | 4 ++-- obd/obd.py | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/obd/asynchronous.py b/obd/asynchronous.py index 3a8eaf5a..47f91ad4 100644 --- a/obd/asynchronous.py +++ b/obd/asynchronous.py @@ -47,10 +47,10 @@ class Async(OBD): def __init__(self, portstr=None, baudrate=None, protocol=None, fast=True, timeout=0.1, check_voltage=True, start_low_power=False, - delay_cmds=0.25): + delay_cmds=0.25, connect=True): self.__thread = None super(Async, self).__init__(portstr, baudrate, protocol, fast, - timeout, check_voltage, start_low_power) + timeout, check_voltage, start_low_power, connect) self.__commands = {} # key = OBDCommand, value = Response self.__callbacks = {} # key = OBDCommand, value = list of Functions self.__running = False diff --git a/obd/obd.py b/obd/obd.py index 5bd29e60..7a82eadc 100644 --- a/obd/obd.py +++ b/obd/obd.py @@ -50,7 +50,7 @@ class OBD(object): """ def __init__(self, portstr=None, baudrate=None, protocol=None, fast=True, - timeout=0.1, check_voltage=True, start_low_power=False): + timeout=0.1, check_voltage=True, start_low_power=False, connect=True): self.interface = None self.supported_commands = set(commands.base_commands()) self.fast = fast # global switch for disabling optimizations @@ -60,13 +60,18 @@ def __init__(self, portstr=None, baudrate=None, protocol=None, fast=True, self.__frame_counts = {} # keeps track of the number of return frames for each command logger.info("======================= python-OBD (v%s) =======================" % __version__) - self.__connect(portstr, baudrate, protocol, + if connect: + self.__connect(portstr, baudrate, protocol, check_voltage, start_low_power) # initialize by connecting and loading sensors - self.__load_commands() # try to load the car's supported commands + self.__load_commands() # try to load the car's supported commands logger.info("===================================================================") - def __connect(self, portstr, baudrate, protocol, check_voltage, - start_low_power): + def connect(self, *args, **kwargs): + self.__connect(*args, **kwargs) + self.__load_commands() + + def __connect(self, portstr=None, baudrate=None, protocol=None, check_voltage=True, + start_low_power=False): """ Attempts to instantiate an ELM327 connection object. """