使用命令行解析库来解析参数
This commit is contained in:
146
kunlun.py
146
kunlun.py
@@ -11,6 +11,7 @@ import xmodem
|
|||||||
import os
|
import os
|
||||||
import binascii
|
import binascii
|
||||||
import threading
|
import threading
|
||||||
|
import argparse
|
||||||
from bin.log import myprint
|
from bin.log import myprint
|
||||||
from bin.log import log_init
|
from bin.log import log_init
|
||||||
from bin.log import mywrite
|
from bin.log import mywrite
|
||||||
@@ -141,6 +142,9 @@ def read_input(s_port:serial.Serial):
|
|||||||
read()
|
read()
|
||||||
_recv_thread=False
|
_recv_thread=False
|
||||||
|
|
||||||
|
def recv_ser_end():
|
||||||
|
global _recv_thread
|
||||||
|
_recv_thread=False
|
||||||
# 循环接收串口数据
|
# 循环接收串口数据
|
||||||
def recv_ser_data(s_port:serial.Serial):
|
def recv_ser_data(s_port:serial.Serial):
|
||||||
global _recv_thread
|
global _recv_thread
|
||||||
@@ -262,9 +266,9 @@ def burn_fun():
|
|||||||
|
|
||||||
burn_flash_bin(ser, modem, iot_flash_file)
|
burn_flash_bin(ser, modem, iot_flash_file)
|
||||||
myprint (f"Total transmission time: {(time_stamp_start+time_stamp_end)} s")
|
myprint (f"Total transmission time: {(time_stamp_start+time_stamp_end)} s")
|
||||||
time.sleep(3)
|
# time.sleep(3)
|
||||||
ser_read_data=ser.read(4096)
|
# ser_read_data=ser.read(4096)
|
||||||
print_device_str(ser_read_data)
|
# print_device_str(ser_read_data)
|
||||||
|
|
||||||
|
|
||||||
# 配置要上传的镜像配置
|
# 配置要上传的镜像配置
|
||||||
@@ -295,63 +299,73 @@ def global_def():
|
|||||||
global time_stamp_end
|
global time_stamp_end
|
||||||
global function_type
|
global function_type
|
||||||
global upload_key
|
global upload_key
|
||||||
|
global parser
|
||||||
arg_num=len(sys.argv)
|
global user_log
|
||||||
if(arg_num<=1):
|
global log_timeout
|
||||||
return
|
args = parser.parse_args()
|
||||||
|
|
||||||
init_str="WQKL"
|
init_str="WQKL"
|
||||||
serial_com=sys.argv[1]
|
serial_com=args.port
|
||||||
b_rate=115200
|
b_rate=115200
|
||||||
nb_rate=1500000
|
nb_rate=1500000
|
||||||
def set_upload():
|
user_log=args.log
|
||||||
global function_type
|
log_timeout=args.timeout
|
||||||
if(sys.argv[3].endswith('.bin')):
|
|
||||||
if(os.path.exists(sys.argv[3])):
|
# 上传或者下载flash镜像
|
||||||
# 存在这个文件 是下载
|
if(args.flash_file is not None):
|
||||||
function_type='download'
|
iot_flash_file=args.flash_file
|
||||||
else:
|
if(os.path.exists(iot_flash_file)):
|
||||||
# 不存在这个文件 是上传
|
function_type='download'
|
||||||
function_type='upload'
|
ram_file=f'kl{args.kunlun_version}_ram_build.bin'
|
||||||
else:
|
ram_file=os.path.join(bin_path(),ram_file)
|
||||||
print("param err.")
|
else:
|
||||||
sys.exit(-1)
|
function_type='upload'
|
||||||
if(arg_num==2):
|
ram_file=f'bootram_kl{args.kunlun_version}.bin'
|
||||||
if(sys.argv[1].endswith('.bin')):
|
ram_file=os.path.join(bin_path(),ram_file)
|
||||||
function_type='convert'
|
if(args.kunlun_version is None):
|
||||||
elif(arg_num==3):
|
print("请指定参数 -k")
|
||||||
if(sys.argv[2].endswith('.bin')):
|
sys.exit(-1)
|
||||||
function_type='ram'
|
if(args.upload_key is not None):
|
||||||
elif(arg_num==4):
|
upload_key = args.upload_key
|
||||||
set_upload()
|
else:
|
||||||
upload_key="all"
|
upload_key='all'
|
||||||
elif(arg_num==5):
|
elif(args.ram_file is not None):
|
||||||
set_upload()
|
function_type='ram'
|
||||||
upload_key=sys.argv[4]
|
ram_file=args.ram_file
|
||||||
|
iot_flash_file=ram_file
|
||||||
|
elif(args.bin_convert is not None):
|
||||||
|
function_type='convert'
|
||||||
|
iot_flash_file=args.bin_convert
|
||||||
|
elif(args.console is not None):
|
||||||
|
iot_flash_file=args.console
|
||||||
|
function_type='console'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def parser_init():
|
||||||
|
global parser
|
||||||
|
parser = argparse.ArgumentParser(description='kunlun相关工具脚本')
|
||||||
|
# 添加参数
|
||||||
|
parser.add_argument('-p','--port',action='store', help='输入使用的串口')
|
||||||
|
parser.add_argument('-r', '--ram_file', action='store', help='输入使用的ram程序')
|
||||||
|
parser.add_argument('-f', '--flash_file', action='store', help='输入使用的flash程序')
|
||||||
|
parser.add_argument('-c', '--console', action='store', help='指定控制台保存的文件名,不指定则不进入控制台')
|
||||||
|
parser.add_argument('-u','--upload_key',action='store',help='上传时保存分区的关键字,不指定则上传所有')
|
||||||
|
parser.add_argument('-k','--kunlun_version',action='store',help='上传下载时的kunlun版本 3或者1')
|
||||||
|
parser.add_argument('-b','--bin_convert',action='store',help='转换bin文件')
|
||||||
|
parser.add_argument('-l','--log',action='store_true',default=False, help='下载ram或flash程序之后是否进入log界面,默认否')
|
||||||
|
parser.add_argument('-t','--timeout',action='store',type=float,default=5,help='启用log时接收log的时间')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if(function_type=='upload'):
|
|
||||||
ram_file=f'bootram_{sys.argv[2]}.bin'
|
|
||||||
iot_flash_file=sys.argv[3]
|
|
||||||
ram_file=os.path.join(bin_path(),ram_file)
|
|
||||||
elif(function_type=='download'):
|
|
||||||
ram_file=f'{sys.argv[2]}_ram_build.bin'
|
|
||||||
iot_flash_file=sys.argv[3]
|
|
||||||
ram_file=os.path.join(bin_path(),ram_file)
|
|
||||||
elif(function_type=='ram'):
|
|
||||||
ram_file=sys.argv[2]
|
|
||||||
iot_flash_file=sys.argv[2]
|
|
||||||
elif(function_type=='convert'):
|
|
||||||
iot_flash_file=sys.argv[1]
|
|
||||||
|
|
||||||
|
|
||||||
def print_help():
|
def print_help():
|
||||||
help=f'''
|
help=f'''
|
||||||
自动判断上传还是下载:
|
输入:
|
||||||
{sys.argv[0]} [com] [kl1/kl3] [upload.bin/download.bin] <key>
|
{sys.argv[0]} -h
|
||||||
下载ram文件:
|
查看帮助
|
||||||
{sys.argv[0]} [com] [ram.bin]
|
|
||||||
转换bin文件:
|
|
||||||
{sys.argv[0]} [file.bin]
|
|
||||||
'''
|
'''
|
||||||
print(help)
|
print(help)
|
||||||
|
|
||||||
@@ -364,6 +378,10 @@ if __name__ == '__main__':
|
|||||||
time_stamp_start, time_stamp_end = 0, 0
|
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
|
init_str, serial_com, b_rate, nb_rate, ram_file, iot_flash_file, ser = None, None, None, None, None, None, None
|
||||||
upload_key=None
|
upload_key=None
|
||||||
|
parser=None
|
||||||
|
user_log=False
|
||||||
|
log_timeout=0
|
||||||
|
parser_init()
|
||||||
global_def()
|
global_def()
|
||||||
if(function_type is None):
|
if(function_type is None):
|
||||||
print("param err.")
|
print("param err.")
|
||||||
@@ -374,13 +392,21 @@ if __name__ == '__main__':
|
|||||||
bin_to_hex_file(iot_flash_file,calc_hex_name())
|
bin_to_hex_file(iot_flash_file,calc_hex_name())
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
if(serial_com is None):
|
||||||
|
myprint(f"请指定串口 {serial_com}")
|
||||||
|
sys.exit(-1)
|
||||||
try:
|
try:
|
||||||
ser = serial.Serial(port=serial_com, baudrate=b_rate, timeout=0.3)
|
ser = serial.Serial(port=serial_com, baudrate=b_rate, timeout=0.3)
|
||||||
except Exception:
|
except Exception:
|
||||||
myprint(f"serial com:{serial_com} open failed.")
|
myprint(f"serial com:{serial_com} open failed.")
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
modem = xmodem.XMODEM(getc, putc, mode='xmodem1k')
|
|
||||||
|
|
||||||
|
if(function_type=='console'):
|
||||||
|
recv_ser_data(ser)
|
||||||
|
read_input(ser)
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
modem = xmodem.XMODEM(getc, putc, mode='xmodem1k')
|
||||||
# 发送启动字符让设备进入xmodem模式
|
# 发送启动字符让设备进入xmodem模式
|
||||||
init_send(ser, init_str)
|
init_send(ser, init_str)
|
||||||
burn_ram_bin(modem, ram_file)
|
burn_ram_bin(modem, ram_file)
|
||||||
@@ -391,9 +417,21 @@ if __name__ == '__main__':
|
|||||||
iot_flash_file=bin_file_decrypt(iot_flash_file)
|
iot_flash_file=bin_file_decrypt(iot_flash_file)
|
||||||
burn_fun()
|
burn_fun()
|
||||||
clear_tmp()
|
clear_tmp()
|
||||||
|
if(user_log):
|
||||||
|
ser.baudrate=b_rate
|
||||||
|
recv_ser_data(ser)
|
||||||
|
time.sleep(log_timeout)
|
||||||
|
recv_ser_end()
|
||||||
elif(function_type=='ram'):
|
elif(function_type=='ram'):
|
||||||
recv_ser_data(ser)
|
if(user_log):
|
||||||
read_input(ser)
|
ser.baudrate=b_rate
|
||||||
|
recv_ser_data(ser)
|
||||||
|
time.sleep(log_timeout)
|
||||||
|
recv_ser_end()
|
||||||
|
else:
|
||||||
|
ser.baudrate=b_rate
|
||||||
|
recv_ser_data(ser)
|
||||||
|
read_input(ser)
|
||||||
|
|
||||||
# if __name__ == "__main__":
|
# if __name__ == "__main__":
|
||||||
# read_input(None)
|
# read_input(None)
|
||||||
|
Reference in New Issue
Block a user