/*@!Encoding:936*/
includes
{
}
variables
{
// 0x125 related definition
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;//Define RollingCounter and initialize
int tempCheckSum_125=0x00;//Define the value of checkSum and initialize it
byte tempCheckSum_buff[10];
}
//Create timer 0x125
on timer Timer_125
{
CRC_ID();
output(meg_125);
setTimer(Timer_125,Cycle_Timer_125);
}
//Write default values to corresponding controls in the panel
on start
{
//0x125
sysSetVariableInt(sysvar::TestOne_0x125::Button,1);//The default value of the Button-bound control is 1, open state
sysSetVariableInt(sysvar::TestOne_0x125::Length,8);//The value of Length binding is 8 by default
sysSetVariableInt(sysvar::TestOne_0x125::Cycle_Time,50);//Cycle_Time bound control default value is 50
sysSetVariableInt(sysvar::TestOne_0x125::VehicleSpeedVD,0);//0x0:Valid,0x1:Invalid
sysSetVariableInt(sysvar::TestOne_0x125::VehicleSpeed,30);//The default value of vehicle speed is 0
//sysSetVariableInt(sysvar::TestOne_0x125::RollingCounter,0);//The default value of RollingCounter is 0
sysSetVariableInt(sysvar::TestOne_0x125::RollingCounter_Button1,1);//The RollingCounter button is turned on by default
sysSetVariableInt(sysvar::TestOne_0x125::CheckSum_Button,1);//CheckSum default value is 0
}
//Click the button to trigger periodic sending of messages_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);
}
}
//Change the length manually in the panel
on sysvar_update sysvar::TestOne_0x125::Length
{
meg_125.DLC=@this;
}
//Change the period value manually in the panel
on sysvar_update sysvar::TestOne_0x125::Cycle_Time
{
Cycle_Timer_125=@this;
}
//Manually change the effective speed value in the panel
on sysvar_update sysvar::TestOne_0x125::VehicleSpeedVD
{
tempVehicleSpeedVD_125=@this;
meg_125.Test_VehicleSpeedVD.phys=tempVehicleSpeedVD_125;
}
//Change the speed value manually in the panel
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;
}
//Manually enter the value of RollingCounter
if(@sysvar::TestOne_0x125::RollingCounter_Button1==0)
{
meg_125.Test_RollingCounter=@TestOne_0x125::RollingCounter;
}
}
/*checksum algorithm implementation
/*checksum is implemented and output in packets*/
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;
}
//Manually enter the CheckSum value
if(@sysvar::TestOne_0x125::CheckSum_Button==0)
{
meg_125.Test_CRC_125=@TestOne_0x125::CheckSum;
write("Fixed value: %x",meg_125.Test_CRC_125);
}
}
// Verify data, verify data length,
byte LP_E2E_P02_Protect(byte data[], byte Messagelength)
{
byte i, j, crc8;
byte Init = 0xFF; //Initial value Init
byte Polynomial = 0x3E; //Polynomial
byte XOR=0xFF; //The result XOR value
byte CRC;
crc8 = Init;
for(i = 0; i < Messagelength; i++)
{
crc8 ^= data[i]; //The verification data contains DATA ID + message byte0~byte6
for(j = 0; j < 8; j++)
{
if(crc8 & 0x80)
{
crc8 = (crc8 << 1) ^ Polynomial;
}
else
{
crc8 = (crc8 << 1);
}
}
}
return CRC= crc8 ^ XOR;
}