Sending Periodic CAN signals on button press using CAPL and CANalyzer

我只是一个虾纸丫 提交于 2019-12-13 20:21:30

问题


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


回答1:


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.




回答2:


Your requirement is - First, set a periodic timer at 50ms. on Button Press. Second, output message on the timer event(50ms periodic). So your code should be like this-

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

//This only gets called once at the measurement start because you want to send the same value in each period.
on 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);
}


来源:https://stackoverflow.com/questions/49729516/sending-periodic-can-signals-on-button-press-using-capl-and-canalyzer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!