目录
0 引言
过完了春节开工第一篇,俗话说好记性不如烂笔头,理论看100遍都不如在项目里跟着走一遍学的快,本期带来两个CAPL的小练习希望能让大家有所收获。
1 定时器练习:
定时器是CAPL中的重要知识点,定时器分为秒定时器Timer和毫秒定时器msTimer,本例将使用定时器完成一个报文数据不断累加的过程,可以当成CRC校验码的超超低配版。
variables
{
message 0x555 msg1={dlc =8};
msTimer timer1;
}
on key 'a'
{
setTimer(timer1,100);//按a启动定时器
}
on timer timer1
{
setTimer(timer1,100);//如果想要循环发送的话需要在on timer事件中加上
msg1.byte(7)=msg1.byte(7)+1;
output(msg1);
}
运行结果:
可以看到报文ID为555,长度为8字节的报文,第8个字节以100ms循环发送,每次都累加1。
2 错误帧练习
variables
{
message 0x555 msg1={dlc =1};
msTimer timer1;
}
on key 'a'
{
output(errorframe);
}
on errorframe
{
write("错误帧出现在通道 %d.",this.can);
}
运行结果:
3 综合练习
该练习将上面两个小练习的知识点综合起来方便巩固:按下键盘a发送错误帧,再次按下键盘a循环发送报文。
variables
{
message 0x555 msg1={dlc =8};
msTimer timer1;
int condition=0;
}
on key 'a'
{
condition =!condition;
if(condition == 1)
{
cancelTimer(timer1);//取消定时器
output(errorframe);
write("condition =%d",condition);
}
else
{
setTimer(timer1,100);
write("condition =%d",condition);
}
}
on errorframe
{
write("错误帧出现在通道 %d.",this.can);
}
on timer timer1
{
setTimer(timer1,100);
msg1.byte(7)=msg1.byte(7)+1;
output(msg1);
}
运行结果如下:
如果需要加入HIL自动化测试群的欢迎私信或者评论区留言,谢谢大家。