diff --git a/README.md b/README.md index cb386a1d..bfc231a5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ python-OBD ========== +**Forked version** +- Added voltage gathering even if car is off + A python module for handling realtime sensor data from OBD-II vehicle ports. Works with ELM327 OBD-II adapters, and is fit for the Raspberry Pi. diff --git a/obd/elm327.py b/obd/elm327.py index 79e6abff..07bc8c97 100644 --- a/obd/elm327.py +++ b/obd/elm327.py @@ -120,6 +120,9 @@ def __init__(self, portname, baudrate, protocol, timeout, self.__low_power = False self.timeout = timeout + # custom + self.___voltage = '' + # ------------- open port ------------- try: self.__port = serial.serial_for_url(portname, @@ -177,6 +180,8 @@ def __init__(self, portname, baudrate, protocol, timeout, # -------------------------- AT RV (read volt) ------------------------ if check_voltage: r = self.__send(b"AT RV") + # save voltage to instance (useful even if car turned off) + self.___voltage = r if not r or len(r) != 1 or r[0] == '': self.__error("No answer from 'AT RV'") return @@ -565,3 +570,8 @@ def __read(self): lines = [s.strip() for s in re.split("[\r\n]", string) if bool(s)] return lines + + @property + def voltage(self): + """ return the voltage saved on init """ + return self.___voltage \ No newline at end of file diff --git a/obd/obd.py b/obd/obd.py index 5bd29e60..3957e119 100644 --- a/obd/obd.py +++ b/obd/obd.py @@ -59,6 +59,9 @@ def __init__(self, portstr=None, baudrate=None, protocol=None, fast=True, self.__last_header = ECU_HEADER.ENGINE # for comparing with the previously used header self.__frame_counts = {} # keeps track of the number of return frames for each command + # custom vars + self.___voltage = '' + logger.info("======================= python-OBD (v%s) =======================" % __version__) self.__connect(portstr, baudrate, protocol, check_voltage, start_low_power) # initialize by connecting and loading sensors @@ -98,6 +101,7 @@ def __connect(self, portstr, baudrate, protocol, check_voltage, if self.interface.status() == OBDStatus.NOT_CONNECTED: # the ELM327 class will report its own errors self.close() + else: self.___voltage = self.interface.voltage def __load_commands(self): """ @@ -313,3 +317,8 @@ def __build_command_string(self, cmd): cmd_string = b"" return cmd_string + + @property + def voltage(self): + """ get the voltage gathered from interface on init """ + return self.___voltage