使用命令行解析库来解析参数
This commit is contained in:
146
kunlun.py
146
kunlun.py
@@ -11,6 +11,7 @@ import xmodem
|
||||
import os
|
||||
import binascii
|
||||
import threading
|
||||
import argparse
|
||||
from bin.log import myprint
|
||||
from bin.log import log_init
|
||||
from bin.log import mywrite
|
||||
@@ -141,6 +142,9 @@ def read_input(s_port:serial.Serial):
|
||||
read()
|
||||
_recv_thread=False
|
||||
|
||||
def recv_ser_end():
|
||||
global _recv_thread
|
||||
_recv_thread=False
|
||||
# 循环接收串口数据
|
||||
def recv_ser_data(s_port:serial.Serial):
|
||||
global _recv_thread
|
||||
@@ -262,9 +266,9 @@ def burn_fun():
|
||||
|
||||
burn_flash_bin(ser, modem, iot_flash_file)
|
||||
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)
|
||||
# time.sleep(3)
|
||||
# ser_read_data=ser.read(4096)
|
||||
# print_device_str(ser_read_data)
|
||||
|
||||
|
||||
# 配置要上传的镜像配置
|
||||
@@ -295,63 +299,73 @@ def global_def():
|
||||
global time_stamp_end
|
||||
global function_type
|
||||
global upload_key
|
||||
|
||||
arg_num=len(sys.argv)
|
||||
if(arg_num<=1):
|
||||
return
|
||||
global parser
|
||||
global user_log
|
||||
global log_timeout
|
||||
args = parser.parse_args()
|
||||
|
||||
init_str="WQKL"
|
||||
serial_com=sys.argv[1]
|
||||
serial_com=args.port
|
||||
b_rate=115200
|
||||
nb_rate=1500000
|
||||
def set_upload():
|
||||
global function_type
|
||||
if(sys.argv[3].endswith('.bin')):
|
||||
if(os.path.exists(sys.argv[3])):
|
||||
# 存在这个文件 是下载
|
||||
function_type='download'
|
||||
else:
|
||||
# 不存在这个文件 是上传
|
||||
function_type='upload'
|
||||
else:
|
||||
print("param err.")
|
||||
sys.exit(-1)
|
||||
if(arg_num==2):
|
||||
if(sys.argv[1].endswith('.bin')):
|
||||
function_type='convert'
|
||||
elif(arg_num==3):
|
||||
if(sys.argv[2].endswith('.bin')):
|
||||
function_type='ram'
|
||||
elif(arg_num==4):
|
||||
set_upload()
|
||||
upload_key="all"
|
||||
elif(arg_num==5):
|
||||
set_upload()
|
||||
upload_key=sys.argv[4]
|
||||
user_log=args.log
|
||||
log_timeout=args.timeout
|
||||
|
||||
# 上传或者下载flash镜像
|
||||
if(args.flash_file is not None):
|
||||
iot_flash_file=args.flash_file
|
||||
if(os.path.exists(iot_flash_file)):
|
||||
function_type='download'
|
||||
ram_file=f'kl{args.kunlun_version}_ram_build.bin'
|
||||
ram_file=os.path.join(bin_path(),ram_file)
|
||||
else:
|
||||
function_type='upload'
|
||||
ram_file=f'bootram_kl{args.kunlun_version}.bin'
|
||||
ram_file=os.path.join(bin_path(),ram_file)
|
||||
if(args.kunlun_version is None):
|
||||
print("请指定参数 -k")
|
||||
sys.exit(-1)
|
||||
if(args.upload_key is not None):
|
||||
upload_key = args.upload_key
|
||||
else:
|
||||
upload_key='all'
|
||||
elif(args.ram_file is not None):
|
||||
function_type='ram'
|
||||
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():
|
||||
help=f'''
|
||||
自动判断上传还是下载:
|
||||
{sys.argv[0]} [com] [kl1/kl3] [upload.bin/download.bin] <key>
|
||||
下载ram文件:
|
||||
{sys.argv[0]} [com] [ram.bin]
|
||||
转换bin文件:
|
||||
{sys.argv[0]} [file.bin]
|
||||
输入:
|
||||
{sys.argv[0]} -h
|
||||
查看帮助
|
||||
'''
|
||||
print(help)
|
||||
|
||||
@@ -364,6 +378,10 @@ if __name__ == '__main__':
|
||||
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
|
||||
upload_key=None
|
||||
parser=None
|
||||
user_log=False
|
||||
log_timeout=0
|
||||
parser_init()
|
||||
global_def()
|
||||
if(function_type is None):
|
||||
print("param err.")
|
||||
@@ -374,13 +392,21 @@ if __name__ == '__main__':
|
||||
bin_to_hex_file(iot_flash_file,calc_hex_name())
|
||||
sys.exit(0)
|
||||
|
||||
if(serial_com is None):
|
||||
myprint(f"请指定串口 {serial_com}")
|
||||
sys.exit(-1)
|
||||
try:
|
||||
ser = serial.Serial(port=serial_com, baudrate=b_rate, timeout=0.3)
|
||||
except Exception:
|
||||
myprint(f"serial com:{serial_com} open failed.")
|
||||
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模式
|
||||
init_send(ser, init_str)
|
||||
burn_ram_bin(modem, ram_file)
|
||||
@@ -391,9 +417,21 @@ if __name__ == '__main__':
|
||||
iot_flash_file=bin_file_decrypt(iot_flash_file)
|
||||
burn_fun()
|
||||
clear_tmp()
|
||||
if(user_log):
|
||||
ser.baudrate=b_rate
|
||||
recv_ser_data(ser)
|
||||
time.sleep(log_timeout)
|
||||
recv_ser_end()
|
||||
elif(function_type=='ram'):
|
||||
recv_ser_data(ser)
|
||||
read_input(ser)
|
||||
if(user_log):
|
||||
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__":
|
||||
# read_input(None)
|
||||
|
Reference in New Issue
Block a user