【零基础学习CAPL】——CAN报文的发送(LiveCounter——生命信号)
includes
{
}
variables
{
message ESC_125 meg1;
msTimer Timer1;
long Cycle_Timer1;
int Button1;
int VehicleSpeedVD1;
long VehicleSpeed1;
int tempRollingCounter=0x00;//定义RollingCounter并且初始化
}
//创建一个报文
on message ESC_125
{
// (0)=0x11;
// (1)=0x12;
// =8;
//CANFD报文发送标志
meg1.FDF = 1;
meg1.BRS = 1;
}
//创建定时器
on timer Timer1
{
output(meg1);
setTimer(Timer1,Cycle_Timer1);
}
//向面板中对应的控件写入默认值
on start
{
sysSetVariableInt(sysvar::TestOne::Button,1);//Button绑定的控件默认值为1,打开状态
sysSetVariableInt(sysvar::TestOne::Length,8);//Length绑定的值默认为8
sysSetVariableInt(sysvar::TestOne::Cycle_Time,50);//Cycle_Time绑定的控件 默认值为50
sysSetVariableInt(sysvar::TestOne::VehicleSpeedVD,0);//0x0:Valid,0x1:Invalid
sysSetVariableInt(sysvar::TestOne::VehicleSpeed,0);//车速默认值为0
sysSetVariableInt(sysvar::TestOne::RollingCounter,0);//循环码默认为0
}
//点击按钮触发报文周期性发送
on sysvar_update sysvar::TestOne::Button
{
Button1 = @this;
if(Button1 == 1)
{
setTimer(Timer1,Cycle_Timer1);
}
else
{
cancelTimer(Timer1);
}
}
//在面板中手动改动长度
on sysvar_update sysvar::TestOne::Length
{
meg1.DLC=@this;
}
//在面板中手动改动周期值
on sysvar_update sysvar::TestOne::Cycle_Time
{
Cycle_Timer1=@this;
}
//在面板中手动改动速度有效值
on sysvar_update sysvar::TestOne::VehicleSpeedVD
{
VehicleSpeedVD1=@this;
meg1.ESC_VehicleSpeedVD.phys=VehicleSpeedVD1;
}
//在面板中手动改动速度值
on sysvar_update sysvar::TestOne::VehicleSpeed
{
VehicleSpeed1=@this;
meg1.ESC_VehicleSpeed.phys=VehicleSpeed1;
}
///liveCpunter 算法实现
//RollingCounter(new)=(RollingCounter(old)+1)&0x0F
//0-15之间的循环
//可使用tempRollingCounter=tempRollingCounter%16的经典算法
On signal_update ESC_125::ESC_RollingCounter
{
tempRollingCounter++;
tempRollingCounter=tempRollingCounter%16;
if(@sysvar::TestOne::RollingCounter_Button1==1)
{
meg1.ESC_RollingCounter=tempRollingCounter;
@TestOne::RollingCounter=tempRollingCounter;
write("循环值:%d",tempRollingCounter);
}
else
{
meg1.ESC_RollingCounter=1;
}
//手动输入RollingCounter的值
if(@sysvar::TestOne::RollingCounter_Button1==0)
{
meg1.ESC_RollingCounter=@TestOne::RollingCounter;
write("固定值:%d",meg1.ESC_RollingCounter);
}
}