问题描述
我一直在编写CAPL脚本,该脚本会在一定的延迟后在每个通道(2个不发送)上发送消息。我想使用 SetTimer()和 mstimer :: isRunning 函数生成以下延迟。
我可以使用setTimer函数,但是我不知道如何使用 mstimer :: isRunning 。
代码如下所示:
I have been writing a CAPL script that would send message on each channel (2 no's) after a certain delay. The following delay i want to generate using SetTimer() and mstimer::isRunning function.I can use setTimer function but I dont know how to use mstimer::isRunning.The code is shown below:
Variables{
message * temp = {DLC=8};
mstimer timer1;
}
on timer timer1{
//Do nothing
}
onstart{
for(noofChannel=1;noofChannel<=2;noofChannel++){
settimer(timer1,100);
temp.CAN = noofChannel;
temp.ID = 0xAA;
While (mstimer::isrunning)==0 // I need to write this right.
{ //wait for timer to expire}
Output(temp);
}
推荐答案
mstimer :: isrunning
使用 isTimerActive()
方法。
isTimerActive()
如果计时器正在运行,则返回1,如果计时器已过期,则返回0。
因此您的代码应如下所示:
Instead of mstimer::isrunning
use isTimerActive()
method.isTimerActive()
returns 1 if timers is running and 0 if it is expired.So your code will look like:
on start{
for(noofChannel=1;noofChannel<=2;noofChannel++){
settimer(timer1,100);
temp.CAN = noofChannel;
temp.ID = 0xAA;
While (isTimerActive(timer1) == 1)
{ //wait for timer to expire}
}
Output(temp);
}
}
但我不建议这样做。您可以通过 onTimer
But I would not recommend doing this. Instead of looping in on start
, you can ouput 2nd message through onTimer
on start{
temp.CAN = 1;
temp.ID = 0xAA;
Output(temp);
settimer(timer1,100);
}
on timer timer1{
temp.CAN = 2;
Output(temp);
}
如果要保持通用性,即不限于2个频道,可以取一个变量并在计时器中将其递增。
If you want to keep it generic i.e. not restricting to 2 channels, you can take a variable and increment it in timer.
这篇关于CAPL编程使用Timer作为延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!