问题
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);
}
回答1:
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);
}
}
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);
}
If you want to keep it generic i.e. not restricting to 2 channels, you can take a variable and increment it in timer.
回答2:
I asked Vector for the same question and they answered it something like this: “msTimer.isRunning” gives us status of Timer, whether timer running or not The usability is mentioned below:
on timer myTimer
{
write("Running Status %d",myTimer.isRunning());
}
" timeToElapse() " function can also be used for the following case. Syntax:
timer t;
setTimer(t,5);
write("Time to elapse: %d",timeToElapse(t)); // Writes 5
回答3:
/*Solution on how timer works*/
variables
{
mstimer t1,t2;/*millisec timer*/
timer t3;/*sec timer*/
}
on timer t1
{
settimer(t2, 10);/*wait in timer t1 for 10ms and then goes to timer t2*/
}
on timer t2
{
settimer(t3, 10);/*wait in timer t2 for 10ms and then goes to timer t3*/
}
on timer t3
{
settimer(t4, 10);/*wait in timer t3 for 10sec and then goes to timer t4*/
}
on timer t4
{
settimer(t1, 10);/*wait in timer t4 for 10sec and then goes to timer t1*/
}
on start()
{
settimer(t1,10);/*waits here for 10ms and then goes to timer t1*/
}
来源:https://stackoverflow.com/questions/30474842/capl-programming-usage-of-timer-as-a-delay