首页 文章

使用CAPL和CANalyzer按下按钮发送定期CAN信号

提问于
浏览
0

我正在尝试将一组CAN帧发送到CAN总线 . 我正在使用CAPL编程和CANalyzer8.5来模拟和Panel设计师创建一个按钮 . 我的要求是首先使用PANEL设计器创建一个按钮 . 只有按下按钮,它才会开始向总线发送周期性的CAN帧 . 我对如何实现它感到有点困惑 . 到目前为止,我已经设法使用CAPL编写了两个单独的程序 . 第一个程序定期发送数据 . 按下按钮时,第二个代码仅发送一次数据 . 我想合并两个代码,以便按下按钮开始定期发送 .

第一个代码

/*@!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);
}

第二码

/*@!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);
}

如上所述,当我按下按钮时,它应该开始定期发送CAN帧 . 但问题是,我无法在函数内调用函数,如下所示:

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

请指教 . 谢谢

1 回答

  • 2

    on start事件仅在测量开始时调用一次,sysvar也是一个事件,只是在你的情况下,当你按下某个按钮时它会被调用 .

    也许试试这个:

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

    但是在某些时候你可能想要使用函数cancelTimer再次停止计时器,可能是使用不同的按钮或按键 . 有关更多示例,请查看CANalyzer帮助中的CAPL部分 .

相关问题