Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/cedricp/ddt4all
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricp committed Nov 24, 2016
2 parents 41ffcb2 + 5c25ea0 commit cf1c68d
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 75 deletions.
11 changes: 10 additions & 1 deletion ddt4all.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ def scan(self):
item = gui.QListWidgetItem(ecu)
self.treeview_ecu.addItem(item)

for ecu in self.ecu_scan.approximate_ecus.keys():
item = gui.QListWidgetItem(ecu)
item.setForeground(core.Qt.red)
self.treeview_ecu.addItem(item)

self.progressstatus.setValue(0)

def setConnected(self, on):
Expand Down Expand Up @@ -318,7 +323,11 @@ def changeECU(self, index):
ecu_name = unicode(item[0].toString().toUtf8(), encoding="UTF-8")
self.treeview_params.clear()

ecu = self.ecu_scan.ecus[ecu_name]
if ecu_name in self.ecu_scan.ecus:
ecu = self.ecu_scan.ecus[ecu_name]
else:
ecu = self.ecu_scan.approximate_ecus[ecu_name]

ecu_file = "ecus/" + ecu.href
ecu_addr = ecu.addr
self.paramview = parameters.paramWidget(self.scrollview, ecu_file, ecu_addr, ecu_name, self.logview)
Expand Down
117 changes: 72 additions & 45 deletions ecu.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(self, xml, endian):
self.shiftbytescount = 0
self.replybytes = ''
self.manualsend = False
self.sentbytes = 0
self.sentbytes = ''
self.dataitems = {}
self.sendbyte_dataitems = {}
self.name = 'uninit'
Expand Down Expand Up @@ -227,10 +227,7 @@ def setValue(self, value, bytes_list, dataitem, request_endian):
return None

for i in range(self.bytescount):
if not little_endian:
bytes_list[i] = ord(value[i])
else:
bytes_list[self.bytescount - i - 1] = ord(value[i])
bytes_list[self.bytescount - i - 1] = ord(value[i])

return bytes_list

Expand Down Expand Up @@ -269,10 +266,9 @@ def setValue(self, value, bytes_list, dataitem, request_endian):
hex_bytes = [hex_value[i:i + 2] for i in range(0, len(hex_value), 2)]

n = 0
hex_len = len(hex_bytes) - 1
if len(hex_bytes) > self.bytescount:

if len(hex_bytes) > self.bytescount:
return None

for h in hex_bytes:
original_byte = int('0x' + bytes_list[n + start_byte], 16)
original_value = int('0x' + h, 16)
Expand All @@ -284,10 +280,7 @@ def setValue(self, value, bytes_list, dataitem, request_endian):

value_formatted = "{0:#0{1}x}".format(new, 4)[2:].upper()

if not little_endian:
bytes_list[start_byte + n] = value_formatted
else:
bytes_list[start_byte + hex_len - n] = value_formatted
bytes_list[start_byte + n] = value_formatted

n += 1

Expand Down Expand Up @@ -363,16 +356,16 @@ def getHexValue(self, resp, dataitem, req_endian):
if len(hexval) == 0:
return None

if little_endian:
a = hexval
b = ''
if not len(a) % 2:
for i in range(0, len(a), 2):
b = a[i:i + 2] + b
hexval = b
else:
print "Warning, cannot convert little endian value, report error please"
return None
# if little_endian:
# a = hexval
# b = ''
# if not len(a) % 2:
# for i in range(0, len(a), 2):
# b = a[i:i + 2] + b
# hexval = b
# else:
# print "Warning, cannot convert little endian value, report error please"
# return None

if bits % 8:
if little_endian:
Expand Down Expand Up @@ -459,6 +452,17 @@ def checkWith(self, diagversion, supplier, soft, version, addr):
self.addr = addr
return True

def checkApproximate(self, diagversion, supplier, soft, version, addr):
if self.diagversion != diagversion:
return False
if self.supplier != supplier:
return False
if self.soft != soft:
return False

