can_bootloader/main.aardio

409 lines
10 KiB
Plaintext
Raw Normal View History

2024-08-31 14:18:38 +08:00
import fonts.fontAwesome;
2022-05-14 15:57:52 +08:00
import win.ui;
/*DSG{{*/
2024-10-09 16:27:03 +08:00
mainForm = win.form(text="CAN_Bootloader";right=699;bottom=473;border="dialog frame";max=false)
2022-05-14 15:57:52 +08:00
mainForm.add(
2024-10-09 09:09:01 +08:00
btnFlashLeft={cls="button";text="左侧刷写
Left side flashing";left=30;top=129;right=168;bottom=243;z=2};
btnFlashRight={cls="button";text="右侧刷写
Right side flashing";left=181;top=131;right=319;bottom=245;z=7};
btnStopBoot={cls="button";text="停止
Stop";left=29;top=272;right=319;bottom=346;z=3};
2024-08-31 14:18:38 +08:00
edit={cls="edit";left=361;top=19;right=692;bottom=401;autovscroll=false;edge=1;multiline=1;vscroll=1;z=1};
2024-10-09 09:09:01 +08:00
plus={cls="plus";text="等待刷写
Waiting for flashing";left=33;top=375;right=316;bottom=460;bgcolor=12639424;z=10};
2024-08-31 14:18:38 +08:00
progress={cls="progress";left=361;top=435;right=689;bottom=463;edge=1;max=100;min=0;z=4};
static={cls="static";text='\uF127';left=43;top=30;right=114;bottom=98;align="center";center=1;color=255;font=LOGFONT(h=-56;name='FontAwesome');transparent=1;z=8};
static2={cls="static";text="设备未连接,请先连接设备";left=126;top=42;right=326;bottom=91;transparent=1;z=9};
2024-10-09 16:27:03 +08:00
static3={cls="static";text="刷写进度 Flashing writing progress";left=361;top=410;right=688;bottom=430;transparent=1;z=5};
static4={cls="static";text="no.";left=39;top=102;right=141;bottom=122;align="right";transparent=1;z=11};
static5={cls="static";text="V0.3_20241009";left=545;top=1;right=689;bottom=17;align="right";center=1;notify=1;transparent=1;z=6};
static6={cls="static";text="00000";left=145;top=102;right=203;bottom=120;transparent=1;z=12}
2022-05-14 15:57:52 +08:00
)
/*}}*/
import win;
import usb2canfd;
2022-05-18 10:43:03 +08:00
import win.timer;
2024-10-09 16:27:03 +08:00
import win.inputBox;
import fsys.ini;
import console
// 获取当前时间
var currentDate = time.now()
// 设置格式化字符串为 "YYYYMMDD"
currentDate.format = "%Y%m%d"
side_now = "";
2022-05-18 10:43:03 +08:00
2022-05-18 14:06:44 +08:00
var bootstate = false;//boot状态标志
2022-05-14 15:57:52 +08:00
2024-10-09 16:27:03 +08:00
var inifile=fsys.ini("\配置文件.ini");
//读取小节对象
sec = inifile.getSection("计数");
if(sec.当前计数 == null){
sec.当前计数 = 1;
sec.左成功 = 0;
sec.左失败 = 0;
sec.右成功 = 0;
sec.右失败 = 0;
//sec.VIN = "YSM4ZPAA0SF405299";
sec.save()
}
当前计数 = sec.当前计数;
左成功 = sec.左成功;
左失败 = sec.左失败;
右成功 = sec.右成功;
右失败 = sec.右失败;
//console.dump(sec);
mainForm.static6.text = string.format("%05d",当前计数);
//VINCODE = sec.VIN;
//console.log(VINCODE);
2022-05-18 14:06:44 +08:00
Display = function(str){//显示日志
2023-03-20 19:42:48 +08:00
var nowtime = time();
nowtime.format="%H:%M:%S";
mainForm.edit.print(tostring(nowtime) + " " + str);
2022-05-14 15:57:52 +08:00
}
2024-08-31 14:18:38 +08:00
2022-05-18 14:06:44 +08:00
CANHw = usb2canfd.USB2CANHW();//尝试加载CAN dll
2022-05-14 15:57:52 +08:00
var ret = CANHw.LoadDll();
if(ret == 0){
Display("DLL加载成功");
2024-10-09 09:09:01 +08:00
Display("DLL loaded successfully
");
2022-05-14 15:57:52 +08:00
}
2022-09-30 16:32:52 +08:00
else {
Display("DLL加载失败");
2024-10-09 09:09:01 +08:00
Display("DLL loading failed
");
2022-09-30 16:32:52 +08:00
}
2024-10-09 16:27:03 +08:00
var write_log_file = function(id,ifsuccess,vin){
if(ifsuccess){
success = "success";
}
else {
success = "failed";
}
var vinr6 = string.right(vin,6);
var logfile_name = string.format("\log\%05d_%s_%s_%s.txt",当前计数,success,vinr6,side_now);
//console.log(logfile_name);
//string.save(logfile_name,"VIN = " ++ vin);
string.save(logfile_name,'test');
string.save(logfile_name, mainForm.edit.text);
}
2022-05-14 15:57:52 +08:00
//CAN通讯线程
import thread.command;
var listener = thread.command();
2022-05-18 10:43:03 +08:00
var flag_connected=0;
2022-05-14 15:57:52 +08:00
listener.$Display = function(str){
Display(str);
}
2022-05-17 16:34:44 +08:00
listener.$ShowPath = function(str){
2024-08-31 14:18:38 +08:00
//mainForm.edFile.text = str;
2023-03-20 19:42:48 +08:00
2022-05-17 16:34:44 +08:00
}
2022-10-04 16:01:27 +08:00
listener.$ShowFlashDrvPath = function(str){
2024-08-31 14:18:38 +08:00
//mainForm.edFile2.text = str;
2022-10-04 16:01:27 +08:00
}
2022-05-18 10:43:03 +08:00
listener.$SetProgress = function(pos){
if(pos >= 0 && pos <= 100){
mainForm.progress.pos = pos;
}
}
listener.$Connected = function(){
2024-08-31 14:18:38 +08:00
Display("设备连接成功");
2024-10-09 09:09:01 +08:00
Display("Device connection successful
");
2024-08-31 14:18:38 +08:00
thrdTable.设备连接状态 = 1;
2022-05-18 10:43:03 +08:00
}
listener.$SendEnd = function(isSuccess){
2022-10-05 09:17:27 +08:00
//timer_bootcount.disable();
var usetime = time.now().diffsecond(starttime);
2022-05-18 14:06:44 +08:00
bootstate = false;
2022-05-18 10:43:03 +08:00
if(isSuccess == true){
2022-10-05 09:17:27 +08:00
Display("刷写成功,用时 " + usetime + " S")
2024-10-09 16:27:03 +08:00
Display("Flashing successfully, took "+usetime+" S
2024-10-09 09:09:01 +08:00
");
2024-10-09 16:27:03 +08:00
mainForm.plus.text = "刷写完成\Flashing successfully";
2024-08-31 14:18:38 +08:00
//mainForm.plus.bgcolor = 0x32CD32;
mainForm.plus.background = 0xFF32CD32;
2024-10-09 16:27:03 +08:00
if(side_now == "L"){
左成功 += 1;
}
else {
右成功 += 1;
}
2022-05-18 10:43:03 +08:00
}
2024-08-31 14:18:38 +08:00
else {
Display("刷写失败");
2024-10-09 09:09:01 +08:00
Display("Flashing failed
");
mainForm.plus.text = "刷写失败 Flashing failed";
2024-08-31 14:18:38 +08:00
//mainForm.plus.bgcolor = 0x2200E3;
mainForm.plus.background = 0xFFE30022;
2024-10-09 16:27:03 +08:00
if(side_now == "L"){
左失败 += 1;
}
else {
右失败 += 1;
}
2024-08-31 14:18:38 +08:00
}
2024-10-09 16:27:03 +08:00
write_log_file(当前计数,isSuccess,VINCODE);
当前计数 += 1;
mainForm.static6.text = string.format("%05d",当前计数);
sec.当前计数 = 当前计数;
sec.左成功 = 左成功;
sec.左失败 = 左失败;
sec.右成功 = 右成功;
sec.右失败 = 右失败;
//sec.VIN = VINCODE;
sec.save();
2022-05-18 10:43:03 +08:00
}
2024-08-31 14:18:38 +08:00
import thread.table;
thrdTable = thread.table("多线程共享数据");
thrdTable.设备连接状态 = 0;
thrdTable.设备 = {};
thrdTable.test = "123"
thrdTable["文件加载状态"] = false;
thrdTable["电源电压"] = 0;
2022-05-14 15:57:52 +08:00
//线程函数
var CanThread = function(){
//线程函数内部要添加自已的import语句
import win;
import console;
2024-08-31 14:18:38 +08:00
import thread.table;
thrdTable = thread.table("多线程共享数据");
2022-10-05 09:17:27 +08:00
//console.log("线程开始运行")
2024-08-31 14:18:38 +08:00
//console.log(t_thrdTable.test);
2022-05-14 15:57:52 +08:00
loadcodex("\user\CanThread.aardio");//加载线程程序
2024-08-31 14:18:38 +08:00
2022-05-14 15:57:52 +08:00
//在子线程启动消息循环
win.loopMessage(FuncLoopMsg)
}
2024-08-31 14:18:38 +08:00
var DiagBootReq = function(id){
::PostThreadMessage(thrdId,102,id,0)//自定义消息
2024-10-09 09:09:01 +08:00
mainForm.plus.text = "刷写中请勿断开电源或USB连接
During flashing, do not disconnect the power
or USB connection";
2024-08-31 14:18:38 +08:00
//mainForm.plus.bgcolor = 0x00CCFF;
mainForm.plus.background = 0xFF00CCFF;
2022-05-14 17:00:49 +08:00
}
var DiagStopReq = function(){
::PostThreadMessage(thrdId,103,0,0)//自定义消息
2024-08-31 14:18:38 +08:00
//mainForm.plus.text = "刷写停止";
//mainForm.plus.background = 0xFFC0DCC0
2022-05-14 17:00:49 +08:00
}
2022-05-14 15:57:52 +08:00
2024-08-31 14:18:38 +08:00
//import console;
//console.log("1");
mainForm.btnFlashLeft.oncommand = function(id,event){
2024-10-09 16:27:03 +08:00
2024-08-31 14:18:38 +08:00
if(thrdTable.设备连接状态 == 0){
Display("设备未连接");
2024-10-09 09:09:01 +08:00
Display("device not connected
");
2022-05-18 10:43:03 +08:00
return;
}
2022-05-18 14:06:44 +08:00
if(bootstate == true){
return ;
}
2024-08-31 14:18:38 +08:00
if(thrdTable["文件加载状态"] == false){
Display("烧录文件加载错误");
2024-10-09 09:09:01 +08:00
Display("Burning file loading error
");
2024-08-31 14:18:38 +08:00
return;
2023-03-20 19:42:48 +08:00
}
2024-10-09 16:27:03 +08:00
// 创建一个输入对话框
var inputBox = win.inputBox();
inputBox.info.text = "请输入VIN码";
inputBox.input.text = VINCODE;
inputBox.text = "输入VIN码";
// 显示输入对话框并获取用户输入
var result = inputBox.doModal();
if(result == null){
Display('请输入正确的VIN');
Display('Please enter the correct VIN\n')
return;
}
VINCODE = result;
side_now = "L";
2022-05-18 14:06:44 +08:00
bootstate = true;
2022-05-18 10:43:03 +08:00
mainForm.edit.text = "";
2024-10-09 16:27:03 +08:00
Display("VIN:" ++ VINCODE ++ '\n');
2024-08-31 14:18:38 +08:00
::PostThreadMessage(thrdId,106,0x720,0x620);//自定义消息
2024-10-09 16:27:03 +08:00
Display(tostring(currentDate));
Display("左侧开始刷写流程");
Display('Start flashing process\n');
2022-10-10 08:19:50 +08:00
starttime = time.now();
2024-08-31 14:18:38 +08:00
DiagBootReq(1);//左侧
2022-05-14 15:57:52 +08:00
}
2022-05-17 16:34:44 +08:00
2022-05-18 14:06:44 +08:00
mainForm.onClose = function(hwnd,message,wParam,lParam){
2024-08-31 14:18:38 +08:00
raw.closehandle(thrdHandle)
2022-10-12 17:01:37 +08:00
}
2023-03-20 19:42:48 +08:00
2022-05-14 15:57:52 +08:00
2024-08-31 14:18:38 +08:00
autoconnect = win.timer(mainForm,100);
var autoconnect_state = 0;
var power_err_count = 0;
autoconnect.onTimer = function(){
select(autoconnect_state) {
case 0 {
if(thrdTable.设备连接状态 == 0){
//断开
mainForm.static.text='\uF127'
2024-10-09 09:09:01 +08:00
mainForm.static2.text = "设备未连接,请先连接设备 The device is not connected, please connect the device first";
2024-08-31 14:18:38 +08:00
mainForm.static.color=0xff0;
::PostThreadMessage(thrdId,100,0,0);
//Display("刷新");
sleep(20);
if(#thrdTable.设备 > 0){
Display(thrdTable.设备[1].name);
::PostThreadMessage(thrdId,101,1,0x80);//连接 0x80 -> CANFD
2024-08-31 14:18:38 +08:00
}
}
else {
autoconnect_state = 3;
power_err_count = 0;
}
}
case 1 {
::PostThreadMessage(thrdId,104,0,0);
autoconnect_state = 2;
}
case 2 {
if(thrdTable["电源电压"] < 100){
::PostThreadMessage(thrdId,105,1,0);
power_err_count++;
if(power_err_count < 10){
autoconnect_state = 1;
}
else {
mainForm.static2.text = "电源异常,请检查线路"
autoconnect_state = 4;
}
}
else {
2024-10-09 16:27:03 +08:00
//import console;
//console.log(thrdTable["电源电压"])
2024-08-31 14:18:38 +08:00
autoconnect_state = 3;
}
}
case 3 {
if(thrdTable["文件加载状态"] == true){
mainForm.static.color=0xff00;
mainForm.static.text='\uF0C1';
2024-10-09 09:09:01 +08:00
mainForm.static2.text = "设备已连接,可以开始刷写 The device is connected and can start flashing";
2024-08-31 14:18:38 +08:00
::PostThreadMessage(thrdId,115,1,0);//连接
autoconnect_state = 4;
}
else {
mainForm.static2.text = "文件已损坏,请重新获取烧录上位机";
autoconnect_state = 4;
mainForm.btnFlashLeft.disabled = true;
mainForm.btnFlashRight.disabled = true;
}
}
case 4 {
//...
}
else {
}
2023-03-20 19:42:48 +08:00
}
2024-08-31 14:18:38 +08:00
2023-03-20 19:42:48 +08:00
}
2022-05-14 15:57:52 +08:00
2024-08-31 14:18:38 +08:00
mainForm.btnFlashRight.oncommand = function(id,event){
if(thrdTable.设备连接状态 == 0){
Display("设备未连接");
2024-10-09 09:09:01 +08:00
Display("device not connected
");
2024-08-31 14:18:38 +08:00
return;
}
if(bootstate == true){
return ;
}
if(thrdTable["文件加载状态"] == false){
Display("烧录文件加载错误");
2024-10-09 09:09:01 +08:00
Display("Burning file loading error
");
2024-08-31 14:18:38 +08:00
return;
2023-03-20 19:42:48 +08:00
}
2024-10-09 16:27:03 +08:00
// 创建一个输入对话框
var inputBox = win.inputBox();
inputBox.info.text = "请输入VIN码";
//inputBox.input.text = VINCODE;
inputBox.text = "输入VIN码";
// 显示输入对话框并获取用户输入
var result = inputBox.doModal();
if(result == null ){
Display('请输入正确的VIN');
Display('Please enter the correct VIN\n')
return;
}
VINCODE = result;
side_now = "R";
2024-08-31 14:18:38 +08:00
bootstate = true;
mainForm.edit.text = "";
2024-10-09 16:27:03 +08:00
Display("VIN:" ++ VINCODE ++ '\n');
2024-08-31 14:18:38 +08:00
::PostThreadMessage(thrdId,106,0x710,0x610);//自定义消息
2024-10-09 16:27:03 +08:00
Display(tostring(currentDate));
Display("右侧开始刷写流程");
2024-10-09 09:09:01 +08:00
Display("Start flashing process
");
2024-08-31 14:18:38 +08:00
starttime = time.now();
2024-10-09 16:27:03 +08:00
DiagBootReq(2);//
2023-03-20 19:42:48 +08:00
}
2022-05-14 15:57:52 +08:00
2024-08-31 14:18:38 +08:00
mainForm.btnStopBoot.oncommand = function(id,event){
DiagStopReq();
}
2024-08-31 14:18:38 +08:00
autoconnect.enable();
2024-08-31 14:18:38 +08:00
//import console;
//console.log("io.exis = ",io.exist("\res\P417_SWTL_20240709.S19"));
2024-10-09 16:27:03 +08:00
click_times = 0;
mainForm.static5.oncommand = function(id,event){
click_times+=1;
if(click_times >= 5){
var frmChild = mainForm.loadForm("\dlg\about.aardio");
frmChild.show();
click_times = 0;
}
}
2022-05-14 15:57:52 +08:00
thrdHandle,thrdId = thread.create( CanThread );
2022-05-14 15:57:52 +08:00
mainForm.show();
return win.loopMessage();