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 1/6] 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 2/6] 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 3/6] 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 4/6] 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 5/6] 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 6/6] 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.")