Sending Periodic CAN signals on button press using CAPL and CANalyzer

bislinux picture bislinux · Apr 9, 2018 · Viewed 9.2k times · Source

I am trying to send a set of CAN frames on to CAN bus. I am using CAPL to program and CANalyzer8.5 to simulate and Panel designer to create a button. My requirement is to first create a button using PANEL designer. Only on button press it should start sending periodic CAN frames on to the bus. I am a bit confused as to how to achieve it. So far I have managed to write two separate programs using CAPL. First program sends data at start periodically. Second code sends data only once when the button is pressed. I want to merge both the codes to start sending periodically on button press.

first code

/*@!Encoding:1252*/
includes
{
}

variables
{
  msTimer mytimer;
  message 0x100 A={dlc=8};
  message 0x200 B={dlc=8};
  message 0x300 C={dlc=8};
  message 0x400 D={dlc=8};
}

on start
{
  setTimer(mytimer,50);
}

on timer mytimer
{
  A.byte(0)=0x64;
  B.byte(4)=0x32;
  C.byte(6)=0x20;
  D.byte(7)=0x80;
  output(A);
  output(B);
  output(C);
  output(D);
  setTimer(mytimer,50);
}

Second Code

/*@!Encoding:1252*/
includes
{
}

variables
{

  message 0x100 A={dlc=8};
  message 0x200 B={dlc=8};
  message 0x300 C={dlc=8};
  message 0x400 D={dlc=8};
}


on sysvar test::myButton
{
  A.byte(0)=0x64;
  B.byte(4)=0x32;
  C.byte(6)=0x20;
  D.byte(7)=0x80;
  output(A);
  output(B);
  output(C);
  output(D);
}

So as mentioned, when i press the button, it should start sending the CAN frames periodically. But the problem is,i cannot call a function within function as below:

on start
{
    on sysvar test::myButton
    {
        ....
    }
}

please advice me. thank you

Answer

Marco picture Marco · Apr 10, 2018

The on start event is only called once at measurement start, on sysvar is also an event, just that in your case it gets called when you press a certain button.

Maybe try this:

variables
{
  msTimer mytimer;
  message 0x100 A={dlc=8};
  message 0x200 B={dlc=8};
  message 0x300 C={dlc=8};
  message 0x400 D={dlc=8};
}

on start // This only gets called once at measurement start
{
  A.byte(0)=0x64;
  B.byte(4)=0x32;
  C.byte(6)=0x20;
  D.byte(7)=0x80;
}

on sysvar test::myButton  // Starts the timer when button is pressed
{
  setTimer(mytimer,50);
}

on timer mytimer
{
  output(A);
  output(B);
  output(C);
  output(D);
  setTimer(mytimer,50);
}

However at some point you probably want to stop the timer again using the function cancelTimer maybe with the use of a different button or a press of a key. For more examples, take a look at the CAPL Section in the CANalyzer Help.