【零基础学习CAPL】——CAN报文的发送(CRC——ChecSum值)
/*@!Encoding:936*/
includes
{
}
variables
{
// 0x125相关定义
message Test_125 meg_125;
msTimer Timer_125;
long Cycle_Timer_125;
int tempButton_125;
int tempVehicleSpeedVD_125;
long tempVehicleSpeed_125;
int tempRollingCounter_125=0x00;//定义RollingCounter并且初始化
int tempCheckSum_125=0x00;//定义checkSum的值并且初始化
byte tempCheckSum_buff[10];
}
//创建定时器0x125
on timer Timer_125
{
CRC_ID();
output(meg_125);
setTimer(Timer_125,Cycle_Timer_125);
}
//向面板中对应的控件写入默认值
on start
{
//0x125
sysSetVariableInt(sysvar::TestOne_0x125::Button,1);//Button绑定的控件默认值为1,打开状态
sysSetVariableInt(sysvar::TestOne_0x125::Length,8);//Length绑定的值默认为8
sysSetVariableInt(sysvar::TestOne_0x125::Cycle_Time,50);//Cycle_Time绑定的控件 默认值为50
sysSetVariableInt(sysvar::TestOne_0x125::VehicleSpeedVD,0);//0x0:Valid,0x1:Invalid
sysSetVariableInt(sysvar::TestOne_0x125::VehicleSpeed,30);//车速默认值为0
//sysSetVariableInt(sysvar::TestOne_0x125::RollingCounter,0);//RollingCounter默认值为0
sysSetVariableInt(sysvar::TestOne_0x125::RollingCounter_Button1,1);//RollingCounter 按钮默认打开
sysSetVariableInt(sysvar::TestOne_0x125::CheckSum_Button,1);//CheckSum默认值为0
}
//点击按钮触发报文周期性发送_0x125
on sysvar_update sysvar::TestOne_0x125::Button
{
tempButton_125 = @this;
if(tempButton_125 == 1)
{
setTimer(Timer_125,Cycle_Timer_125);
}
else
{
cancelTimer(Timer_125);
}
}
//在面板中手动改动长度
on sysvar_update sysvar::TestOne_0x125::Length
{
meg_125.DLC=@this;
}
//在面板中手动改动周期值
on sysvar_update sysvar::TestOne_0x125::Cycle_Time
{
Cycle_Timer_125=@this;
}
//在面板中手动改动速度有效值
on sysvar_update sysvar::TestOne_0x125::VehicleSpeedVD
{
tempVehicleSpeedVD_125=@this;
meg_125.Test_VehicleSpeedVD.phys=tempVehicleSpeedVD_125;
}
//在面板中手动改动速度值
on sysvar_update sysvar::TestOne_0x125::VehicleSpeed
{
tempVehicleSpeed_125=@this;
meg_125.Test_VehicleSpeed.phys=tempVehicleSpeed_125;
}
On signal_update Test_125::Test_RollingCounter
{
tempRollingCounter_125++;
tempRollingCounter_125=tempRollingCounter_125%16;
if(@sysvar::TestOne_0x125::RollingCounter_Button1==1)
{
meg_125.Test_RollingCounter=tempRollingCounter_125;
@TestOne_0x125::RollingCounter=tempRollingCounter_125;
}
else
{
meg_125.Test_RollingCounter=1;
}
//手动输入RollingCounter的值
if(@sysvar::TestOne_0x125::RollingCounter_Button1==0)
{
meg_125.Test_RollingCounter=@TestOne_0x125::RollingCounter;
}
}
/*checksum 算法实现
/*checksum具体在报文中实现与输出*/
void CRC_ID()
{
int j;
long tempCheckSum;
tempCheckSum=0;
for(j=0;j<7;j++)
{
tempCheckSum_buff[j] = meg_125.byte(j);
}
meg_125.Test_CRC_125.phys = LP_E2E_P02_Protect(tempCheckSum_buff,7);
if(@sysvar::TestOne_0x125::CheckSum_Button==1)
{
@TestOne_0x125::CheckSum=meg_125.Test_CRC_125;
}
else
{
meg_125.Test_CRC_125=1;
}
//手动输入CheckSum的值
if(@sysvar::TestOne_0x125::CheckSum_Button==0)
{
meg_125.Test_CRC_125=@TestOne_0x125::CheckSum;
write("固定值:%x",meg_125.Test_CRC_125);
}
}
// 校验数据、校验数据长度、
byte LP_E2E_P02_Protect(byte data[], byte Messagelength)
{
byte i, j, crc8;
byte Init = 0xFF; //初始值 Init
byte Polynomial = 0x3E; //多项式 Polynomial
byte XOR=0xFF; //结果异或值 XOR
byte CRC;
crc8 = Init;
for(i = 0; i < Messagelength; i++)
{
crc8 ^= data[i]; //校验数据包含 DATA ID + 报文 byte0~byte6
for(j = 0; j < 8; j++)
{
if(crc8 & 0x80)
{
crc8 = (crc8 << 1) ^ Polynomial;
}
else
{
crc8 = (crc8 << 1);
}
}
}
return CRC= crc8 ^ XOR;
}