You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CANoe CAPL控制器自动生成代码测试:丢包场景实现需求

解决方案:CAPL实现CAN消息丢失场景的响应验证

我来帮你搞定这个丢包场景的CAPL代码完善问题。CAPL本身没有直接提供你想要的WaitForMessage(int aTimeOut, Message yourMessage)函数,但我们可以基于内置的测试函数封装一个符合需求的版本,同时把丢包场景的验证逻辑补全。

1. 封装自定义WaitForMessage函数

先定义一个完全匹配你需求的函数:返回0表示成功接收目标消息,返回-1表示超时:

int WaitForMessage(int timeoutMs, Message msgTemplate)
{
    // CAPL内置的TestWaitForMessage返回1=成功接收,0=超时
    if (TestWaitForMessage(msgTemplate, timeoutMs) == 1)
    {
        return 0; // 符合你的规则:成功接收返回0
    }
    else
    {
        return -1; // 超时未接收返回-1
    }
}

如果需要更精准的匹配(比如检查消息内的特定信号值),可以在函数里额外添加信号校验逻辑,后面我会补充示例。

2. 完善你的现有代码片段

接下来把这个函数整合到你的丢包场景代码中,这里假设你已经定义了receivingCan_SNA作为CAN2在丢包时应返回的响应消息模板:

// 先定义自定义的WaitForMessage函数
int WaitForMessage(int timeoutMs, Message msgTemplate)
{
    if (TestWaitForMessage(msgTemplate, timeoutMs) == 1)
    {
        return 0;
    }
    else
    {
        return -1;
    }
}

on timer sendMessage { 
    if(anzahlAnBotschaften > 0) // 已发送消息数量
    { 
        if(anzahlAnBotschaften % 3 == 0) // 每3次触发消息丢失
        { 
            botschaftWirdGesendet = 0; 
            // 等待CAN2的SNA响应,超时时间设为500ms(可根据你的通信周期调整)
            lRet = WaitForMessage(500, receivingCan_SNA); 
            if(lRet == 0) { // 成功接收到SNA,测试通过
                TestStepPass("010.1", "SNA was triggered"); 
            } else { // 超时未收到SNA,测试失败
                TestStepFail("010.1", "Timeout was triggered, but no SNA was found"); 
            } 
        } else { 
            botschaftWirdGesendet = 1; 
            output(sendingCan_BrkSys); 
            lRet = TestGetWaitEventMsgData(receivingCan_aMessage); 
            if(lRet == 0) { 
                // 正向场景处理逻辑
            } 
        } 
        anzahlAnBotschaften -- ; 
        setTimer(botschaftsAusfall,20); 
    } 
}

3. 进阶:添加信号值校验

如果需要验证SNA消息的内容是否符合预期(比如特定信号值),可以修改自定义函数:

int WaitForMessage(int timeoutMs, Message msgTemplate)
{
    if (TestWaitForMessage(msgTemplate, timeoutMs) == 1)
    {
        // 假设SNA消息内有个名为sna_triggered的信号,值为1表示正确触发丢包响应
        if (msgTemplate.sna_triggered == 1)
        {
            return 0;
        }
        else
        {
            // 接收到消息但信号值不符合预期,同样返回-1
            return -1;
        }
    }
    else
    {
        return -1;
    }
}

内容的提问来源于stack exchange,提问作者gaskraank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:20:51