self.addr = addr
return True

def checkProtocol(self):
if not self.protocol in self.protocols:
print "Unknown protocol '", self.protocol, "' "
Expand Down Expand Up @@ -518,6 +522,7 @@ def addTarget(self, target):
def clear(self):
self.totalecu = 0
self.ecus = {}
self.approximate_ecus = {}
self.num_ecu_found = 0

def scan(self, progress=None, label=None):
Expand All @@ -538,25 +543,19 @@ def scan(self, progress=None, label=None):

if options.simulation_mode:
if txa == "74B":
#can_response = "61 80 82 00 14 97 39 04 33 33 30 40 50 54 87 04 00 05 00 06 00 00 00 00 00 00 01"
can_response = "61 80 82 00 14 97 39 04 33 33 30 40 50 54 87 04 00 05 00 01 00 00 00 00 00 00 01"
elif txa == "7E0":
can_response = "61 80 82 00 44 66 27 44 32 31 33 82 00 38 71 38 00 A7 74 00 56 05 02 01 00 00"
# Test approximate case
#can_response = "61 80 82 00 44 66 27 44 32 31 33 82 00 38 71 38 00 A7 74 00 56 05 02 01 00 00"
can_response = "61 80 82 00 44 66 27 44 32 31 33 82 00 38 71 38 00 A7 75 00 56 05 02 01 00 00"
else:
can_response = "61 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
else:
can_response = options.elm.request(req='2180', positive='61', cache=False)

if len(can_response) > 59:
diagversion = str(int(can_response[21:23],16))
supplier = can_response[24:32].replace(' ', '').decode('hex')
soft = can_response[48:53].replace(' ', '')
version = can_response[54:59].replace(' ', '')

for target in self.ecu_database.targets:
if target.checkWith(diagversion, supplier, soft, version, addr):
self.ecus[target.name] = target
self.num_ecu_found += 1
label.setText("Found %i ecu" % self.num_ecu_found)
self.check_ecu(can_response, label, addr)
options.elm.close_protocol()

def scan_kwp(self, progress=None, label=None):
if options.simulation_mode:
Expand All @@ -576,17 +575,45 @@ def scan_kwp(self, progress=None, label=None):
else:
continue

if len(can_response) > 59:
diagversion = str(int(can_response[21:23], 16))
supplier = can_response[24:32].replace(' ', '').decode('hex')
soft = can_response[48:53].replace(' ', '')
version = can_response[54:59].replace(' ', '')

for target in self.ecu_database.targets:
if target.checkWith(diagversion, supplier, soft, version, addr):
self.ecus[target.name] = target
self.num_ecu_found += 1
label.setText("Found %i ecu" % self.num_ecu_found)
self.check_ecu(can_response, label, addr)

options.elm.close_protocol()

def check_ecu(self, can_response, label, addr):
if len(can_response) > 59:
diagversion = str(int(can_response[21:23], 16))
supplier = can_response[24:32].replace(' ', '').decode('hex')
soft = can_response[48:53].replace(' ', '')
version = can_response[54:59].replace(' ', '')
approximate_ecu = []
found_exact = False
found_approximate = False

for target in self.ecu_database.targets:
if target.checkWith(diagversion, supplier, soft, version, addr):
self.ecus[target.name] = target
self.num_ecu_found += 1
label.setText("Found %i ecu" % self.num_ecu_found)
found_exact = True
break
elif target.checkApproximate(diagversion, supplier, soft, version, addr):
approximate_ecu.append(target)
found_approximate = True

# Try to find the closest possible version of an ECU
if not found_exact and found_approximate:
min_delta_version = 0xFFFFFF
kept_ecu = None
for tgt in approximate_ecu:
delta = abs(int('0x' + tgt.version, 16) - int('0x' + version, 16))
if delta < min_delta_version:
min_delta_version = delta
kept_ecu = tgt

