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

How to read value of timer in CAPL?

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

值 (y-x) 始终显示 1ms,因为计时器设置为 1ms,但如果条件在 0.7ms 时变为真怎么办。我想要条件变为真的确切时间。

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

我看不到要访问的任何值。在这里你正在做布尔检查就是这样。请澄清您的问题。

使用 CAPL 函数 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;
}

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

/* 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*/

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

You can define time events in CAPL. When this event occurs, i.e. when a certain period of time elapses, the associated on timer procedure is called.

据我所见,通过在 on timer 中执行布尔检查,您将始终在计时器结束时读取当前经过的时间。如果您想因某种情况发生而过早退出计时器,我建议您采用完整的解决方法。您是否尝试过设置系统变量?

如果您需要测量小于 1ms 的时间单位,请使用 TimeNowNS() 函数(纳秒)。基本上将您的代码替换为 TimeNow() 和 TimeNowNS(),并将使用的变量调整为双倍,因此您记录的时间戳将正确适合。