【问题标题】:CAPL Programming usage of Timer as a delay定时器的 CAPL 编程使用作为延迟
【发布时间】:2015-08-09 02:08:22
【问题描述】:

我一直在编写一个 CAPL 脚本,该脚本会在一定延迟后在每个频道(2 个否)上发送消息。我想使用 SetTimer()mstimer::isRunning 函数生成以下延迟。 我可以使用 setTimer 函数,但我不知道如何使用 mstimer::isRunning。 代码如下:

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

    }

【问题讨论】:

  • 在询问有关 CAPL 的内容时,指定您处于“模拟”还是“测试”模式很重要。根据您编写的脚本类型,可以使用/禁用某些功能。
  • 感谢您的建议!我使用的是模拟模式,但我需要一个兼容模拟和实时的解决方案。

标签: can-bus capl


【解决方案1】:

使用isTimerActive() 方法代替mstimer::isrunningisTimerActive() 如果计时器正在运行,则返回 1,如果已过期,则返回 0。 所以你的代码看起来像:

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

      }
    }

但我不建议这样做。您可以通过onTimer 输出第二条消息,而不是循环输入on start

on start{
            temp.CAN = 1;
            temp.ID = 0xAA;
            Output(temp);
            settimer(timer1,100);
        }

on timer timer1{
    temp.CAN = 2;
    Output(temp);
}

如果您想保持通用性,即不限制为 2 个通道,您可以获取一个变量并在计时器中增加它。

【讨论】:

    【解决方案2】:

    我向 Vector 询问了同样的问题,他们的回答如下: “msTimer.isRunning”给我们定时器的状态,定时器是否运行 下面提到了可用性:

          on timer myTimer
          {
             write("Running Status %d",myTimer.isRunning());
          }
    

    “timeToElapse()”函数也可以用于以下情况。 语法:

          timer t;
          setTimer(t,5);
          write("Time to elapse: %d",timeToElapse(t)); // Writes 5
    

    【讨论】:

      【解决方案3】:
      /*Solution on how timer works*/
      
      variables
      {
      mstimer t1,t2;/*millisec timer*/
      timer t3;/*sec timer*/
      }
      on timer t1
      {
      settimer(t2, 10);/*wait in timer t1 for 10ms and then goes to timer t2*/
      }
      on timer t2
      {
      settimer(t3, 10);/*wait in timer t2 for 10ms and then goes to timer t3*/
      }
      on timer t3
      {
      settimer(t4, 10);/*wait in timer t3 for 10sec and then goes to timer t4*/
      }
      on timer t4
      {
      settimer(t1, 10);/*wait in timer t4 for 10sec and then goes to timer t1*/
      }
      
      on start()
      {
      settimer(t1,10);/*waits here for 10ms and then goes to timer t1*/
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-13
        • 2022-06-19
        • 2017-12-28
        • 1970-01-01
        • 2011-07-06
        • 1970-01-01
        • 2018-09-08
        • 1970-01-01
        相关资源
        最近更新 更多