From 52d4ec3bc3317186b96c226238f22faf4c6217a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Michel=20Pour=C3=A9?= Date: Mon, 26 Jun 2017 18:56:07 +0200 Subject: [PATCH 01/13] Rename functions getting a value with get_ prefix for clarity. Add get_baud_rate() to query speed of connection. --- obd/elm327.py | 27 +++++++++++++++++---------- obd/obd.py | 48 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/obd/elm327.py b/obd/elm327.py index c469975b..e28a68e1 100644 --- a/obd/elm327.py +++ b/obd/elm327.py @@ -258,7 +258,7 @@ def auto_protocol(self): def set_baudrate(self, baud): if baud is None: # when connecting to pseudo terminal, don't bother with auto baud - if self.port_name().startswith("/dev/pts"): + if self.get_port_name().startswith("/dev/pts"): logger.debug("Detected pseudo terminal, skipping baudrate setup") return True else: @@ -330,29 +330,36 @@ def __error(self, msg): logger.error(str(msg)) - def port_name(self): + def get_port_name(self): if self.__port is not None: return self.__port.portstr else: return "" - def status(self): - return self.__status - - - def ecus(self): - return self.__protocol.ecu_map.values() + def get_port_baudrate(self): + if self.__port is not None: + return self.__port.baudrate + else: + return "" - def protocol_name(self): + def get_protocol_name(self): return self.__protocol.ELM_NAME - def protocol_id(self): + def get_protocol_id(self): return self.__protocol.ELM_ID + def get_ecus(self): + return self.__protocol.ecu_map.values() + + + def status(self): + return self.__status + + def close(self): """ Resets the device, and sets all diff --git a/obd/obd.py b/obd/obd.py index 221725b1..233c796b 100644 --- a/obd/obd.py +++ b/obd/obd.py @@ -155,40 +155,46 @@ def status(self): return self.interface.status() - # not sure how useful this would be - - # def ecus(self): - # """ returns a list of ECUs in the vehicle """ - # if self.interface is None: - # return [] - # else: - # return self.interface.ecus() + def get_ecus(self): + """ returns a list of ECUs in the vehicle """ + if self.interface is None: + return [] + else: + return self.interface.get_ecus() - def protocol_name(self): + def get_protocol_name(self): """ returns the name of the protocol being used by the ELM327 """ if self.interface is None: return "" else: - return self.interface.protocol_name() + return self.interface.get_protocol_name() - def protocol_id(self): + def get_protocol_id(self): """ returns the ID of the protocol being used by the ELM327 """ if self.interface is None: return "" else: - return self.interface.protocol_id() + return self.interface.get_protocol_id() - def port_name(self): + def get_port_name(self): """ Returns the name of the currently connected port """ if self.interface is not None: - return self.interface.port_name() + return self.interface.get_port_name() else: return "" + def get_port_baudrate(self): + """ Returns the speed of the currently connected port """ + if self.interface is not None: + return str(self.interface.get_port_baudrate()) + else: + return "" + + def is_connected(self): """ Returns a boolean for whether a connection with the car was made. @@ -214,6 +220,18 @@ def supports(self, cmd): is supported by the car """ return cmd in self.supported_commands + + + def get_supported_commands(self): + """ + Returns a list of commands + supported by the car + """ + + if self.interface is not None: + return self.supported_commands + else: + return [] def test_cmd(self, cmd, warn=True): @@ -228,7 +246,7 @@ def test_cmd(self, cmd, warn=True): return False # mode 06 is only implemented for the CAN protocols - if cmd.mode == 6 and self.interface.protocol_id() not in ["6", "7", "8", "9"]: + if cmd.mode == 6 and self.interface.get_protocol_id() not in ["6", "7", "8", "9"]: if warn: logger.warning("Mode 06 commands are only supported over CAN protocols") return False From 06042399b4f01b51957c16ffd66956a01d8bcd1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Michel=20Pour=C3=A9?= Date: Mon, 26 Jun 2017 19:42:12 +0200 Subject: [PATCH 02/13] Add print_discover() function to print all information discovered: protocol, port name, port speed and supported commands. --- obd/obd.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/obd/obd.py b/obd/obd.py index 233c796b..dcc860e2 100644 --- a/obd/obd.py +++ b/obd/obd.py @@ -213,7 +213,23 @@ def print_commands(self): for c in self.supported_commands: print(str(c)) - + def print_discovered(self): + """ + Utility function meant to print all information discovered: + protocol, port name, port baudrate and all supported commands. + """ + if self.interface is not None: + print ("The following information was used to connect to the ECU:") + print ("Protocole: " + self.get_protocol_name()) + print ("Port name: " + self.get_port_name()) + print ("Port rate: " + self.get_port_baudrate()) + print ("The following commands are supported:") + + for c in self.supported_commands: + print(str(c)) + else: + print ("Impossible to print discovered information: no connection to the ECU.") + def supports(self, cmd): """ Returns a boolean for whether the given command From a1a0236b3071d0245eb523030bb80707cb4604c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Michel=20Pour=C3=A9?= Date: Mon, 26 Jun 2017 19:45:59 +0200 Subject: [PATCH 03/13] Typo --- obd/obd.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/obd/obd.py b/obd/obd.py index dcc860e2..080c4014 100644 --- a/obd/obd.py +++ b/obd/obd.py @@ -219,11 +219,11 @@ def print_discovered(self): protocol, port name, port baudrate and all supported commands. """ if self.interface is not None: - print ("The following information was used to connect to the ECU:") + print ("The following settings were used to connect to the ECU:") print ("Protocole: " + self.get_protocol_name()) print ("Port name: " + self.get_port_name()) print ("Port rate: " + self.get_port_baudrate()) - print ("The following commands are supported:") + print ("The following OBD commands are supported:") for c in self.supported_commands: print(str(c)) From 9d823cb67ec266ca38fdd6d2878acd92c237b734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Michel=20Pour=C3=A9?= Date: Mon, 26 Jun 2017 20:04:32 +0200 Subject: [PATCH 04/13] Updade test to reflect changes in obd.py. --- tests/test_OBD.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/test_OBD.py b/tests/test_OBD.py index 81516f2f..cee10535 100644 --- a/tests/test_OBD.py +++ b/tests/test_OBD.py @@ -23,7 +23,7 @@ def __init__(self, portname, UNUSED_baudrate=None, UNUSED_protocol=None): self._status = OBDStatus.CAR_CONNECTED self._last_command = None - def port_name(self): + def get_port_name(self): return self._portname def status(self): @@ -32,10 +32,10 @@ def status(self): def ecus(self): return [ ECU.ENGINE, ECU.UNKNOWN ] - def protocol_name(self): + def get_protocol_name(self): return "ISO 15765-4 (CAN 11/500)" - def protocol_id(self): + def get_protocol_id(self): return "6" def close(self): @@ -122,30 +122,30 @@ def test_port_name(): """ o = obd.OBD("/dev/null") o.interface = FakeELM("/dev/null") - assert o.port_name() == o.interface._portname + assert o.get_port_name() == o.interface.get_port_name() o.interface = FakeELM("A different port name") - assert o.port_name() == o.interface._portname + assert o.get_port_name() == o.interface.get_port_name() def test_protocol_name(): o = obd.OBD("/dev/null") o.interface = None - assert o.protocol_name() == "" + assert o.get_protocol_name() == "" o.interface = FakeELM("/dev/null") - assert o.protocol_name() == o.interface.protocol_name() + assert o.get_protocol_name() == o.interface.get_protocol_name() def test_protocol_id(): o = obd.OBD("/dev/null") o.interface = None - assert o.protocol_id() == "" + assert o.get_protocol_id() == "" o.interface = FakeELM("/dev/null") - assert o.protocol_id() == o.interface.protocol_id() + assert o.get_protocol_id() == o.interface.get_protocol_id() From eb87fecd926d627ea49a6d57b0c2917a3763f1bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Michel=20Pour=C3=A9?= Date: Mon, 26 Jun 2017 22:25:10 +0200 Subject: [PATCH 05/13] Sort list of supported OBD commands. --- obd/obd.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/obd/obd.py b/obd/obd.py index 080c4014..9edeed1b 100644 --- a/obd/obd.py +++ b/obd/obd.py @@ -225,8 +225,13 @@ def print_discovered(self): print ("Port rate: " + self.get_port_baudrate()) print ("The following OBD commands are supported:") + _mylist=[] for c in self.supported_commands: - print(str(c)) + _mylist.append(str(c)) + _mylist.sort() + for i in _mylist: + print i + else: print ("Impossible to print discovered information: no connection to the ECU.") From 65b50e7209efeb298d1eeab5dfe157bf4a23c98f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Michel=20Pour=C3=A9?= Date: Tue, 27 Jun 2017 09:21:50 +0200 Subject: [PATCH 06/13] Use print(i) instead of print i for Python compliance. --- obd/obd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/obd/obd.py b/obd/obd.py index 9edeed1b..5f984bf6 100644 --- a/obd/obd.py +++ b/obd/obd.py @@ -230,7 +230,7 @@ def print_discovered(self): _mylist.append(str(c)) _mylist.sort() for i in _mylist: - print i + print (i) else: print ("Impossible to print discovered information: no connection to the ECU.") From bf848826612b4cc619f8c3a3c0a8085871591c41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Michel=20Pour=C3=A9?= Date: Tue, 27 Jun 2017 10:07:19 +0200 Subject: [PATCH 07/13] Add list of error messages. --- obd/elm327.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/obd/elm327.py b/obd/elm327.py index e28a68e1..78f09df1 100644 --- a/obd/elm327.py +++ b/obd/elm327.py @@ -101,6 +101,24 @@ class ELM327: # going to be less picky about the time required to detect it. _TRY_BAUDS = [ 38400, 9600, 230400, 115200, 57600, 19200 ] + _ERROR_MESSAGES = [ + "?", # Misunderstood command received on the RS232 input. + "ACT ALERT", # No RS232 activity, switching to low-power standby mode + "BUFFER FULL", # RS232buffer is filling at a faster rate than transmission + "BUS BUSY", + "BUS ERROR", + "CAN ERROR", + "DATA ERROR", + " Date: Tue, 27 Jun 2017 10:36:11 +0200 Subject: [PATCH 08/13] Add list of error messages. --- obd/elm327.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/obd/elm327.py b/obd/elm327.py index 78f09df1..19a8ef0a 100644 --- a/obd/elm327.py +++ b/obd/elm327.py @@ -102,22 +102,22 @@ class ELM327: _TRY_BAUDS = [ 38400, 9600, 230400, 115200, 57600, 19200 ] _ERROR_MESSAGES = [ - "?", # Misunderstood command received on the RS232 input. - "ACT ALERT", # No RS232 activity, switching to low-power standby mode - "BUFFER FULL", # RS232buffer is filling at a faster rate than transmission - "BUS BUSY", - "BUS ERROR", - "CAN ERROR", - "DATA ERROR", - " Date: Tue, 27 Jun 2017 10:38:34 +0200 Subject: [PATCH 09/13] Fix list of error messages. --- obd/elm327.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/obd/elm327.py b/obd/elm327.py index 19a8ef0a..7243d738 100644 --- a/obd/elm327.py +++ b/obd/elm327.py @@ -117,7 +117,7 @@ class ELM327: ["NO DATA", "ELM327 returned no data, either because answer is empty or not understood or not supported."], [" Date: Tue, 27 Jun 2017 11:17:06 +0200 Subject: [PATCH 10/13] First attempt to report ELM327 error messages. No possibility to test. --- obd/elm327.py | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/obd/elm327.py b/obd/elm327.py index 7243d738..5a0b18b0 100644 --- a/obd/elm327.py +++ b/obd/elm327.py @@ -102,22 +102,22 @@ class ELM327: _TRY_BAUDS = [ 38400, 9600, 230400, 115200, 57600, 19200 ] _ERROR_MESSAGES = [ - ["?", "ELM327 reports misundersting command received on the RS232 input."], - ["ACT ALERT", "ELM327 warns no RS232 activity. Might switch to low-power standby mode."], - ["BUFFER FULL", "ELM327 reports that RS232 buffer is filling at a faster rate than transmission. Increase baud speed."], - ["BUS BUSY", "ELM327 reports too much activity on BUS. Check wiring."], - ["BUS ERROR", "ELM327 received a BUS error. This might be normal when starting CAN listening."], - ["CAN ERROR", "ELM327 received a CAN system ERROR. CAN has difficulty initializing, sending or receiving."], - ["DATA ERROR", "ELM327 received a error response from vehicule, data lost."], - [" Date: Tue, 27 Jun 2017 12:33:27 +0200 Subject: [PATCH 11/13] Fix ELM327 error messages. --- obd/elm327.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/obd/elm327.py b/obd/elm327.py index 5a0b18b0..319fdcc5 100644 --- a/obd/elm327.py +++ b/obd/elm327.py @@ -135,7 +135,6 @@ def __init__(self, portname, baudrate, protocol): self.__port = None self.__protocol = UnknownProtocol([]) - # ------------- open port ------------- try: self.__port = serial.Serial(portname, \ @@ -475,12 +474,12 @@ def __read(self): # end on chevron (ELM prompt character) if self.ELM_PROMPT in buffer: break - else : - # Check errors received by ELM327 and send only a warning - for iError in self._ERROR_MESSAGES: - if iError[0] in buffer: - logger.warning(iError[1]) - break + + #Check errors received by ELM327 and write debug + for iError in self._ERROR_MESSAGES: + if iError[0] in buffer: + logger.debug(iError[1]) + break # log, and remove the "bytearray( ... )" part logger.debug("read: " + repr(buffer)[10:-1]) From 11d909a47349b17bc7935f872930ac98bd3741ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Michel=20Pour=C3=A9?= Date: Tue, 27 Jun 2017 13:17:05 +0200 Subject: [PATCH 12/13] Adding fake command for testing pupose. --- obd/commands.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/obd/commands.py b/obd/commands.py index 0f511009..5c076b9c 100644 --- a/obd/commands.py +++ b/obd/commands.py @@ -287,15 +287,17 @@ __mode9__ = [ # name description cmd bytes decoder ECU fast - # OBDCommand("PIDS_9A" , "Supported PIDs [01-20]" , b"0900", 4, pid, ECU.ENGINE, True), - # OBDCommand("VIN_MESSAGE_COUNT" , "VIN Message Count" , b"0901", 1, uas(0x01), ECU.ENGINE, True), - # OBDCommand("VIN" , "Get Vehicle Identification Number" , b"0902", 20, raw_string, ECU.ENGINE, True), + OBDCommand("PIDS_9A" , "Supported PIDs [01-20]" , b"0900", 4, pid, ECU.ENGINE, True), + OBDCommand("VIN_MESSAGE_COUNT" , "VIN Message Count" , b"0901", 1, uas(0x01), ECU.ENGINE, True), + OBDCommand("VIN" , "Get Vehicle Identification Number" , b"0902", 20, raw_string, ECU.ENGINE, True), ] __misc__ = [ # name description cmd bytes decoder ECU fast OBDCommand("ELM_VERSION" , "ELM327 version string" , b"ATI", 0, raw_string, ECU.UNKNOWN, False), OBDCommand("ELM_VOLTAGE" , "Voltage detected by OBD-II adapter" , b"ATRV", 0, elm_voltage, ECU.UNKNOWN, False), + OBDCommand("FAKE_COMMAND_1" , "Non-existing command returning error" , b"ATXYZ", 0, raw_string, ECU.UNKNOWN, False), + ] From 5d45f3cc89b4f8c4af7963bd8e37a34c451c4c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Michel=20Pour=C3=A9?= Date: Tue, 27 Jun 2017 18:00:01 +0200 Subject: [PATCH 13/13] Replace byte() with b prefix. --- obd/elm327.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/obd/elm327.py b/obd/elm327.py index 319fdcc5..de8c6a2a 100644 --- a/obd/elm327.py +++ b/obd/elm327.py @@ -102,22 +102,22 @@ class ELM327: _TRY_BAUDS = [ 38400, 9600, 230400, 115200, 57600, 19200 ] _ERROR_MESSAGES = [ - [bytes("?"), "?. ELM327 reports misundersting command received on the RS232 input."], - [bytes("ACT ALERT"), "ACT ALERT: ELM327 warns no RS232 activity. Might switch to low-power standby mode."], - [bytes("BUFFER FULL"), "BUFFER FULL: ELM327 reports that RS232 buffer is filling at a faster rate than transmission. Increase baud speed."], - [bytes("BUS BUSY"), "BUS BUSY: ELM327 reports too much activity on BUS. Check wiring."], - [bytes("BUS ERROR"), "BUS ERROR: ELM327 reports a BUS error. This might be normal when starting CAN listening."], - [bytes("CAN ERROR"), "CAN ERROR: ELM327 reports a CAN system ERROR. CAN has difficulty initializing, sending or receiving."], - [bytes("