CAPL Programming usage of Timer as a delay

Yash Vardhan picture Yash Vardhan · May 27, 2015 · Viewed 32.2k times · Source

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

    }

Answer

Swanand picture Swanand · Jun 8, 2015

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.