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

the value (y-x) always shows 1ms as the timer is set to 1ms, but what if the condition has become true at 0.7ms. I wanted the exact time at which the condition became true.

I have used timeNow function to get the timestamps.

Nikhil
  • 139
  • 1
  • 5
  • 14

5 Answers5

2

Using CAPL-Function timeToElapse it is possible to read the remaining time of your timer, when it will expire the event procedure implemented in on timer V2G will be called.

In Help you can find more explanation about timer methods in CANoe

CAPL Functions » Classes » Timer, MsTimer

Regarding your update in the original question. How about this:

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

Don't forget to press the defined key while measurement is running.

An Other
  • 331
  • 1
  • 7
1

As many have already pointed out, I believe you are using on timer uncorrectly. From Vector knowledge base,

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.

From what I see here, by performing your boolean check inside on timer, you will always read the current time elapsed at the end of the timer. If you want to exit the timer prematurely due to a certain condition happening, I would suggest a complete workaround. Have you tried setting a system variable?

Daemon Painter
  • 3,208
  • 3
  • 29
  • 44
1

If you need to measure time smaller than 1ms unit, use TimeNowNS() function (nanoseconds). Basically replace your code with TimeNow() with TimeNowNS(), and adjust the used variables to doubles, so your timestamps recorded will properly fit.

VioletVynil
  • 502
  • 5
  • 11
0

i couldn't see any value to access. Here u are doing boolean check that's all. Please clarify your question.

itsrajon
  • 283
  • 2
  • 15
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*/
Prakash
  • 27
  • 1
  • 9