首页 文章

CAPL代码,在代码中加入延迟

提问于
浏览
2

我有一个CAPL测试代码,用于控制CAN信号发送的开始 . 我的目标是延迟发送过程的开始 . 我的想法是通过setTimer()函数与isTimerActive()结合使用 .

一般来说,我的代码如下所示:

main() {   
CANstart();
function_2();
function_3();   
}

CANstart() {    
  SetTimer(Delay, 5000); //Timer initialization, set to be 5000ms

  while (isTimerActive()==1) {
    // this while loop avoids that the code is proceding while the settimer exception is being called and executed
  }

  StartCANTransmitting(); // After this function, jump back to main and proceed with function_2   
}

on timer Delay {
  // Do nothing, just wait   
}

上面的程序代码导致卡在那一点,CANoe没有响应,我可以通过taskmanager结束模拟的唯一方法 .

  • 从我这边进一步检查得出结论,计时器需要更多的时间来处理,而根本没有执行 .

  • 如果没有isTimerActive()函数,程序代码不会等待计时器完成,也没有任何延迟 . 似乎代码运行而不等待异常 .

  • 似乎CAPL处理循环非常糟糕 .

我查看stackoverflow和以下论坛帖子谈论我所提供的非常类似的问题,而不提供任何有效的解决方案:

CAPL Programming usage of Timer as a delay

Are timers running, while loops are active?

Delay function in CAPL apart from testwaitfortimeout()

1 回答

  • 0

    我发现你的代码存在很多问题 . 它实际上根本不像代码,但更像是伪代码 . 它是否在您的CAPL浏览器上编译?

    main() {   
    CANstart();
    function_2();
    function_3();   
    }
    

    如果这是function declaration,那么它缺少类型和返回值 . 另外,你什么时候期待 main() 被执行?

    这同样适用于:

    CANstart()
    

    让我们退一步吧 . 你需要延迟可以传输的开始 . 如果你需要这样做,因为你有CANalyzer / CANoe运行之外的代码,那么我建议你通过命令行调用应用程序(请参阅指南获取更多帮助) .

    但是,如果您需要在设置配置中运行块,如重播块,Loggin块或其他,我建议您执行以下操作:

    variables {
        /* define your variables here. You need to define all messages you want to send and respective signal values if not defaulted */
        message 0x12345678 msg1;   // refer to CAPL guide on how to define message type variables
        msTimer delay;
        msTimer msgClock1;
    }
    
    on start {
        /* when you hit the start measurements button (default F9) */
        setTimer(delay, 5000);   // also note your syntax is wrong in the example
    }
    
    on timer delay {
        /* when timer expires, start sending messages */
        output(msg1);    // send your message
        setTimer(msgClock1,250);    // set timer for cyclic message sending
    }
    
    on timer msgClock1 {
        /* this mimicks the behaviour of a IG block */
        setTimer(msgClock1,250);    // keep sending message
        output(msg1)
    }
    

    这是否达到了你的目标?请随时询问更多详情 .

相关问题