9q terminal V2

This commit is contained in:
shrlnm 2019-12-08 12:44:43 +03:00
parent 05201af0e0
commit 4ec29342b3
5 changed files with 297 additions and 38 deletions

View File

@ -0,0 +1,81 @@
# QR25 Idle speed correction
$addr = 7A
can500 # init can macro
# check ECU
exit_if_not 83 26 8 FF 44
exit_if_not 83 26 17 FF 43
:checkeng
goto_if 1201 5 4 FFFF 0000 runeng
goto engisrun
:runeng
cls
#
# Run ENGINE
#
if_key q end
wait 1
goto checkeng
:engisrun
cls
var lastResponse = $lastResponse
#
# Idle Speed
#
value 1201 5 4 FFFF 25 0 2
#
# ECU is running
#
# Select correction
#
# Press:
# 0 reset
# 1 +25
# 2 +50
# 3 +75
# 4 +100
#
# q for Exit
#
if_key 0 key0
if_key 1 key1
if_key 2 key2
if_key 3 key3
if_key 4 key4
if_key q end
wait 1
goto engisrun
:key0
3B0200
goto engisrun
:key1
3B0202
goto engisrun
:key2
3B0204
goto engisrun
:key3
3B0206
goto engisrun
:key4
3B0208
goto engisrun
:end
exit

View File

@ -0,0 +1,7 @@
# terminal
$addr = 7A
can500 # init can macro
session 10C0

View File

@ -196,19 +196,20 @@ class ECU:
flist.sort() flist.sort()
dumpname = os.path.join("./dumps/", flist[-1]) dumpname = os.path.join("./dumps/", flist[-1])
print 'Loading dump:', dumpname #print 'Loading dump:', dumpname
df = open(dumpname,'rt') self.elm.loadDump(dumpname)
lines = df.readlines() #df = open(dumpname,'rt')
df.close() #lines = df.readlines()
#df.close()
for l in lines: #
l = l.strip().replace('\n','') #for l in lines:
if l.count(':')==1: # l = l.strip().replace('\n','')
req,rsp = l.split(':') # if l.count(':')==1:
ecudump[req] = rsp # req,rsp = l.split(':')
# ecudump[req] = rsp
self.elm.setDump( ecudump ) #
#self.elm.setDump( ecudump )
def get_st( self, name ): def get_st( self, name ):
if name not in self.States.keys(): if name not in self.States.keys():

View File

@ -618,7 +618,25 @@ class ELM:
def setDump(self, ecudump): def setDump(self, ecudump):
""" define ecudum for demo mode""" """ define ecudum for demo mode"""
self.ecudump = ecudump self.ecudump = ecudump
def loadDump(self, dumpname):
print 'Loading dump:', dumpname
df = open(dumpname, 'rt')
lines = df.readlines()
df.close()
ecudump = {}
for l in lines:
l = l.strip().replace('\n', '')
if l.count(':') == 1:
req, rsp = l.split(':')
ecudump[req] = rsp
self.setDump(ecudump)
def debugMonitor(self): def debugMonitor(self):
byte = "" byte = ""
try: try:

View File

