Skip to content

Commit

Permalink
Merge pull request cedricp#901 from josefe17/master
Browse files Browse the repository at this point in the history
Fixed issue with sniffing commands encoding
  • Loading branch information
Furtif authored Nov 1, 2023
2 parents e31bea3 + ae01a61 commit 77ab6d2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
29 changes: 15 additions & 14 deletions elm.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,9 @@ def __init__(self, portName, rate, adapter_type="STD", maxspeed="No"):
os.mkdir("./logs")

if len(options.log) > 0:
self.lf = open("./logs/elm_" + options.log, "at")
self.vf = open("./logs/ecu_" + options.log, "at")
self.lf = open("./logs/elm_" + options.log + ".txt", "at", encoding="utf-8")
self.vf = open("./logs/ecu_" + options.log + ".txt", "at", encoding="utf-8")
self.vf.write("Timestamp;ECU_CAN_Address_HEX;Raw_Command_HEX;Raw_Response_HEX_or_STR;Error_message_if_happens\n")

self.lastCMDtime = 0
self.ATCFC0 = options.opt_cfc0
Expand Down Expand Up @@ -624,7 +625,7 @@ def __init__(self, portName, rate, adapter_type="STD", maxspeed="No"):

def raise_odb_speed(self, baudrate):
# Software speed switch
res = self.port.write(("ST SBR " + str(baudrate) + "\r").encode('utf8'))
res = self.port.write(("ST SBR " + str(baudrate) + "\r").encode('utf-8'))

# Command echo
res = self.port.expect_carriage_return()
Expand All @@ -646,13 +647,13 @@ def raise_odb_speed(self, baudrate):
def raise_elm_speed(self, baudrate):
# Software speed switch to 115Kbps
if baudrate == 57600:
res = self.port.write("ATBRD 45\r".encode("utf8"))
res = self.port.write("ATBRD 45\r".encode("utf-8"))
elif baudrate == 115200:
res = self.port.write("ATBRD 23\r".encode("utf8"))
res = self.port.write("ATBRD 23\r".encode("utf-8"))
elif baudrate == 230400:
res = self.port.write("ATBRD 11\r".encode("utf8"))
res = self.port.write("ATBRD 11\r".encode("utf-8"))
elif baudrate == 500000:
res = self.port.write("ATBRD 8\r".encode("utf8"))
res = self.port.write("ATBRD 8\r".encode("utf-8"))
else:
return

Expand All @@ -665,7 +666,7 @@ def raise_elm_speed(self, baudrate):
self.port.change_rate(baudrate)
version = self.port.expect_carriage_return()
if "ELM327" in version:
self.port.write('\r'.encode('utf8'))
self.port.write('\r'.encode('utf-8'))
res = self.port.expect('>')
if "OK" in res:
print("ELM full speed connection OK ")
Expand All @@ -681,7 +682,7 @@ def raise_elm_speed(self, baudrate):
def __del__(self):
try:
print("ELM reset...")
self.port.write("ATZ\r".encode("utf8"))
self.port.write("ATZ\r".encode("utf-8"))
except:
pass

Expand Down Expand Up @@ -735,9 +736,9 @@ def request(self, req, positive='', cache=True, serviceDelay="0"):
if self.vf != 0:
tmstr = datetime.now().strftime("%H:%M:%S.%f")[:-3]
if self.currentaddress in dnat:
self.vf.write(tmstr + ";" + dnat[self.currentaddress] + ";" + req + ";" + rsp + "\n")
self.vf.write(tmstr + ";" + "0x" + dnat[self.currentaddress] + ";" + "0x" + req + ";" + "0x" + rsp.rstrip().replace(" ", ",0x") + ";" +"\n")
else:
print("Unknown address ", self.currentaddress, req, rsp)
print("Unknown address: ", self.currentaddress, "0x" + req, "0x" + rsp)
self.vf.flush()

return rsp
Expand Down Expand Up @@ -912,7 +913,7 @@ def send_can(self, command):
if self.vf != 0:
tmstr = datetime.now().strftime("%H:%M:%S.%f")[:-3]
self.vf.write(
tmstr + ";" + dnat[self.currentaddress] + ";" + command + ";" + result + ";" + errorstr + "\n")
tmstr + ";" + dnat[self.currentaddress] + ";" + "0x" + command + ";" + result + ";" + errorstr + "\n")
self.vf.flush()

# populate L1 cache
Expand Down Expand Up @@ -1193,7 +1194,7 @@ def monitor_can_bus(self, callback):
if options.simulation_mode:
pass
else:
self.port.write("AT MA\r")
self.port.write("AT MA\r".encode('utf-8'))
stream = ""
while not self.monitorstop:
byte = self.port.read()
Expand All @@ -1211,7 +1212,7 @@ def monitor_can_bus(self, callback):
if byte:
stream += byte

self.port.write("AT\r")
self.port.write("AT\r".encode('utf-8'))
self.port.expect('>')

def init_can(self):
Expand Down
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def scanselvehicle(self):

def init(self):
self.list.clear()
self.list.setSortingEnabled(True)
self.list.setColumnCount(8)
self.list.model().setHeaderData(0, core.Qt.Horizontal, _('ECU name'))
self.list.model().setHeaderData(1, core.Qt.Horizontal, _('ID'))
Expand Down Expand Up @@ -570,6 +571,7 @@ def __init__(self, parent=None):

self.setConnected(True)
self.tabbedview.setCurrentIndex(1)
self.showMaximized()

def setIcon(self):
appIcon = gui.QIcon("icons/obd.png")
Expand Down
2 changes: 1 addition & 1 deletion parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ def initXML(self):
print(_("XML file not found : ") + self.ddtfile)
return

self.ecurequestsparser = ecu.Ecu_file(xdoc)
self.ecurequestsparser = ecu.Ecu_file(self.ddtfile, True)

target = self.getChildNodesByName(xdoc, u"Target")[0]
if not target:
Expand Down

0 comments on commit 77ab6d2

Please sign in to comment.