本次程序是基于上一次的更新,代码注释请见上一篇文章,上一次的校验为CRC_32输入取反,输出取反的类型,本次代码是CRC_32输入未取反,输出未取反的类型,请自己选择。
上篇文章链接如下
详细代码描述
本次流程图有所改变,见下
代码如下
/*@!Encoding:936*/
includes
{
}
variables
{
long mNewPage;
message 0x711 ecu_rx_phy;
message 0x700 ecu_rx_fun;
message 0x791 ecu_rx;
message 0x711 ecu_rx_3E00;
msTimer TesterPresent_3E;
msTimer boot_order;
msTimer countinue_36; //为防止打印跟不上速度
int file_check_num; //判断有几个文件需要刷写
int boot_check=1;
int TesterPresent_3E_start=0;
byte make_key[4],send_key[4];
int step_control; //用来判断进行到哪一步 ,因为有多个返回值相同,用于区分
long fileHandle = 0; //获取文件句柄
long file_line=0; //行计数
const int bufferSize=100; //s19文件行数据长度
char buffer[bufferSize]; //行字节数
byte file_byte_all[4]; //文件字节总数
byte start_address[4]; //起始地址
int choice; //选择文件
byte file_send_all[1025]; //将s19行里面的数据进行读取
long file_send_all_byte=0; //进行行字节计数
long file_send_all_byte_line=0; //进行读取行的读取字节数计数
long file_send_max; //每次允许发送字节数
int end_flag=0; //结束标志位,1代表结束,用来判断文件是否读取完成
byte file_send_step=0x01; //发送文件,一共进行了几次
int Suppress_negative_response=0; //36服务执行过程中可能会有否定响应,
long msg_time_control=0; //报文发送时间太短,设置相当于for函数i++使用
msTimer msg_time_1ms; //设置报文间隔1ms发送一条
byte mcg_byt0=0x20; //控制21-2f循环发送报文
int DE_1001=0; //1001判断标志
dword crc =0xFFFFFFFF;
dword Crc32Table[256];
}
on timer TesterPresent_3E
{
if(@sysvar::env_set::startBtn == 1)
{
ecu_rx_3E00.dlc=8;
ecu_rx_3E00.byte(0)=0x02;
ecu_rx_3E00.byte(1)=0x3E;
ecu_rx_3E00.byte(2)=0x00;
output(ecu_rx_3E00);
// setTimer(TesterPresent_3E,1300);
}
else
{
write("TesterPresent Stop");
}
}
on timer countinue_36
{
file_send_message();TransferData_36();
}
on timer boot_order
{
switch (boot_check)
{
case 1: //进入1003
Init_ecu_rx_phy();SwitchToExtendedSession_1003();break;
case 2://检查预编程条件
Init_ecu_rx_phy();RoutineControl_31010203();break;
case 3://控制DTC关闭
Init_ecu_rx_phy();ControlDTCSetting_8502();break;
case 4://通信控制,禁止收发非诊断报文
Init_ecu_rx_phy();CommunicationControl_2803();break;
case 5://读写数据 待定
break;
case 6: //进入编程会话
Init_ecu_rx_phy();ProgrammingSession_1002();break;
case 7://过安全访问
Init_ecu_rx_phy();SecurityAccess_27();break;
case 14://擦除内存
Init_ecu_rx_phy();Get_Address_and_byteNum();
if(choice==1)
{
eraseMemory_3101FF00();
}
else
{
boot_check=8;
}
break;
case 8://请求下载
Init_ecu_rx_phy();RequestDownload_34();break;
case 9://传输数据
Suppress_negative_response=1;file_send_message();TransferData_36();break;
case 10://请求退出传输
RequestTransferExit_37();break;
case 15://检查编程完整性
CheckProgrammingIntegrity_31010202();break;
case 11://检查编程依赖性
Init_ecu_rx_phy();voidRoutineControl_3101FF01();break;
case 12://硬件复位
Init_ecu_rx_phy();EcuReset_1101();break;
case 13://切换默认会话
Init_ecu_rx_phy();DefaultSession_1001();break;
}
if(@sysvar::env_set::startBtn==1)
{
setTimer(boot_order,100);
}
}
on sysvar sysvar::env_set::startBtn
{
char file_path_app[100];
char file_path_driver[100];
sysGetVariableString(sysvar::files_space::app_path,file_path_app, elCount(file_path_app)); //获取文件路径
sysGetVariableString(sysvar::files_space::driverPath,file_path_driver, elCount(file_path_driver)); //获取文件路径
if(@sysvar::env_set::startBtn==1)
{
writeLineEx(mNewPage,1,"开始测试");
if(file_path_app[1]!=':' || file_path_driver[1]!=':')
{
file_check_num=0;
putValueToControl("bootpanel","OutputTrace","请选择文件再开始\r");
}
else
{
choice=0;
file_check_num=2;
}
make_table();
if(file_check_num!=0)
{
file_send_step=0x01;
boot_check=1;
setTimer(boot_order,1000);
DeleteControlContent("bootpanel","OutputTrace");
setTimer(TesterPresent_3E,1000);
putValueToControl("bootpanel","OutputTrace","进入默认会话\r");
ecu_rx_phy.dlc=8;
ecu_rx_phy.byte(0)=0x02;
ecu_rx_phy.byte(1)=0x10;
ecu_rx_phy.byte(2)=0x01;
output(ecu_rx_phy);
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
}
else
{
putValueToControl("bootpanel","OutputTrace","刷写停止\r");
}
}
on start
{
writeConfigure(mNewPage, 20, 1, "H:\\bootloader_log.txt");
writeclear(1);
}
dword bitrev( dword input, int bw)//反转
{
int i;
dword var;
var = 0;
for(i=0;i<bw;i++)
{
if(input & 0x01)
{
var |= 1<<(bw-1-i);
}
input>>=1;
}
return var;
}
void make_table()//制作码表
{
int i;
int j;
qword c;
qword poly=0x4C11DB7;
poly = bitrev(poly, 32);
for (i = 0; i<256; i++) // 0x00-0xFF
{
c = bitrev(i,8);
for (j = 0; j<8; j++) // bit 0-7
{
if (c & 1)
{
c = poly ^ (c >> 1);
}
else
{
c = c >> 1;
}
}
Crc32Table[i] = bitrev(c,32);
writeLineEx(mNewPage,1,"%08x ",Crc32Table[i] );
// write(" %02x 0x%08x ",i, Crc32Table[i]); //码表打印
}
}
//1003
void SwitchToExtendedSession_1003()
{
putValueToControl("bootpanel","OutputTrace","进入预编程阶段\r");
writeLineEx(mNewPage,1,"进入预编程阶段");
putValueToControl("bootpanel","OutputTrace","进入扩展会话\r");
writeLineEx(mNewPage,1,"进入扩展会话");
ecu_rx_phy.dlc=8;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x02;
ecu_rx_phy.byte(1)=0x10;
ecu_rx_phy.byte(2)=0x03;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//3101 0203
void RoutineControl_31010203()
{
putValueToControl("bootpanel","OutputTrace","检查预编程条件\r");
writeLineEx(mNewPage,1,"检查预编程条件");
ecu_rx_phy.dlc=8;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x04;
ecu_rx_phy.byte(1)=0x31;
ecu_rx_phy.byte(2)=0x01;
ecu_rx_phy.byte(3)=0xFF;
ecu_rx_phy.byte(4)=0x02;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//85 02
void ControlDTCSetting_8502()
{
putValueToControl("bootpanel","OutputTrace","DTC关闭\r");
writeLineEx(mNewPage,1,"DTC关闭");
ecu_rx_phy.dlc=8;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x02;
ecu_rx_phy.byte(1)=0x85;
ecu_rx_phy.byte(2)=0x02;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//28
void CommunicationControl_2803()
{
putValueToControl("bootpanel","OutputTrace","通信控制,禁止收发非诊断报文\r");
writeLineEx(mNewPage,1,"通信控制");
ecu_rx_phy.dlc=8;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x03;
ecu_rx_phy.byte(1)=0x28;
ecu_rx_phy.byte(2)=0x03;
ecu_rx_phy.byte(3)=0x03;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//22 可选
void ReadDataByIdentifier_22()
{
}
void ProgrammingSession_1002()
{
putValueToControl("bootpanel","OutputTrace","进入主编程阶段\r");
putValueToControl("bootpanel","OutputTrace","进入编程会话\r");
writeLineEx(mNewPage,1,"进入主编程阶段");
writeLineEx(mNewPage,1,"进入编程会话");
ecu_rx_phy.dlc=8;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x02;
ecu_rx_phy.byte(1)=0x10;
ecu_rx_phy.byte(2)=0x02;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
void SecurityAccess_27()
{
putValueToControl("bootpanel","OutputTrace","过安全访问\r");
writeLineEx(mNewPage,1,"过安全访问");
ecu_rx_phy.dlc=8;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x02;
ecu_rx_phy.byte(1)=0x27;
ecu_rx_phy.byte(2)=0x09;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
void Init_ecu_rx_phy() //初始化ecu_rx_phy
{
int i=0;
boot_check=0;
for(i=0;i<8;i++)
{
ecu_rx_phy.byte(i)=0;
}
}
void SecurityAccess_make_key_And_sendkey() //已经做处理
{
int64 seed=0x00;
long seed_cheek=0x00;
long MASK=0x22004222;
byte sendkey_check[4];
int i;
seed=make_key[0]*16*16*16*16*16*16+make_key[1]*16*16*16*16+make_key[2]*16*16+make_key[3];
seed = seed << 1;
write("seed key 0x%x",seed);
seed_cheek=seed;
for(i=0;i<10;i++)
{
send_key[0]=seed>>24;
send_key[1]=(seed>>16)%0x100;
send_key[2]=(seed>>8)%0x100;
send_key[3]=seed%0x100;
sendkey_check[0]=seed_cheek>>24;
sendkey_check[1]=(seed_cheek>>16)%0x100;
sendkey_check[2]=(seed_cheek/256)%0x100;
sendkey_check[3]=seed_cheek%0x100;
if(send_key[0]==sendkey_check[0] && send_key[0]==sendkey_check[0] && send_key[0]==sendkey_check[0] && send_key[0]==sendkey_check[0])
{
break;
}
}
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x06;
ecu_rx_phy.byte(1)=0x27;
ecu_rx_phy.byte(2)=0x0A;
ecu_rx_phy.byte(3)=send_key[0];
ecu_rx_phy.byte(4)=send_key[1];
ecu_rx_phy.byte(5)=send_key[2];
ecu_rx_phy.byte(6)=send_key[3];
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//获取字节长度和起始地址 //34服务用
void Get_Address_and_byteNum()
{
long fileReadStatus;
long file_size;
int i=0;
char file_path[100];
long file_byte;
writeLineEx(mNewPage,1,"文件句柄 %d",choice);
file_byte=0;
if(choice==0)
{
sysGetVariableString(sysvar::files_space::driverPath,file_path, elCount(file_path)); //获取文件路径
fileHandle = OpenFileRead(file_path,0); //获取文件句柄
}
else
{
sysGetVariableString(sysvar::files_space::app_path,file_path, elCount(file_path)); //获取文件路径
fileHandle = OpenFileRead(file_path,0); //获取文件句柄
}
//获取总字符数
while(1)
{
Init_file_buffer();
Filegetstringsz(buffer,elcount(buffer), fileHandle);
if(buffer[0]==0)
{
break;
}
i++;
if(Type_conversion_char_byte(buffer[0])==0xFF && Type_conversion_char_byte(buffer[1])==0x03)
{
file_byte=file_byte+Type_conversion_char_byte(buffer[2])*16+Type_conversion_char_byte(buffer[3])-5;
}
}
fileClose (fileHandle);
//file_line=i;
file_byte_all[3]=file_byte%0x100;
file_byte_all[2]=file_byte>>8%0x100;
file_byte_all[1]=file_byte>>16%0x100;
file_byte_all[0]=file_byte>>24;
//获取起始地址
if(choice==0)
{
sysGetVariableString(sysvar::files_space::driverPath,file_path, elCount(file_path)); //获取文件路径
fileHandle = OpenFileRead(file_path,0); //获取文件句柄
}
else
{
sysGetVariableString(sysvar::files_space::app_path,file_path, elCount(file_path)); //获取文件路径
fileHandle = OpenFileRead(file_path,0); //获取文件句柄
}
while(1)
{
Init_file_buffer();
Filegetstringsz(buffer,elcount(buffer), fileHandle);
if(buffer[0]==0)
{
break;
}
if(Type_conversion_char_byte(buffer[0])==0xFF && Type_conversion_char_byte(buffer[1])==0x03)
{
start_address[0]=Type_conversion_char_byte(buffer[4])*0x10+Type_conversion_char_byte(buffer[5]);
start_address[1]=Type_conversion_char_byte(buffer[6])*0x10+Type_conversion_char_byte(buffer[7]);
start_address[2]=Type_conversion_char_byte(buffer[8])*0x10+Type_conversion_char_byte(buffer[9]);
start_address[3]=Type_conversion_char_byte(buffer[10])*0x10+Type_conversion_char_byte(buffer[11]);
write("起始地址 %x %x %x %x",start_address[0],start_address[1],start_address[2],start_address[3]);
break;
}
}
fileClose (fileHandle);
}
void eraseMemory_3101FF00()
{
step_control=14;
putValueToControl("bootpanel","OutputTrace","擦除内存\r");
writeLineEx(mNewPage,1,"擦除内存");
Init_ecu_rx_phy();
ecu_rx_phy.dlc=8;
ecu_rx_phy.byte(0)=0x10;
ecu_rx_phy.byte(1)=0x0D;
ecu_rx_phy.byte(2)=0x31;
ecu_rx_phy.byte(3)=0x01;
ecu_rx_phy.byte(4)=0xFF;
ecu_rx_phy.byte(5)=0x00;
ecu_rx_phy.byte(6)=0x44;
ecu_rx_phy.byte(7)=start_address[0];
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
void eraseMemory_3101FF00_continue()
{
Init_ecu_rx_phy();
ecu_rx_phy.dlc=8;
ecu_rx_phy.byte(0)=0x21;
ecu_rx_phy.byte(1)=start_address[1];
ecu_rx_phy.byte(2)=start_address[2];
ecu_rx_phy.byte(3)=start_address[3];
ecu_rx_phy.byte(4)=file_byte_all[0];
ecu_rx_phy.byte(5)=file_byte_all[1];
ecu_rx_phy.byte(6)=file_byte_all[2];
ecu_rx_phy.byte(7)=file_byte_all[3];
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
void RequestDownload_34()
{
step_control=8;
putValueToControl("bootpanel","OutputTrace","请求下载\r");
writeLineEx(mNewPage,1,"请求下载");
Init_ecu_rx_phy();
ecu_rx_phy.dlc=8;
ecu_rx_phy.byte(0)=0x10;
ecu_rx_phy.byte(1)=0x0B;
ecu_rx_phy.byte(2)=0x34;
ecu_rx_phy.byte(3)=0x00;
ecu_rx_phy.byte(4)=0x44;
ecu_rx_phy.byte(5)=start_address[0];
ecu_rx_phy.byte(6)=start_address[1];
ecu_rx_phy.byte(7)=start_address[2];
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
void Get_Address_and_byteNum_continue()
{
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x21;
ecu_rx_phy.byte(1)=start_address[3];
ecu_rx_phy.byte(2)=file_byte_all[0];
ecu_rx_phy.byte(3)=file_byte_all[1];
ecu_rx_phy.byte(4)=file_byte_all[2];
ecu_rx_phy.byte(5)=file_byte_all[3];
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
step_control=0;
}
//读取文件数据位,并赋值 36服务用
void file_send_message()
{
long fileReadStatus;
long file_size;
int i=0;
char file_path[100];
int file_byte;
int file_start_line;
int file_line_byte_num=0;
file_start_line=file_line;
//初始化file_send_all
for(file_send_all_byte=0;file_send_all_byte< elCount(file_send_all);file_send_all_byte++)
{
file_send_all[file_send_all_byte]=0x00;
}
file_send_all_byte=0;
if(choice==0)
{
sysGetVariableString(sysvar::files_space::driverPath,file_path, elCount(file_path)); //获取文件路径
fileHandle = OpenFileRead(file_path,0); //获取文件句柄
}
else
{
sysGetVariableString(sysvar::files_space::app_path,file_path, elCount(file_path)); //获取文件路径
fileHandle = OpenFileRead(file_path,0); //获取文件句柄
}
for(file_line=1;file_line<file_start_line;file_line++)
{
Filegetstringsz(buffer,elcount(buffer), fileHandle);
}
//
for(file_line;;file_line++)
{
Init_file_buffer();
Filegetstringsz(buffer,elcount(buffer), fileHandle);
if(file_line>=file_start_line)
{
if(Type_conversion_char_byte(buffer[0])==0xFF && Type_conversion_char_byte(buffer[1])==0x03)
{
file_line_byte_num=Type_conversion_char_byte(buffer[2])*16+Type_conversion_char_byte(buffer[3]);
}
else
{
file_line_byte_num=0;
}
}
//读取到空行 结束
if(buffer[0]==0 && file_line>=file_start_line)
{
write("结束的行数 %d %d %d",file_line,file_send_max,file_send_all_byte);
end_flag=1;
break;
}
//判断数据是不是超过设定的长度
if(file_send_all_byte+file_line_byte_num-5 > file_send_max)
{
file_send_all_byte_line=0;
for(i=12;file_send_max-2-file_send_all_byte>0;i=i+2)
{
file_send_all[file_send_all_byte]=Type_conversion_char_byte(buffer[i])*16+Type_conversion_char_byte(buffer[i+1]);
file_send_all_byte++;
file_send_all_byte_line++;
}
break;
}
//将每一行数据输入到file_send_all字符数组里
if(file_line_byte_num != 0)
{
for(i=12+file_send_all_byte_line*2;i<12+(file_line_byte_num-5)*2;i=i+2)
{
file_send_all[file_send_all_byte]=Type_conversion_char_byte(buffer[i])*16+Type_conversion_char_byte(buffer[i+1]);
file_send_all_byte++;
}
file_send_all_byte_line=0;
}
}
}
dword crc32(byte data[], dword len)//无返转
{
dword i =0;
for (i = 0; i < len; i++)
{
crc = Crc32Table[((crc>>24)^data[i]) & 0xff] ^ (crc << 8);
}
writeLineEx(mNewPage,1,"CRC %08x",crc);
return crc;
}
void TransferData_36()
{
int i=0;
crc32(file_send_all,file_send_all_byte);
step_control=9;
write("结束的行数 %d %d %d",file_line,file_send_max,file_send_all_byte);
putValueToControl("bootpanel","OutputTrace","数据传输\r");
writeLineEx(mNewPage,1,"数据传输");
//writeLineEx(mNewPage,1,"结束的行数 %d %d %d",file_line,file_send_max,file_send_all_byte);
Init_ecu_rx_phy();
ecu_rx_phy.dlc=8;
ecu_rx_phy.byte(0)=0x10+(file_send_all_byte>>8);
ecu_rx_phy.byte(1)=(file_send_all_byte%0x100)+0x02;
ecu_rx_phy.byte(2)=0x36;
ecu_rx_phy.byte(3)=file_send_step;
ecu_rx_phy.byte(4)=file_send_all[0];
ecu_rx_phy.byte(5)=file_send_all[1];
ecu_rx_phy.byte(6)=file_send_all[2];
ecu_rx_phy.byte(7)=file_send_all[3];
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
file_send_step=file_send_step+0x01;
}
void TransferData_36_continue()
{
int i;
step_control=0;
write("进入36continue %x",ecu_rx_phy.byte(0));
mcg_byt0=0x20;
msg_time_control=0;
setTimer(msg_time_1ms,1);
}
on timer msg_time_1ms
{
Init_ecu_rx_phy();
if(mcg_byt0>=0x2F)
{
mcg_byt0=0X20;
}
else
{
mcg_byt0=mcg_byt0+0x01;
}
ecu_rx_phy.byte(0)=mcg_byt0;
ecu_rx_phy.byte(1)=file_send_all[4+msg_time_control*7];
ecu_rx_phy.byte(2)=file_send_all[5+msg_time_control*7];
ecu_rx_phy.byte(3)=file_send_all[6+msg_time_control*7];
ecu_rx_phy.byte(4)=file_send_all[7+msg_time_control*7];
ecu_rx_phy.byte(5)=file_send_all[8+msg_time_control*7];
ecu_rx_phy.byte(6)=file_send_all[9+msg_time_control*7];
ecu_rx_phy.byte(7)=file_send_all[10+msg_time_control*7];
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
if(10+msg_time_control*7>=file_send_all_byte-1)
{
//数据发送完成,停发报文,因为之前设置,懒得改,相当于for函数的break;
writeLineEx(mNewPage,1,"%d %d",10+msg_time_control*7,file_send_all_byte-1 );
}
else
{
msg_time_control++;
setTimer(msg_time_1ms,30);
}
}
void voidRoutineControl_3101FF01()
{
putValueToControl("bootpanel","OutputTrace","检查编程依赖性\r");
writeLineEx(mNewPage,1,"检查编程依赖性");
ecu_rx_phy.dlc=8;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x04;
ecu_rx_phy.byte(1)=0x31;
ecu_rx_phy.byte(2)=0x01;
ecu_rx_phy.byte(3)=0xFF;
ecu_rx_phy.byte(4)=0x01;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
void EcuReset_1101()
{
putValueToControl("bootpanel","OutputTrace","电控单元复位\r");
writeLineEx(mNewPage,1,"电控单元复位");
ecu_rx_phy.dlc=8;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x02;
ecu_rx_phy.byte(1)=0x11;
ecu_rx_phy.byte(2)=0x01;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
void DefaultSession_1001()
{
putValueToControl("bootpanel","OutputTrace","切换默认会话\r");
writeLineEx(mNewPage,1,"切换默认会话");
ecu_rx_phy.dlc=8;
DE_1001=1;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x02;
ecu_rx_phy.byte(1)=0x10;
ecu_rx_phy.byte(2)=0x01;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
void Init_file_buffer()
{
int i;
for(i=0;i< elCount(buffer);i++ )
{
buffer[i]=0;
}
}
byte Type_conversion_char_byte(char buffer_one)
{
byte buffer_return;
if(buffer_one<71 && buffer_one>64)
{
buffer_return=buffer_one-55;
}
else if(buffer_one<58 && buffer_one>47)
{
buffer_return=buffer_one-48;
}
else if(buffer_one=='S')
{
buffer_return=0xFF;
}
return buffer_return;
}
void RequestTransferExit_37()
{
putValueToControl("bootpanel","OutputTrace","请求退出传输\r");
writeLineEx(mNewPage,1,"请求退出传输");
Init_ecu_rx_phy();
ecu_rx_phy.dlc=8;
ecu_rx_phy.byte(0)=0x01;
ecu_rx_phy.byte(1)=0x37;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
void CheckProgrammingIntegrity_31010202()
{
step_control=15;
putValueToControl("bootpanel","OutputTrace","检查编程完整性\r");
writeLineEx(mNewPage,1,"检查编程完整性");
Init_ecu_rx_phy();
crc = crc ^ 0xFFFFFFFF;
writeLineEx(mNewPage,1,"CRC %08x",crc);
ecu_rx_phy.dlc=8;
ecu_rx_phy.byte(0)=0x10;
ecu_rx_phy.byte(1)=0x08;
ecu_rx_phy.byte(2)=0x31;
ecu_rx_phy.byte(3)=0x01;
ecu_rx_phy.byte(4)=0xF0;
ecu_rx_phy.byte(5)=0x01;
ecu_rx_phy.byte(6)=crc>>24;
ecu_rx_phy.byte(7)=(crc>>16)%0x100;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
void CheckProgrammingIntegrity_31010202_continue()
{
Init_ecu_rx_phy();
ecu_rx_phy.dlc=8;
ecu_rx_phy.byte(0)=0x21;
ecu_rx_phy.byte(1)=(crc>>8)%0x100;
ecu_rx_phy.byte(2)=crc%0x100;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
on message 0x791
{
if(!(this.byte(1)==0x7E && this.byte(2)== 0x00 && this.byte(0)==0x02))
{
putValueToControl("bootpanel","OutputTrace",this,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
writeLineEx(mNewPage,1,"%x %2x %2x %2x %2x %2x %2x %2x %2x",this.id,this.byte(0),this.byte(1),this.byte(2),this.byte(3),this.byte(4),this.byte(5),this.byte(6),this.byte(7));
writeLineEx(mNewPage,1," ");
if(this.byte(1)==0x50 && ecu_rx_phy.byte(2)==0x03 )//1003返回值
{
boot_check=2;
}
else if(this.byte(1)==0x71 && (( ecu_rx_phy.byte(3)==0xFF && ecu_rx_phy.byte(4)==0x02 )||(ecu_rx_phy.byte(3)==0x02 && ecu_rx_phy.byte(4)==0x03)) )//31 01 02 03返回值
{
boot_check=3;
}
else if(this.byte(1)==0xC5 && this.byte(2)==0x02) //8502返回值
{
boot_check=4;
}
else if(this.byte(1)==0x68 && this.byte(2)==0x03) //28 03 返回值
{
boot_check=6;
}
else if(this.byte(1)==0x50 && this.byte(2)==0x02) //10 02 返回值
{
boot_check=7;
}
else if(this.byte(1)==0x67 && this.byte(2)==0x09) // 27 09 返回值
{
make_key[0]=this.byte(3);make_key[1]=this.byte(4);make_key[2]=this.byte(5);make_key[3]=this.byte(6);
SecurityAccess_make_key_And_sendkey();
}
else if(this.byte(1)==0x67 && this.byte(2)==0x0A) //27 0A 返回值
{
boot_check=14;
}
else if(this.byte(1)==0x71 && this.byte(2)==0x01 && this.byte(3)==0xFF && this.byte(4)==0x00)
{
boot_check=8;
}
else if(this.byte(1)==0x74 ) //34010203返回值
{
boot_check=9;file_send_max = this.byte(3)*0x100 + this.byte(4);
}
else if(this.byte(1)==0x76 && end_flag==1) //传输完成
{
end_flag=0;boot_check=10;
file_send_step=0x01;
file_line=0;
}
else if(this.byte(1)==0x76 && end_flag!=1) //继续传输
{
setTimer(countinue_36,3);
}
else if(this.byte(1)==0x77) //退出传输
{
Suppress_negative_response=0;
boot_check=15;
}
else if(this.byte(1)==0x71 && this.byte(2)==0x01 && ((this.byte(3)==0xF0 && this.byte(4)==0x01)||(this.byte(3)==0x02 && this.byte(4)==0x02)))
{
crc =0xFFFFFFFF;
if(choice==0)
{
choice++;
boot_check=14;
}
else
{
boot_check=11;
}
}
else if(this.byte(0)==0x30 && this.byte(1)==0x00)
{
switch(step_control)
{
case 8:Get_Address_and_byteNum_continue();break;
case 9:TransferData_36_continue();break;
case 14:eraseMemory_3101FF00_continue();break;
case 15:CheckProgrammingIntegrity_31010202_continue();break;
}
}
else if(this.byte(1)==0x71 && ( (ecu_rx_phy.byte(3)==0xFF && ecu_rx_phy.byte(4)==0x01)) )//31 01 FF 01返回值
{
boot_check=12;
}
if(this.byte(1)==0x51)
{
boot_check=13;
}
if(this.byte(1)==0x50 && DE_1001==1)
{
putValueToControl("bootpanel","OutputTrace","刷写成功");
writeLineEx(mNewPage,1,"刷写成功");
DE_1001=0;
}
else if(this.byte(1)==0x7F) //否定响应
{
if( this.byte(3)== 0x78)
{
putValueToControl("bootpanel","OutputTrace","等待继续\r");
}
else
{
putValueToControl("bootpanel","OutputTrace","刷写失败\r");
}
}
}
}
本代码仅供参考交流,如有需要原工程,请联系,有偿给予。也可联系做一些项目。
如有引用请备注链接