@ -21,6 +21,9 @@ stack = []
auto_macro = "" auto_macro = ""
auto_dia = False auto_dia = False
debug_mode = False
key_pressed = ''
mod_globals.os = os.name mod_globals.os = os.name
@ -175,6 +178,7 @@ def optParser():
global auto_macro global auto_macro
global auto_dia global auto_dia
global debug_mode
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
#usage = "%prog -p <port> [options]", #usage = "%prog -p <port> [options]",
@ -238,6 +242,13 @@ def optParser():
default=False, default=False,
action="store_true") action="store_true")
parser.add_argument("--debug",
help="for debug purpose only",
dest="dbg",
default=False,
action="store_true")
options = parser.parse_args() options = parser.parse_args()
if not options.port and mod_globals.os != 'android': if not options.port and mod_globals.os != 'android':
@ -260,6 +271,7 @@ def optParser():
mod_globals.opt_cfc0 = options.cfc mod_globals.opt_cfc0 = options.cfc
mod_globals.opt_n1c = options.n1c mod_globals.opt_n1c = options.n1c
auto_dia = options.dia auto_dia = options.dia
debug_mode = options.dbg
class FileChooser(): class FileChooser():
lay = '''<?xml version="1.0" encoding="utf-8"?> lay = '''<?xml version="1.0" encoding="utf-8"?>
@ -426,9 +438,17 @@ def play_macro(mname, elm):
stack.remove(mname) stack.remove(mname)
def term_cmd( c, elm ):
global var
rsp = elm.request(c, cache=False)
#rsp = elm.cmd(rcmd)
var['$lastResponse'] = rsp
return rsp
def bit_cmd( l, elm, fnc='set_bits' ): def bit_cmd( l, elm, fnc='set_bits' ):
error_msg = '''ERROR: command should have 5 parameters: <lid> <rsp_len> <offset> <hex mask> <hex value>" error_msg1 = '''ERROR: command should have 5 parameters:
<command> <lid> <rsp_len> <offset> <hex mask> <hex value>
<lid> - ECUs local identifier. Length should be 2 simbols for KWP or 4 for CAN <lid> - ECUs local identifier. Length should be 2 simbols for KWP or 4 for CAN
<rsp_len> - lengt of command response including positive response bytes, equals MinBytes from ddt db <rsp_len> - lengt of command response including positive response bytes, equals MinBytes from ddt db
<offset> - offeset in bytes to first changed byte (starts from 1 not 0) <offset> - offeset in bytes to first changed byte (starts from 1 not 0)
@ -437,16 +457,55 @@ def bit_cmd( l, elm, fnc='set_bits' ):
<hex mask> and <hex value> should have equal length <hex mask> and <hex value> should have equal length
''' '''
error_msg2 = '''ERROR: command should have 6 parameters:
<command> <lid> <rsp_len> <offset> <hex mask> <hex value> <label>
<lid> - ECUs local identifier. Length should be 2 simbols for KWP or 4 for CAN
<rsp_len> - lengt of command response including positive response bytes, equals MinBytes from ddt db
<offset> - offeset in bytes to first changed byte (starts from 1 not 0)
<hex mask> - bit mask for changed bits, 1 - changable, 0 - untachable
<hex value> - bit value
<label> - label to go
<hex mask> and <hex value> should have equal length
if fnc not in ['set_bits','xor_bits','exit_if','exit_if_not']: '''
error_msg3 = '''ERROR: command should have 6 parameters:
<command> <lid> <rsp_len> <offset> <hex mask> <hex value> <label>
<lid> - ECUs local identifier. Length should be 2 simbols for KWP or 4 for CAN
<rsp_len> - lengt of command response including positive response bytes, equals MinBytes from ddt db
<offset> - offeset in bytes to first changed byte (starts from 1 not 0)
<hex mask> - bit mask for changed bits, 1 - changable, 0 - untachable
<val step> - value step
<val offset> - value offset
<val divider> - value divider
'''
if fnc not in ['set_bits','xor_bits','exit_if','exit_if_not'] and \
fnc not in ['goto_if','goto_if_not', 'value']:
print "\nERROR: Unknown function\n" print "\nERROR: Unknown function\n"
return return
par = l.strip().split(' ') par = l.strip().split(' ')
if len(par)!=5:
print error_msg
return
if fnc in ['set_bits','xor_bits','exit_if','exit_if_not']:
error_msg = error_msg1
if len(par)!=5:
print error_msg
return
if fnc in ['goto_if','goto_if_not']:
error_msg = error_msg2
if len(par)!=6:
print error_msg
return
if fnc in ['value']:
error_msg = error_msg3
if len(par)==4:
par = par + ['1','0','1']
if len(par)!=7:
print error_msg
return
try: try:
lid = par[0].strip() lid = par[0].strip()
@ -454,11 +513,22 @@ def bit_cmd( l, elm, fnc='set_bits' ):
off = int(par[2].strip())-1 off = int(par[2].strip())-1
mask = par[3].strip() mask = par[3].strip()
val = par[4].strip() val = par[4].strip()
if fnc in ['goto_if', 'goto_if_not']:
go = par[5].strip()
if fnc in ['value']:
val = '0'*len(mask)
stp = par[4].strip()
ofs = par[5].strip()
div = par[6].strip()
except: except:
print error_msg print error_msg
return return
if len(lid) not in [2,4] or (len(mask)!=len(val)) or off<0 or off>lng: if len(lid) in [2,4] and off>=0 and off<=lng:
if fnc not in ['value'] and (len(mask)!=len(val)):
print error_msg
return
else:
print error_msg print error_msg
return return
@ -467,10 +537,11 @@ def bit_cmd( l, elm, fnc='set_bits' ):
else: #CAN else: #CAN
rcmd = '22' + lid rcmd = '22' + lid
rsp = elm.cmd(rcmd) rsp = term_cmd( rcmd, elm )
rsp = rsp.replace(' ','')[:lng*2].upper() rsp = rsp.replace(' ','')[:lng*2].upper()
print "read value:",rsp if fnc not in ['value']:
print "read value:",rsp
if len(rsp) != lng * 2: if len(rsp) != lng * 2:
print '\nERROR: Length is unexpected\n' print '\nERROR: Length is unexpected\n'
@ -493,6 +564,7 @@ def bit_cmd( l, elm, fnc='set_bits' ):
diff = 0 diff = 0
i = 0 i = 0
int_val = 0
while i<len(mask)/2: while i<len(mask)/2:
c_by = int(rsp[(off+i)*2:(off+i)*2+2],16) c_by = int(rsp[(off+i)*2:(off+i)*2+2],16)
c_ma = int(mask[i*2:i*2+2],16) c_ma = int(mask[i*2:i*2+2],16)
@ -503,6 +575,8 @@ def bit_cmd( l, elm, fnc='set_bits' ):
elif fnc == 'set_bits': elif fnc == 'set_bits':
n_by = (c_by & ~c_ma) | c_va n_by = (c_by & ~c_ma) | c_va
else: else:
n_by = c_by & c_ma
int_val = int_val * 256 + n_by
if (c_by & c_ma) != (c_va & c_ma): if (c_by & c_ma) != (c_va & c_ma):
diff += 1 diff += 1
i += 1 i += 1
@ -516,32 +590,67 @@ def bit_cmd( l, elm, fnc='set_bits' ):
if fnc == 'exit_if': if fnc == 'exit_if':
if diff==0: if diff==0:
print "\n Match. Exit \n" print "Match. Exit"
sys.exit() sys.exit()
else: else:
print "\n Not match. Continue \n" print "Not match. Continue"
return return
if fnc == 'exit_if_not': if fnc == 'exit_if_not':
if diff!=0: if diff!=0:
print "\n Not match. Exit \n" print "Not match. Exit"
sys.exit() sys.exit()
else: else:
print "\n Match. Continue \n" print "Match. Continue"
return return
if fnc == 'goto_if':
if diff==0:
print "Match. goto:", go
return go
else:
print "Not match. Continue"
return
if fnc == 'goto_if_not':
if diff!=0:
print "Not match. goto:", go
return go
else:
print "Match. Continue"
return
if fnc == 'value':
res = (int_val*float(stp)+float(ofs))/float(div)
print '# LID(',lid,') =', res
return
if rsp[:2]=='61': if rsp[:2]=='61':
wcmd = '3B'+rsp[2:] wcmd = '3B'+rsp[2:]
elif rsp[:2]=='62': elif rsp[:2]=='62':
wcmd = '2E'+rsp[2:] wcmd = '2E'+rsp[2:]
print "write value:", wcmd print "write value:", wcmd
print elm.cmd(wcmd) print term_cmd( wcmd, elm )
def wait_kb( ttw ):
global key_pressed
st = time.time()
kb = mod_utils.KBHit()
while(time.time()<(st+ttw)):
if kb.kbhit():
key_pressed = kb.getch()
time.sleep(0.1)
kb.set_normal_term()
def proc_line( l, elm ): def proc_line( l, elm ):
global macro global macro
global var global var
global cmd_delay global cmd_delay
global key_pressed
if '#' in l: if '#' in l:
@ -554,7 +663,6 @@ def proc_line( l, elm ):
return return
if len(l) == 0: if len(l) == 0:
print
return return
if l in ['q', 'quit', 'e', 'exit', 'end']: if l in ['q', 'quit', 'e', 'exit', 'end']:
@ -564,6 +672,10 @@ def proc_line( l, elm ):
print_help() print_help()
return return
if l in ['cls']:
mod_utils.clearScreen()
return
if l in macro.keys(): if l in macro.keys():
play_macro(l, elm) play_macro(l, elm)
return return
@ -588,18 +700,25 @@ def proc_line( l, elm ):
if vu in var.keys(): if vu in var.keys():
l = re.sub("\\" + vu, var[vu], l) l = re.sub("\\" + vu, var[vu], l)
else: else:
print 'Error: unknown variable', vu, 'in', mname print 'Error: unknown variable', vu
return return
m = re.search('\$\S+', l) m = re.search('\$\S+', l)
l_parts = l.split() l_parts = l.split()
if len(l_parts) > 0 and l_parts[0] in ['wait', 'sleep']: if len(l_parts) > 0 and l_parts[0] in ['wait', 'sleep']:
try: try:
time.sleep(int(l_parts[1])) wait_kb(int(l_parts[1]))
return return
except: except:
pass pass
if len(l_parts) > 0 and l_parts[0] in ['ses', 'session']:
try:
elm.startSession = l_parts[1]
l = l_parts[1]
except:
pass
if len(l_parts) > 0 and l_parts[0] in ['delay']: if len(l_parts) > 0 and l_parts[0] in ['delay']:
cmd_delay = int(l_parts[1]) cmd_delay = int(l_parts[1])
return return
@ -620,24 +739,48 @@ def proc_line( l, elm ):
bit_cmd( l.lower()[7:], elm, fnc='exit_if' ) bit_cmd( l.lower()[7:], elm, fnc='exit_if' )
return return
if l.lower().startswith('goto_if_not'):
go = bit_cmd( l.lower()[11:], elm, fnc='goto_if_not' )
return go
if l.lower().startswith('goto_if'):
go = bit_cmd( l.lower()[7:], elm, fnc='goto_if' )
return go
if l.lower().startswith('if_key'):
if len(l_parts) != 3 or l_parts[1] != key_pressed:
return
else:
key_pressed = ''
return l_parts[2]
if l.lower().startswith('value'):
val = bit_cmd( l.lower()[5:], elm, fnc='value' )
return
if len(l_parts) > 0 and l_parts[0] in ['go','goto']: if len(l_parts) > 0 and l_parts[0] in ['go','goto']:
print l print l
return l_parts[1] return l_parts[1]
if len(l_parts) > 0 and l_parts[0] in ['var','variable']:
print l
return
if l.lower().startswith('_'): if l.lower().startswith('_'):
print elm.send_raw(l[1:]) print elm.send_raw(l[1:])
else: else:
print elm.cmd(l) print term_cmd( l, elm )
if cmd_delay>0: if cmd_delay>0:
print '# delay:', cmd_delay print '# delay:', cmd_delay
time.sleep(cmd_delay) wait_kb(cmd_delay)
def main(): def main():
global auto_macro global auto_macro
global auto_dia global auto_dia
global debug_mode
global macro global macro
global var global var
@ -649,23 +792,23 @@ def main():
optParser() optParser()
#debug
#mod_globals.opt_demo = True
# disable auto flow control # disable auto flow control
mod_globals.opt_cfc0 = True mod_globals.opt_cfc0 = True
print 'Opening ELM' print 'Opening ELM'
elm = mod_elm.ELM( mod_globals.opt_port, mod_globals.opt_speed, True ) elm = mod_elm.ELM( mod_globals.opt_port, mod_globals.opt_speed, True )
# change serial port baud rate
elm.port.check_elm()
if mod_globals.opt_speed < mod_globals.opt_rate and not mod_globals.opt_demo:
elm.port.soft_boudrate(mod_globals.opt_rate)
elm.currentaddress = '7A' elm.currentaddress = '7A'
elm.currentprotocol = 'can' elm.currentprotocol = 'can'
cmd_lines = [] cmd_lines = []
cmd_ref = 0 cmd_ref = 0
#debug
#auto_dia = True
if auto_dia: if auto_dia:
fname = FileChooser().choose() fname = FileChooser().choose()
#debug #debug
@ -675,6 +818,15 @@ def main():
cmd_lines = f.readlines() cmd_lines = f.readlines()
f.close() f.close()
if debug_mode:
mod_globals.opt_demo = True
elm.loadDump('./dumps/term_test.txt')
fname = './macro/test/test.cmd'
if len(fname)>0:
f = open(fname, 'rt')
cmd_lines = f.readlines()
f.close()
if auto_macro != '': if auto_macro != '':
if auto_macro in macro.keys(): if auto_macro in macro.keys():
play_macro( auto_macro, elm ) play_macro( auto_macro, elm )