如何在CAPL中读取定时器的值?

时间:2017-12-27 08:45:25

标签: timer capl canoe

variables
{
    mstimer T1;
    long x,y;
}

on start
{
    setTimer(T1,1); /*Timer set to 1ms*/
    x=timeNow()/100000.0; /*Getting a time stamp when i start a timer*/
}

on timer T1
{
  if(response==0)         /*Check if response is sent or a function*/ has completed successfully*/ /*CONDITION*/
  {
    cancelTimer(T1);      /*Cancel timer if response is correct*/
    y=timeNow()/100000.0; /*Getting a timestamp when i stop a timer.*/
    write("Total time taken is %d",y-x);    /*Getting the time required to complete the condition (response==0)*/
  }
  else /*Setting timer again to check the condition*/
  {
    setTimer(T1,1);
    write("Timer started again");
  }
}

当定时器设置为1ms时,值(y-x)总是显示1ms,但如果条件在0.7ms时变为真,该怎么办呢。我想要条件成真的确切时间。

我使用timeNow函数来获取时间戳。

5 个答案:

答案 0 :(得分:2)

使用CAPL-Function timeToElapse 可以读取计时器的剩余时间,当它将到期时,将调用 on timer V2G 中实现的事件过程。

在“帮助”中,您可以在CANoe中找到有关计时器方法的更多说明

CAPL Functions » Classes » Timer, MsTimer

关于原始问题中的更新。怎么样:

variables{
  mstimer myTimer;
  long timePoint = 0;
  int somethinghappen = 0;
}

on start{
  long firstTimeDuration, period;
  firstTimeDuration = 1000; period = 100;
  setTimerCyclic(myTimer, firstTimeDuration, period); /*Timer is set cyclical*/
  timePoint = timeNow()/100000.0; /* 
    Remember the time on measurement start, 
    which is on start always 0, so what's the reason to do this here?
  */
  write("Start time %5.3f", timePoint);
}

on timer myTimer{
  if(somethinghappen != 0){
    //you need to cancel the timer only when it was set as cyclic, see setTimerCyclic() 
    cancelTimer(myTimer); /*Cancel timer if response arrived*/
    write("Total time taken is %5.3f", timeNow()/100000.0 - timePoint); 
  }else{
    //nothing todo at the moment
  }
}

on key 'b' {//boom
  somethinghappen = 1;
}

在测量运行时,不要忘记按下定义的键。

答案 1 :(得分:1)

正如许多人已经指出的那样,我相信你正在使用on timer不正确。 来自Vector知识库,

  

您可以在CAPL中定义时间事件。当此事件发生时,即经过一段时间后,将调用相关的on timer过程。

从我在这里看到的,通过在on timer内执行布尔检查,您将始终读取计时器结束时经过的当前时间。如果由于某种情况发生而想要提前退出计时器,我会建议一个完整的解决方法。您是否尝试过设置系统变量?

答案 2 :(得分:1)

如果需要测量小于1毫秒单位的时间,请使用TimeNowNS()函数(纳秒)。基本上将您的代码替换为带有TimeNowNS()的TimeNow(),然后将使用的变量调整为两倍,这样记录下来的时间戳就可以正确容纳了。

答案 3 :(得分:0)

我无法看到任何访问价值。在这里,你正在进行布尔检查。请澄清你的问题。

答案 4 :(得分:0)

/* Timer values depend on the value that u gave in settimer() API, This is one of the way you can know the value of timer*/
Variable
{
mstimer t1;
timer t2;
}
on Start
{
Settimer(t1,10);/*waits for 10 ms and then goes to timer t1*/
}

on timer t1
{
settimer(t2,10);/*waits for 10 s and then goes to timer t2, so the value of timer t1 = 10s*/
}

on timer t2
{

}

/*This is what i understood from what you asked. For further clarification give some more explanation on what you are asking*/