From cc944b53a5e3eee6846b9082dc78537bd0fc8597 Mon Sep 17 00:00:00 2001 From: cedric Date: Tue, 10 Nov 2020 10:32:59 +0100 Subject: [PATCH] Possibility to choose baudrate --- ddt4all.py | 33 +++++++++++++++++++++------------ elm.py | 52 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 55 insertions(+), 30 deletions(-) diff --git a/ddt4all.py b/ddt4all.py index 178b8b2f..9dd69024 100644 --- a/ddt4all.py +++ b/ddt4all.py @@ -1086,7 +1086,7 @@ def __init__(self): self.securitycheck = False self.selectedportspeed = 38400 self.adapter = "STD" - self.raise_obdlink_speed = False + self.raise_port_speed = "No" super(portChooser, self).__init__(None) layout = widgets.QVBoxLayout() label = widgets.QLabel(self) @@ -1196,11 +1196,10 @@ def __init__(self): layout.addLayout(darkstylelayout) obdlinkspeedlayout = widgets.QHBoxLayout() - self.obdlinkspeedcheck = widgets.QCheckBox() - self.obdlinkspeedcheck.setChecked(True) - obdlinkspeedlabel = widgets.QLabel(_("Raise UART link connection to max speed")) - obdlinkspeedlayout.addWidget(self.obdlinkspeedcheck) + self.obdlinkspeedcombo = widgets.QComboBox() + obdlinkspeedlabel = widgets.QLabel(_("Change UART speed")) obdlinkspeedlayout.addWidget(obdlinkspeedlabel) + obdlinkspeedlayout.addWidget(self.obdlinkspeedcombo) obdlinkspeedlayout.addStretch() layout.addLayout(obdlinkspeedlayout) @@ -1267,7 +1266,7 @@ def rescan_ports(self): def bt(self): self.adapter = "STD_BT" - self.obdlinkspeedcheck.setEnabled(False) + self.obdlinkspeedcombo.clear() self.wifibutton.blockSignals(True) self.btbutton.blockSignals(True) self.usbbutton.blockSignals(True) @@ -1289,7 +1288,7 @@ def bt(self): def wifi(self): self.adapter = "STD_WIFI" - self.obdlinkspeedcheck.setEnabled(False) + self.obdlinkspeedcombo.clear() self.wifibutton.blockSignals(True) self.btbutton.blockSignals(True) self.usbbutton.blockSignals(True) @@ -1310,7 +1309,13 @@ def wifi(self): def usb(self): self.adapter = "STD_USB" - self.obdlinkspeedcheck.setEnabled(True) + self.obdlinkspeedcombo.clear() + self.obdlinkspeedcombo.addItem("No") + self.obdlinkspeedcombo.addItem("57600") + self.obdlinkspeedcombo.addItem("115200") + self.obdlinkspeedcombo.addItem("230400") + # This mode seems to not be supported + #self.obdlinkspeedcombo.addItem("500000") self.wifibutton.blockSignals(True) self.btbutton.blockSignals(True) self.usbbutton.blockSignals(True) @@ -1332,7 +1337,11 @@ def usb(self): def obdlink(self): self.adapter = "OBDLINK" - self.obdlinkspeedcheck.setEnabled(True) + self.obdlinkspeedcombo.clear() + self.obdlinkspeedcombo.addItem("No") + self.obdlinkspeedcombo.addItem("500000") + self.obdlinkspeedcombo.addItem("1000000") + self.obdlinkspeedcombo.addItem("2000000") self.wifibutton.blockSignals(True) self.btbutton.blockSignals(True) self.usbbutton.blockSignals(True) @@ -1354,7 +1363,7 @@ def obdlink(self): def els(self): self.adapter = "ELS" - self.obdlinkspeedcheck.setEnabled(False) + self.obdlinkspeedcombo.clear() self.wifibutton.blockSignals(True) self.btbutton.blockSignals(True) self.usbbutton.blockSignals(True) @@ -1395,7 +1404,7 @@ def connectedMode(self): self.port = self.ports[portinfo][0] options.port_name = self.ports[portinfo][1] self.mode = 1 - self.raise_obdlink_speed = self.obdlinkspeedcheck.isChecked() + self.raise_port_speed = self.obdlinkspeedcombo.currentText() self.done(True) else: msgbox = widgets.QMessageBox() @@ -1455,7 +1464,7 @@ def demoMode(self): msgbox.exec_() print(_("Initilizing ELM with speed %i...") % port_speed) - options.elm = elm.ELM(options.port, port_speed, pc.adapter, pc.raise_obdlink_speed) + options.elm = elm.ELM(options.port, port_speed, pc.adapter, pc.raise_port_speed) if options.elm_failed: pc.show() pc.logview.append(options.get_last_error()) diff --git a/elm.py b/elm.py index c15e6f79..fa8238f9 100644 --- a/elm.py +++ b/elm.py @@ -562,12 +562,16 @@ class ELM: connectionStatus = False - def __init__(self, portName, rate, adapter_type="STD", maxspeed=False): + def __init__(self, portName, rate, adapter_type="STD", maxspeed="No"): for speed in [int(rate), 38400, 115200, 230400, 57600, 9600, 500000, 1000000, 2000000]: print(_("Trying to open port") + "%s @ %i" % (portName, speed)) self.sim_mode = options.simulation_mode self.portName = portName self.adapter_type = adapter_type + if maxspeed == "No": + maxspeed = 0 + else: + maxspeed = int(maxspeed) if not options.simulation_mode: self.port = Port(portName, speed, self.portTimeout) @@ -602,26 +606,26 @@ def __init__(self, portName, rate, adapter_type="STD", maxspeed=False): if adapter_type == "OBDLINK" and maxspeed and not options.elm_failed and rate != 2000000: print("OBDLink Connection OK, attempting full speed UART switch") try: - self.raise_odb_speed() + self.raise_odb_speed(maxspeed) except: options.elm_failed = True self.connectionStatus = False - print("Failed to switch to change OBDLink to 2Mbs.") - elif rate != 115200 and maxspeed: + print("Failed to switch to change OBDLink to " + str(maxspeed)) + elif adapter_type == "STD_USB" and rate != 115200 and maxspeed: print("ELM Connection OK, attempting high speed UART switch") try: - self.raise_elm_speed() + self.raise_elm_speed(maxspeed) except: options.elm_failed = True self.connectionStatus = False - print("Failed to switch to change ELM to 115Kbs.") + print("Failed to switch to change ELM to " + str(maxspeed)) - def raise_odb_speed(self): - # Software speed switch to 2Mbps - res = self.send_raw("ST SBR 2000000") + def raise_odb_speed(self, baudrate): + # Software speed switch + res = self.send_raw("ST SBR " + str(baudrate)) if "OK" in res: - print("OBDLINK switched to 2Mbs, changing UART speed now...") - self.port.change_rate(2000000) + print("OBDLINK switched baurate OK, changing UART speed now...") + self.port.change_rate(baudrate) time.sleep(1) res = self.send_raw("STI") if "STN" in res: @@ -632,21 +636,33 @@ def raise_odb_speed(self): else: raise - def raise_elm_speed(self): + def raise_elm_speed(self, baudrate): # Software speed switch to 115Kbps - res = self.port.write("ATBRD 23\r".encode("utf8")) + if baudrate == 57600: + res = self.port.write("ATBRD 45\r".encode("utf8")) + elif baudrate == 115200: + res = self.port.write("ATBRD 23\r".encode("utf8")) + elif baudrate == 230400: + res = self.port.write("ATBRD 11\r".encode("utf8")) + elif baudrate == 500000: + res = self.port.write("ATBRD 8\r".encode("utf8")) + else: + return + + # Command echo res = self.port.expect_carriage_return() + # Command result res = self.port.expect_carriage_return() if "OK" in res: - print("ELM switched to 115Kbs, changing UART speed now...") - self.port.change_rate(115200) + print("ELM baudrate switched OK, changing UART speed now...") + self.port.change_rate(baudrate) version = self.port.expect_carriage_return() if "ELM327" in version: self.port.write('\r'.encode('utf8')) res = self.port.expect('>') if "OK" in res: print("ELM full speed connection OK ") - print("ELM Version " + version) + print("Version " + version) else: raise else: @@ -1151,7 +1167,7 @@ def init_can_sniffer(self, filter_addr, br): if options.simulation_mode: return - self.cmd('AT WS') + #self.cmd('AT WS') self.cmd("AT E1") self.cmd("AT L0") self.cmd("AT H0") @@ -1315,7 +1331,7 @@ def init_iso(self): tmstr = datetime.now().strftime("%x %H:%M:%S.%f")[:-3] self.lf.write('#' * 60 + "\n#[" + tmstr + "] Init ISO\n" + '#' * 60 + "\n") self.lf.flush() - self.cmd("AT WS") + #self.cmd("AT WS") self.cmd("AT E1") self.cmd("AT L0") self.cmd("AT D1")