can_bootloader/main.aardio

543 lines
14 KiB
Plaintext
Raw Permalink 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-29 17:16:03 +08:00
mainForm = win.form(text="CAN_Bootloader";right=799;bottom=599;border="dialog frame";max=false)
2022-05-14 15:57:52 +08:00
mainForm.add(
2024-10-29 17:16:03 +08:00
btnConfirmVIN={cls="button";text="确认 / confirm";left=263;top=137;right=402;bottom=171;disabled=1;z=16};
btnFlashLeft={cls="button";text="1.左侧刷写
1.Left side flashing";left=34;top=198;right=214;bottom=298;disabled=1;z=2};
btnFlashRight={cls="button";text="2.右侧刷写
2.Right side flashing";left=224;top=198;right=404;bottom=298;disabled=1;z=7};
2024-10-09 09:09:01 +08:00
btnStopBoot={cls="button";text="停止
2024-10-29 17:16:03 +08:00
Stop";left=31;top=396;right=405;bottom=470;z=3};
btnVerifyVersion={cls="button";text="3.验证
3.verify";left=31;top=310;right=404;bottom=384;disabled=1;z=13};
edVIN={cls="edit";text="000000";left=91;top=137;right=258;bottom=172;disabled=1;edge=1;font=LOGFONT(h=-16);z=14};
edit={cls="edit";left=421;top=21;right=792;bottom=510;autovscroll=false;edge=1;multiline=1;vscroll=1;z=1};
2024-10-09 09:09:01 +08:00
plus={cls="plus";text="等待刷写
2024-10-29 17:16:03 +08:00
Waiting for flashing";left=34;top=485;right=403;bottom=570;bgcolor=12639424;z=10};
progress={cls="progress";left=420;top=546;right=791;bottom=574;edge=1;max=100;min=0;z=4};
2024-08-31 14:18:38 +08:00
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};
2024-10-29 17:16:03 +08:00
static2={cls="static";text="设备未连接,请先连接设备";left=126;top=34;right=326;bottom=83;transparent=1;z=9};
static3={cls="static";text="刷写进度 Flashing writing progress";left=422;top=521;right=749;bottom=541;transparent=1;z=5};
2024-10-09 16:27:03 +08:00
static4={cls="static";text="no.";left=39;top=102;right=141;bottom=122;align="right";transparent=1;z=11};
2024-10-29 17:16:03 +08:00
static5={cls="static";text="V0.5_20241029";left=648;top=2;right=792;bottom=18;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};
static7={cls="static";text="VIN";left=31;top=137;right=78;bottom=172;align="right";center=1;font=LOGFONT(h=-16);transparent=1;z=15}
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.设备 = {};
2024-10-29 17:16:03 +08:00
thrdTable.DIDdata = null;
2024-08-31 14:18:38 +08:00
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-29 17:16:03 +08:00
2024-10-09 16:27:03 +08:00
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-10-29 17:16:03 +08:00
autoconnect = win.timer(mainForm,300);
2024-08-31 14:18:38 +08:00
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;
2024-10-29 17:16:03 +08:00
mainForm.edVIN.disabled = false;
mainForm.btnConfirmVIN.disabled = false;
2024-08-31 14:18:38 +08:00
}
else {
mainForm.static2.text = "文件已损坏,请重新获取烧录上位机";
autoconnect_state = 4;
mainForm.btnFlashLeft.disabled = true;
mainForm.btnFlashRight.disabled = true;
}
}
case 4 {
2024-10-29 17:16:03 +08:00
if(thrdTable.设备连接状态 == 0){
autoconnect_state = 0;
thrdTable.设备 = {};
mainForm.edVIN.disabled = true;
mainForm.btnFlashLeft.disabled = true;
mainForm.btnFlashRight.disabled = true;
mainForm.btnConfirmVIN.disabled = true;
mainForm.btnVerifyVersion.disabled = true;
}
2024-08-31 14:18:38 +08:00
}
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
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-29 17:16:03 +08:00
Display('Start flashing process \n');
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-10-29 17:16:03 +08:00
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
2024-10-29 17:16:03 +08:00
mainForm.btnConfirmVIN.oncommand = function(id,event){
var vinstr = mainForm.edVIN.text;
if(vinstr == null || string.len(vinstr)!=6){
Display("请输入VIN后6位数字");
Display('Please enter the last 6 digits of VIN \n');
}
else {
mainForm.btnFlashLeft.disabled = false;
mainForm.btnFlashRight.disabled = false;
mainForm.btnVerifyVersion.disabled = false;
mainForm.btnConfirmVIN.disabled = true;
mainForm.edVIN.disabled = true;
VINCODE = vinstr;
}
}
ReadDIDdata = function(did){
thrdTable.DIDdata = null;
::PostThreadMessage(thrdId,110,did,0);
//sleep(100);
//return thrdTable.DIDdata;
}
verifyTimerState = 0;
verifyTimer = win.timer(,100);
left_repeat_times = 0;
right_repeat_times = 0;
verifyTimer.onTimer = function(){
select(verifyTimerState) {
case 0 {
left_repeat_times = 0;
right_repeat_times = 0;
verifyTimerState += 1;
}
case 1 {
::PostThreadMessage(thrdId,106,0x720,0x620);//自定义消息
Display('读取左侧开关版本号');
Display('Read left SW Version: \n');
ReadDIDdata(0xf195);
verifyTimerState += 1;
}
case 2 {
swl = thrdTable.DIDdata;
if(swl != null){
if(string.cmp(swl,"SW0303",6) == 0){
Display('左侧版本号正确');
Display('left SW Version correct \n');
verifyTimerState += 1;
}
else {
Display('左侧版本号错误');
Display('left SW Version incorrect \n');
verifyTimerState = 6;
verifyTimer.disable();
}
}
else {//读到空数据
left_repeat_times+=1;
if(left_repeat_times > 3){
Display('左侧版本号错误');
Display('left SW Version incorrect \n');
verifyTimerState = 6;
verifyTimer.disable();
}
else {
verifyTimerState = 1;
}
}
}
case 3 {
::PostThreadMessage(thrdId,106,0x710,0x610);//自定义消息
Display('读取右侧开关版本号');
Display('Read right SW Version: \n');
ReadDIDdata(0xf195);
verifyTimerState += 1;
}
case 4 {
swr = thrdTable.DIDdata;
if(swr != null){
if(string.cmp(swr,"SW0303",6) == 0){
Display('右侧版本号正确');
Display('right SW Version correct \n');
verifyTimerState += 1;
}
else {
Display('右侧版本号错误');
Display('right SW Version incorrect \n');
verifyTimerState = 6;
verifyTimer.disable();
}
}
else {//读到空数据
right_repeat_times+=1;
if(right_repeat_times > 3){
Display('右侧版本号错误');
Display('right SW Version incorrect \n');
verifyTimerState = 6;
verifyTimer.disable();
}
else {
verifyTimerState = 3;
}
}
}
case 5{
Display('软件升级成功');
Display('Software upgrade successful \n');
mainForm.btnFlashLeft.disabled = true;
mainForm.btnFlashRight.disabled = true;
mainForm.edVIN.text = "";
mainForm.edVIN.disabled = false;
mainForm.btnVerifyVersion.disabled = true;
mainForm.btnConfirmVIN.disabled = false;
verifyTimerState += 1;
verifyTimer.disable();
if(VINCODE != null){
var savestr = string.format('VIN:%s ,time:%s ,success \n',VINCODE,tostring(time.now()));
string.save("\验证成功VIN记录.txt",savestr,true);
}
}
case 6{
}
else {
}
}
}
mainForm.btnVerifyVersion.oncommand = function(id,event){
verifyTimerState = 0;
verifyTimer.enable();
}
thrdHandle,thrdId = thread.create( CanThread );
2024-10-29 17:16:03 +08:00
autoconnect.enable();
//console.log(thrdId);
2022-05-14 15:57:52 +08:00
mainForm.show();
return win.loopMessage();