优化脚本 不再使用配置文件
This commit is contained in:
166
kunlun.py
166
kunlun.py
@@ -14,10 +14,12 @@ from log import myprint
|
||||
from log import log_init
|
||||
from bin_to_hex import bin_to_hex_file
|
||||
from bin_to_hex import bin_file_decrypt
|
||||
from bin_to_hex import clear_tmp
|
||||
|
||||
|
||||
def init_send(s_port:serial.Serial, send_str):
|
||||
def init_send(s_port:serial.Serial, send_str:str):
|
||||
s_info = bytearray()
|
||||
send_str=send_str.encode('utf-8')
|
||||
while True:
|
||||
s_port.write(send_str)
|
||||
time.sleep(0.1)
|
||||
@@ -29,29 +31,29 @@ def init_send(s_port:serial.Serial, send_str):
|
||||
m_ram=True
|
||||
else:
|
||||
m_ram=False
|
||||
|
||||
if m_ram:
|
||||
myprint ("Program enters transmission mode...")
|
||||
break
|
||||
|
||||
|
||||
def burn_ram_bin(x_modem:xmodem.XMODEM, r_file):
|
||||
global trans_time_0
|
||||
global time_stamp_end
|
||||
stime = datetime.datetime.now()
|
||||
myprint ("Transferring %s..." % r_file)
|
||||
try:
|
||||
stream = open(r_file, 'rb')
|
||||
except Exception:
|
||||
myprint("Cannot load file, please check the file path and retry. Press <enter> to exit")
|
||||
sys.exit()
|
||||
myprint(f"Cannot load file {r_file}")
|
||||
sys.exit(-1)
|
||||
myprint (f"Transferring {r_file}...")
|
||||
|
||||
xmodem_send = x_modem.send(stream, callback=download_callback)
|
||||
myprint('.')
|
||||
etime = datetime.datetime.now()
|
||||
trans_time_0 = (etime - stime).seconds
|
||||
myprint ("\nTransferring %s result: %s, consuming time: %s s \n" % (r_file, xmodem_send, trans_time_0))
|
||||
time_stamp_end = (etime - stime).seconds
|
||||
myprint (f"Transferring {r_file} result: {xmodem_send}, consuming time: {(time_stamp_end- time_stamp_start)} s ")
|
||||
|
||||
def burn_flash_bin(s_port:serial.Serial, x_modem:xmodem.XMODEM, f_file):
|
||||
global trans_time_1
|
||||
global time_stamp_end
|
||||
s_info = bytearray()
|
||||
while True:
|
||||
bytes2read = s_port.in_waiting
|
||||
@@ -70,20 +72,20 @@ def burn_flash_bin(s_port:serial.Serial, x_modem:xmodem.XMODEM, f_file):
|
||||
m_done=False
|
||||
|
||||
if m_flash:
|
||||
myprint (r"Recieving FLASH-IMAGE in xmodem : C")
|
||||
s_info = bytearray()
|
||||
stime = datetime.datetime.now()
|
||||
myprint ("Transferring %s..." % f_file)
|
||||
try:
|
||||
stream = open(f_file, 'rb')
|
||||
except Exception:
|
||||
myprint("Cannot load file, please check the file path and retry. Press <enter> to exit")
|
||||
sys.exit()
|
||||
myprint(f"Cannot load file {f_file}")
|
||||
sys.exit(-1)
|
||||
myprint (f"Transferring {f_file}...")
|
||||
|
||||
xmodem_send = x_modem.send(stream, quiet=True, callback=download_callback,retry=16)
|
||||
myprint('.')
|
||||
etime = datetime.datetime.now()
|
||||
trans_time_1 = (etime - stime).seconds
|
||||
myprint ("\nTransferring %s result: %s, consuming time: %d s \n" % (f_file,xmodem_send, trans_time_1))
|
||||
time_stamp_end = (etime - stime).seconds
|
||||
myprint (f"Transferring {f_file} result: {xmodem_send}, consuming time: {(time_stamp_end-time_stamp_start)} s ")
|
||||
if(xmodem_send is False):
|
||||
break
|
||||
elif m_done:
|
||||
@@ -116,19 +118,20 @@ def print_device_str(data:bytearray):
|
||||
|
||||
# 上传固件
|
||||
def upload_bin(x_modem:xmodem.XMODEM, w_file):
|
||||
global trans_time_0
|
||||
global time_stamp_end
|
||||
stime = datetime.datetime.now()
|
||||
myprint ("Transferring %s..." % w_file)
|
||||
try:
|
||||
stream = open(w_file, 'wb+')
|
||||
except Exception:
|
||||
myprint("Cannot load file, please check the file path and retry. Press <enter> to exit")
|
||||
sys.exit()
|
||||
myprint(f"Cannot cteate file {w_file}")
|
||||
sys.exit(-1)
|
||||
myprint (f"Receiving {w_file}..." )
|
||||
|
||||
xmodem_send = x_modem.recv(stream, callback=upload_callback)
|
||||
myprint('.')
|
||||
etime = datetime.datetime.now()
|
||||
trans_time_0 = (etime - stime).seconds
|
||||
myprint ("\nTransferring ram.bin result: %s, consuming time: %s s \n" % (xmodem_send, trans_time_0))
|
||||
time_stamp_end = (etime - stime).seconds
|
||||
myprint (f"Receiving {w_file} result: {xmodem_send}, consuming time: {(time_stamp_end-time_stamp_start)} s ")
|
||||
|
||||
|
||||
|
||||
@@ -152,20 +155,27 @@ if not os.path.exists(_work_dir):
|
||||
os.mkdir(_work_dir)
|
||||
|
||||
|
||||
# 日志文件 固定存放在work目录
|
||||
def calc_log_file_name():
|
||||
log_file=sys.argv[1].split('.')[0]+'.log'
|
||||
name=os.path.split(iot_flash_file)[-1]
|
||||
log_file=name+'.log'
|
||||
return os.path.join(_work_dir,log_file)
|
||||
|
||||
# 上传的flash镜像 固定存放在work目录
|
||||
def calc_upload_name():
|
||||
name=sys.argv[1]
|
||||
name=os.path.split(iot_flash_file)[-1]
|
||||
return os.path.join(_work_dir,name)
|
||||
|
||||
# 根据flash镜像生成的hex文件 固定存放在work目录
|
||||
def calc_hex_name():
|
||||
name=sys.argv[1]+'.txt'
|
||||
name=os.path.split(iot_flash_file)[-1]
|
||||
name=name+'.txt'
|
||||
return os.path.join(_work_dir,name)
|
||||
|
||||
# 上传的flash信息 固定存放在work目录
|
||||
def calc_flash_info_name():
|
||||
name=sys.argv[1].split('.')[0]+'.info'
|
||||
name=os.path.split(iot_flash_file)[-1]
|
||||
name=name+'.info'
|
||||
return os.path.join(_work_dir,name)
|
||||
|
||||
|
||||
@@ -193,7 +203,7 @@ def upload_fun():
|
||||
myprint(ser.read(4096).decode('utf-8'))
|
||||
upload_bin(modem,calc_upload_name())
|
||||
|
||||
myprint ("Total transmission time: %s s" % str(trans_time_0+trans_time_1))
|
||||
myprint (f"Total transmission time: {(time_stamp_start+time_stamp_end)} s" )
|
||||
myprint("Start transform bin to hex.")
|
||||
bin_to_hex_file(calc_upload_name(), calc_hex_name())
|
||||
myprint("Transform to hex end.")
|
||||
@@ -207,63 +217,81 @@ def burn_fun():
|
||||
ser.baudrate=nb_rate
|
||||
|
||||
burn_flash_bin(ser, modem, iot_flash_file)
|
||||
myprint ("Total transmission time: %s s" % str(trans_time_0+trans_time_1))
|
||||
myprint (f"Total transmission time: {(time_stamp_start+time_stamp_end)} s")
|
||||
time.sleep(3)
|
||||
ser_read_data=ser.read(4096)
|
||||
print_device_str(ser_read_data)
|
||||
|
||||
|
||||
|
||||
def global_def():
|
||||
global init_str
|
||||
global serial_com
|
||||
global b_rate
|
||||
global nb_rate
|
||||
global ram_file
|
||||
global iot_flash_file
|
||||
global ser
|
||||
global time_stamp_start
|
||||
global time_stamp_end
|
||||
global upload
|
||||
init_str="WQKL"
|
||||
serial_com=sys.argv[1]
|
||||
b_rate=115200
|
||||
nb_rate=1500000
|
||||
|
||||
# kunlun.py [upload.bin]
|
||||
|
||||
if(sys.argv[3]=='upload'):
|
||||
upload=True
|
||||
elif(sys.argv[3]=='download'):
|
||||
upload=False
|
||||
else:
|
||||
if(sys.argv[3].endswith('.bin')):
|
||||
if(os.path.exists(sys.argv[3])):
|
||||
# 存在这个文件 是下载
|
||||
upload=False
|
||||
else:
|
||||
# 不存在这个文件 是上传
|
||||
upload=True
|
||||
else:
|
||||
print("param err.")
|
||||
exit(-1)
|
||||
if(upload):
|
||||
ram_file=f'bootram_{sys.argv[2]}.bin'
|
||||
else:
|
||||
ram_file=f'{sys.argv[2]}_ram_build.bin'
|
||||
ram_file=os.path.join('bin',ram_file)
|
||||
iot_flash_file=sys.argv[-1]
|
||||
|
||||
|
||||
|
||||
|
||||
# 如果不指定上传还是下载 脚本会根据输入文件是否存在来决定上传还是下载
|
||||
# kunlun.py [com] [kl1/kl3] [upload.bin/download.bin]
|
||||
# kunlun.py [com] [kl1/kl3] [upload/download] [upload.bin/download.bin]
|
||||
if __name__ == '__main__':
|
||||
if(len(sys.argv)<2):
|
||||
if(len(sys.argv)<4):
|
||||
print("param too less")
|
||||
exit(-1)
|
||||
|
||||
upload=None
|
||||
time_stamp_start, time_stamp_end = 0, 0
|
||||
init_str, serial_com, b_rate, nb_rate, ram_file, iot_flash_file, ser = None, None, None, None, None, None, None
|
||||
global_def()
|
||||
log_init(calc_log_file_name())
|
||||
exit_flag = 0
|
||||
trans_time_0, trans_time_1 = 0, 0
|
||||
config_file = r"xmodem_config.txt"
|
||||
init_str, sp_num, b_rate, nb_rate, ram_file, iot_flash_file, ser = None, None, None, None, None, None, None
|
||||
for eachline in fileinput.FileInput(config_file):
|
||||
m_info = re.match(r"(\w+)\s*=\s*(.+)", eachline)
|
||||
if m_info:
|
||||
if m_info.group(1) == "init_str":
|
||||
init_str = m_info.group(2)
|
||||
elif m_info.group(1) == "serial_port_num":
|
||||
sp_num = int(m_info.group(2))
|
||||
elif m_info.group(1) == "baud_rate":
|
||||
b_rate = int(m_info.group(2))
|
||||
elif m_info.group(1) == "new_baud_rate":
|
||||
nb_rate = int(m_info.group(2))
|
||||
elif m_info.group(1) == "ram_file":
|
||||
ram_file = m_info.group(2)
|
||||
elif m_info.group(1) == "iot_flash_file":
|
||||
iot_flash_file = m_info.group(2)
|
||||
else:
|
||||
pass
|
||||
|
||||
try:
|
||||
ser = serial.Serial(port='COM' + str(sp_num), baudrate=b_rate, timeout=0.3)
|
||||
ser = serial.Serial(port=serial_com, baudrate=b_rate, timeout=0.3)
|
||||
except Exception:
|
||||
myprint("Serial Port COM%s Conflicts!!! Press <enter> to Close it and retry..." % str(sp_num))
|
||||
sys.exit()
|
||||
myprint(f"serial com:{serial_com} open failed.")
|
||||
sys.exit(-1)
|
||||
modem = xmodem.XMODEM(getc, putc, mode='xmodem1k')
|
||||
|
||||
# 发送启动字符让设备进入xmodem模式
|
||||
init_send(ser, init_str.encode("utf-8"))
|
||||
init_send(ser, init_str)
|
||||
burn_ram_bin(modem, ram_file)
|
||||
|
||||
# burn_ram_bin(modem, ram_file)
|
||||
# upload_fun()
|
||||
|
||||
burn_ram_bin(modem, "bin/kl3_ram_build.bin")
|
||||
dir_path="\\\\10.0.15.200\\内网拷贝进来的\\ran.chuan\\"
|
||||
for item in os.listdir(dir_path):
|
||||
if(item.endswith('.bin')):
|
||||
iot_flash_file=os.path.join(dir_path,item)
|
||||
break
|
||||
# iot_flash_file="C:\\new\\buildout\\kl3_sta_kunlun.bin"
|
||||
iot_flash_file="C:\\new\\buildout\\HZ-SG-TCE3PS03.bin"
|
||||
iot_flash_file=bin_file_decrypt(iot_flash_file)
|
||||
burn_fun()
|
||||
if(upload):
|
||||
upload_fun()
|
||||
else:
|
||||
iot_flash_file=bin_file_decrypt(iot_flash_file)
|
||||
burn_fun()
|
||||
clear_tmp()
|
||||
|
Reference in New Issue
Block a user