在上一篇我们提到了timer定时器的基本定义方式和基本用法,然而在实际操作中通常会要求每隔一段时间发送不同的报文信息,如果要求在一个message上,每隔100ms分别发送不同的报文MSG1,MSG2,MSG3该如何对定时器进行操作,具体看以下代码实例。
#includes
{
}
variables
{
mstimer time1;
message xxx MSG1,MSG2,MSG3;//"xxx"为你自己的工程中已有的信号名称
byte msg1[8]={1,1,1,1,1,1,1,1};
byte msg2[8]={2,2,2,2,2,2,2,2};
byte msg3[8]={3,3,3,3,3,3,3,3};
int setcounter=0;//定义一个变量作为循环媒介
int i;
}
on prestart//在prestart中进行预操作
{
for(i=0;i<8;i++)
{
MSG1.byte(i)=msg1[i];
MSG2.byte(i)=msg2[i];
MSG3.byte(i)=msg3[i];//分别给MSG1,MSG2,MSG3填写报文内容
}
setcounter=0;
}
on start
{
setTimer (time1,100);//进行定时器设定
}
on timer time1//核心代码
{
if(setcounter==0)
{
output(MSG1);
setcounter=1;
}
else if(setcounter==1)
{
output(MSG2);
setcounter=2;
}
else if(setcounter==2)
{
output(MSG3);
setcounter=0;
}
else
{
setcounter=0;
}
}