can_bootloader/main.aardio
2024-10-29 17:16:03 +08:00

543 lines
14 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import fonts.fontAwesome;
import win.ui;
/*DSG{{*/
mainForm = win.form(text="CAN_Bootloader";right=799;bottom=599;border="dialog frame";max=false)
mainForm.add(
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};
btnStopBoot={cls="button";text="停止
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};
plus={cls="plus";text="等待刷写
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};
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=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};
static4={cls="static";text="no.";left=39;top=102;right=141;bottom=122;align="right";transparent=1;z=11};
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}
)
/*}}*/
import win;
import usb2canfd;
import win.timer;
import win.inputBox;
import fsys.ini;
import console
// 获取当前时间
var currentDate = time.now()
// 设置格式化字符串为 "YYYYMMDD"
currentDate.format = "%Y%m%d"
side_now = "";
var bootstate = false;//boot状态标志
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);
Display = function(str){//显示日志
var nowtime = time();
nowtime.format="%H:%M:%S";
mainForm.edit.print(tostring(nowtime) + " " + str);
}
CANHw = usb2canfd.USB2CANHW();//尝试加载CAN dll
var ret = CANHw.LoadDll();
if(ret == 0){
Display("DLL加载成功");
Display("DLL loaded successfully
");
}
else {
Display("DLL加载失败");
Display("DLL loading failed
");
}
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);
}
//CAN通讯线程
import thread.command;
var listener = thread.command();
var flag_connected=0;
listener.$Display = function(str){
Display(str);
}
listener.$ShowPath = function(str){
//mainForm.edFile.text = str;
}
listener.$ShowFlashDrvPath = function(str){
//mainForm.edFile2.text = str;
}
listener.$SetProgress = function(pos){
if(pos >= 0 && pos <= 100){
mainForm.progress.pos = pos;
}
}
listener.$Connected = function(){
Display("设备连接成功");
Display("Device connection successful
");
thrdTable.设备连接状态 = 1;
}
listener.$SendEnd = function(isSuccess){
//timer_bootcount.disable();
var usetime = time.now().diffsecond(starttime);
bootstate = false;
if(isSuccess == true){
Display("刷写成功,用时 " + usetime + " S")
Display("Flashing successfully, took "+usetime+" S
");
mainForm.plus.text = "刷写完成\Flashing successfully";
//mainForm.plus.bgcolor = 0x32CD32;
mainForm.plus.background = 0xFF32CD32;
if(side_now == "L"){
左成功 += 1;
}
else {
右成功 += 1;
}
}
else {
Display("刷写失败");
Display("Flashing failed
");
mainForm.plus.text = "刷写失败 Flashing failed";
//mainForm.plus.bgcolor = 0x2200E3;
mainForm.plus.background = 0xFFE30022;
if(side_now == "L"){
左失败 += 1;
}
else {
右失败 += 1;
}
}
write_log_file(当前计数,isSuccess,VINCODE);
当前计数 += 1;
mainForm.static6.text = string.format("%05d",当前计数);
sec.当前计数 = 当前计数;
sec.左成功 = 左成功;
sec.左失败 = 左失败;
sec.右成功 = 右成功;
sec.右失败 = 右失败;
//sec.VIN = VINCODE;
sec.save();
}
import thread.table;
thrdTable = thread.table("多线程共享数据");
thrdTable.设备连接状态 = 0;
thrdTable.设备 = {};
thrdTable.DIDdata = null;
thrdTable.test = "123"
thrdTable["文件加载状态"] = false;
thrdTable["电源电压"] = 0;
//线程函数
var CanThread = function(){
//线程函数内部要添加自已的import语句
import win;
import console;
import thread.table;
thrdTable = thread.table("多线程共享数据");
//console.log("线程开始运行")
//console.log(t_thrdTable.test);
loadcodex("\user\CanThread.aardio");//加载线程程序
//在子线程启动消息循环
win.loopMessage(FuncLoopMsg)
}
var DiagBootReq = function(id){
::PostThreadMessage(thrdId,102,id,0)//自定义消息
mainForm.plus.text = "刷写中请勿断开电源或USB连接
During flashing, do not disconnect the power
or USB connection";
//mainForm.plus.bgcolor = 0x00CCFF;
mainForm.plus.background = 0xFF00CCFF;
}
var DiagStopReq = function(){
::PostThreadMessage(thrdId,103,0,0)//自定义消息
//mainForm.plus.text = "刷写停止";
//mainForm.plus.background = 0xFFC0DCC0
}
//import console;
//console.log("1");
mainForm.btnFlashLeft.oncommand = function(id,event){
if(thrdTable.设备连接状态 == 0){
Display("设备未连接");
Display("device not connected
");
return;
}
if(bootstate == true){
return ;
}
if(thrdTable["文件加载状态"] == false){
Display("烧录文件加载错误");
Display("Burning file loading error
");
return;
}
side_now = "L";
bootstate = true;
mainForm.edit.text = "";
Display("VIN:" ++ VINCODE ++ '\n');
::PostThreadMessage(thrdId,106,0x720,0x620);//自定义消息
Display(tostring(currentDate));
Display("左侧开始刷写流程");
Display('Start flashing process\n');
starttime = time.now();
DiagBootReq(1);//左侧
}
mainForm.onClose = function(hwnd,message,wParam,lParam){
raw.closehandle(thrdHandle)
}
autoconnect = win.timer(mainForm,300);
var autoconnect_state = 0;
var power_err_count = 0;
autoconnect.onTimer = function(){
select(autoconnect_state) {
case 0 {
if(thrdTable.设备连接状态 == 0){
//断开
mainForm.static.text='\uF127'
mainForm.static2.text = "设备未连接,请先连接设备 The device is not connected, please connect the device first";
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
}
}
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 {
//import console;
//console.log(thrdTable["电源电压"])
autoconnect_state = 3;
}
}
case 3 {
if(thrdTable["文件加载状态"] == true){
mainForm.static.color=0xff00;
mainForm.static.text='\uF0C1';
mainForm.static2.text = "设备已连接,可以开始刷写 The device is connected and can start flashing";
::PostThreadMessage(thrdId,115,1,0);//连接
autoconnect_state = 4;
mainForm.edVIN.disabled = false;
mainForm.btnConfirmVIN.disabled = false;
}
else {
mainForm.static2.text = "文件已损坏,请重新获取烧录上位机";
autoconnect_state = 4;
mainForm.btnFlashLeft.disabled = true;
mainForm.btnFlashRight.disabled = true;
}
}
case 4 {
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;
}
}
else {
}
}
}
mainForm.btnFlashRight.oncommand = function(id,event){
if(thrdTable.设备连接状态 == 0){
Display("设备未连接");
Display("device not connected
");
return;
}
if(bootstate == true){
return ;
}
if(thrdTable["文件加载状态"] == false){
Display("烧录文件加载错误");
Display("Burning file loading error
");
return;
}
side_now = "R";
bootstate = true;
mainForm.edit.text = "";
Display("VIN:" ++ VINCODE ++ '\n');
::PostThreadMessage(thrdId,106,0x710,0x610);//自定义消息
Display(tostring(currentDate));
Display("右侧开始刷写流程");
Display('Start flashing process \n');
starttime = time.now();
DiagBootReq(2);//
}
mainForm.btnStopBoot.oncommand = function(id,event){
DiagStopReq();
}
//import console;
//console.log("io.exis = ",io.exist("\res\P417_SWTL_20240709.S19"));
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;
}
}
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 );
autoconnect.enable();
//console.log(thrdId);
mainForm.show();
return win.loopMessage();