if kept_ecu:
self.approximate_ecus[kept_ecu.name] = kept_ecu
self.num_ecu_found += 1
label.setText("Found %i ecu" % self.num_ecu_found)

if __name__ == '__main__':
ecur = Ecu_file("ecus/UCH_84_J84_04_00.xml", True)
Expand Down
62 changes: 33 additions & 29 deletions parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,16 @@ def setRefreshTime(self, value):
def initELM(self):
if not options.simulation_mode:
if self.protocol == 'CAN':
self.logview.append("Initialisation ELM en mode CAN")
ecu_conf = {'idTx': '', 'idRx': '', 'ecuname': str(self.ecu_name)}
options.elm.init_can()
options.elm.set_can_addr(self.ecu_addr, ecu_conf)
elif self.protocol == 'KWP2000':
self.logview.append("Initialisation ELM en mode KWP2000")
ecu_conf = {'idTx': '', 'idRx': '', 'ecuname': str(self.ecu_name), 'protocol': 'KWP2000'}
options.opt_si = not self.iso_fastinit
options.elm.init_iso()
options.elm.set_iso_addr(self.iso_send_id, ecu_conf)
options.opt_si = not self.iso_fastinit
else:
self.logview.append("Protocole " + self.protocol + " non supporte")

Expand Down Expand Up @@ -633,45 +635,47 @@ def readDTC(self):
if "MoreDTC" in sendbyte_dataitems:
moredtcbyte = sendbyte_dataitems["MoreDTC"].firstbyte - 1

dtclist = [0]
if moredtcbyte > 0:
dtclist = [i for i in range(0, 10)]

shiftbytecount = request.shiftbytescount
dtc_result = {}
dtc_num = 0
for dtcnum in dtclist:
time.sleep(0.1)
bytestosend = list(request.sentbytes.encode('ascii'))
if moredtcbyte != -1:
bytestosend[2 * moredtcbyte + 1] = hex(dtcnum)[-1:]
bytestosend = map(''.join, zip(*[iter(request.sentbytes.encode('ascii'))]*2))

while 1:
dtcread_command = ''.join(bytestosend)
# More DTC, pease
bytestosend[moredtcbyte] = "FF"
can_response = self.sendElm(dtcread_command)
dtc_num += 1

if "WRONG RESPONSE" in can_response:
continue
break

for k in request.dataitems.keys():
ecu_data = self.ecurequestsparser.data[k]
dataitem = request.dataitems[k]
value_hex = ecu_data.getHexValue(can_response, dataitem, request.endian)
while len(can_response) >= shiftbytecount + 2:
for k in request.dataitems.keys():
ecu_data = self.ecurequestsparser.data[k]
dataitem = request.dataitems[k]
value_hex = ecu_data.getHexValue(can_response, dataitem, request.endian)

if value_hex is None:
continue

if not dataitem.name in dtc_result:
dtc_result[dataitem.name] = []

value = int('0x' + value_hex, 16)
if value_hex is None:
continue

if ecu_data.scaled:
dtc_result[dataitem.name].append(str(value))
continue
if not dataitem.name in dtc_result:
dtc_result[dataitem.name] = []

if len(ecu_data.items) > 0 and value in ecu_data.lists:
dtc_result[dataitem.name].append(ecu_data.lists[value])
else:
dtc_result[dataitem.name].append(str(value))
value = int('0x' + value_hex, 16)

if ecu_data.scaled:
dtc_result[dataitem.name].append(str(value))
continue

if len(ecu_data.items) > 0 and value in ecu_data.lists:
dtc_result[dataitem.name].append(ecu_data.lists[value])
else:
dtc_result[dataitem.name].append(str(value))

can_response = can_response[shiftbytecount:]
if shiftbytecount == 0:
break

columns = dtc_result.keys()
self.table = gui.QTableWidget(None)
Expand Down

0 comments on commit cf1c68d

Please sign in to comment.