【问题标题】:CAPL code, putting delay in the codeCAPL 代码,在代码中加入延迟
【发布时间】:2018-09-08 00:16:47
【问题描述】:

我有一个控制 CAN 信号发送开始的 CAPL 测试代码。我的目标是延迟发送过程的开始。 我的想法是通过 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 没有响应,我可以结束模拟的唯一方法是通过任务管理器。

  • 从我这边进一步检查得出的结论是计时器需要更多时间来处理并且根本没有执行。
  • 如果没有 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()

【问题讨论】:

  • 你在使用main,这意味着你可能正在编写一个CAPL测试节点,对吧?在这种情况下,TestWaitForTimeout 是要走的路。正如您使用的那样,CAPL 计时器更多地用于模拟节点等,其中事物是基于事件的。能否请您说明一下您是如何发送 CAN 消息的,以及它到底是哪里出错了?

标签: delay capl canoe


【解决方案1】:

我发现您的代码存在很多问题。它实际上根本不像代码,而更像是伪代码。它可以在您的 CAPL 浏览器上编译吗?

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

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

同样适用于:

CANstart()

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

但是,如果您需要在您的设置配置中运行块,例如 Replay 块、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)
}

这是否实现了您的目标?请随时询问更多详细信息。

【讨论】:

    【解决方案2】:

    您的while (isTimerActive()==1) { 语句似乎有问题。

    CAPL 函数int isTimerActive 需要参数timermstimer 变量和返回值 1,如果计时器处于活动状态,否则 0

    您可以通过以下方式检查计时器是否处于活动状态以及经过的时间。

    timer t;
    write("Active? %d", isTimerActive(t)); // writes 0
    setTimer(t, 5);
    write("Active? %d", isTimerActive(t)); // writes 1
    write("Time to elapse: %d",timeToElapse(t)); // Writes 5
    

    尝试在while (isTimerActive(Delay)==1) {处添加参数timer

    我不建议使用while语句,你可以直接使用定时器来调用函数StartCANTransmitting(),你的Main()应该是MainTest()

    void MainTest()
    {
       TestModuleTitle("Sample Tests");
       TestModuleDescription("This test module calls some test cases to demonstrate ");
       CANstart();
       if (TestGetVerdictLastTestCase() == 1)
          Write("CANstart failed.");
       else
          Write("CANstart passed.");
    }
    
    testcase CANstart() {   
      // add info block to test case in report  
      TestReportAddMiscInfoBlock("Used Test Parameters");
      TestReportAddMiscInfo("Max. voltage", "19.5 V");
      TestReportAddMiscInfo("Max. current", "560 mA");
      TestReportAddMiscInfo("StartCANTransmitting");
    
      SetTimer(Delay, 5000); //Timer initialization, set to be 5000ms
    }
    
    on timer Delay {
      StartCANTransmitting();   
    }
    

    【讨论】:

    • 为什么用这种计时器而不是TestWaitForTimeout?这也会记录在测试报告中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多