3

我一直在编写一个 CAPL 脚本,该脚本会在一定延迟后在每个频道(2 个没有)上发送消息。我想使用SetTimer()mstimer::isRunning函数生成以下延迟。我可以使用 setTimer 函数,但我不知道如何使用mstimer::isRunning。代码如下所示:

    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);

    }
4

3 回答 3

1

而不是mstimer::isrunning使用isTimerActive()方法。 isTimerActive()如果计时器正在运行,则返回 1,如果已过期,则返回 0。所以你的代码看起来像:

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);

      }
    }

但我不建议这样做。而不是循环输入on start,您可以通过输出第二条消息onTimer

on start{
            temp.CAN = 1;
            temp.ID = 0xAA;
            Output(temp);
            settimer(timer1,100);
        }

on timer timer1{
    temp.CAN = 2;
    Output(temp);
}

如果你想让它保持通用,即不限于 2 个通道,你可以取一个变量并在计时器中增加它。

于 2015-06-08T04:00:15.337 回答
0

我问 Vector 相同的问题,他们的回答是这样的:“msTimer.isRunning”为我们提供了 Timer 的状态,无论计时器是否正在运行。下面提到了可用性:

      on timer myTimer
      {
         write("Running Status %d",myTimer.isRunning());
      }

“ timeToElapse() ”函数也可以用于以下情况。句法:

      timer t;
      setTimer(t,5);
      write("Time to elapse: %d",timeToElapse(t)); // Writes 5
于 2015-06-12T03:32:55.327 回答
-1
/*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*/
}
于 2017-12-26T14:32:34.383 回答