CAPL Script
CANoe / CANalyzer 编程

学习CAPL 脚本用于汽车测试

CAPL(通信访问编程语言)是一种基于 C 的语言,让您能够控制 CAN、LIN 和 FlexRay 总线上的仿真、测试和诊断。

什么是 CAPL?

想象一下,您正在开发一个车门控制器 ECU。该控制器需要接收中央系统的锁定/解锁命令,控制车窗移动,并发送车门状态反馈。但在开发过程中,其他 ECU 还没有准备就绪。

CAPL 弥合了这一差距。它让您能够创建虚拟 ECU,发送测试命令,仿真总线流量,并在硬件到达之前验证您的 ECU 的行为。

CAPL 是事件驱动的:不是从连续运行的 main() 函数开始,而是定义处理程序来响应消息、定时器、键盘输入等事件。它与 CANoe/CANalyzer 无缝集成,提供全面的汽车测试功能。

使用报文时间间隔分析工具:在浏览器本地检查周期偏差、重复时间戳及时间倒退,附可手算的示例和排错说明。

您可以用 CAPL 构建什么

仿真不存在的 ECU

按明确的报文和状态规则建立模型,再将观察到的行为与预期要求比较。

temperature_sensor.can

教学片段,未在您的配置中编译验证。请核对工具版本、节点类型及数据库。

// Simulation node: demo payload, 0.1 degree per count.
variables {
    msTimer tempTimer;
    message 0x3A tempMsg;
    byte tempTenths;
}

on start {
    tempTenths = 200;
    tempMsg.CAN = 1;
    tempMsg.DLC = 1;
    setTimer(tempTimer, 100);
}

on timer tempTimer {
    tempMsg.byte(0) = tempTenths;
    output(tempMsg);
    if (tempTenths >= 240) tempTenths = 200;
    else tempTenths++;
    setTimer(tempTimer, 100);
}

自动化测试用例

编写自动验证 ECU 响应的测试用例。检查时序、数据值和总线行为,无需人工检查。

test_door_lock.testcase

教学片段,未在您的配置中编译验证。请核对工具版本、节点类型及数据库。

// Test module, single-bus example. Call from the test flow.
// Checks frame arrival, not the door's physical lock state.
testcase TC_ResponseArrival()
{
    message 0x200 cmd;
    long result;
    cmd.CAN = 1;
    cmd.DLC = 1;
    cmd.byte(0) = 0x01;
    output(cmd);

    result = testWaitForMessage(0x201, 1000);
    if (result == 1) {
        testStepPass("Response", "Response frame received");
    } else if (result == 0) {
        testStepFail("Response", "Timeout after 1000 ms");
    } else {
        testStepFail("Response", "Wait error: %d", result);
    }
}

诊断服务

使用标准化诊断协议与 ECU 通信。读取故障码、配置参数和执行例程。

diagnostics.can

教学片段,未在您的配置中编译验证。请核对工具版本、节点类型及数据库。

// Tester node: replace ECU/service/parameter qualifiers
// with symbols from your configured diagnostic description.
variables {
    diagRequest MyECU.ReadDTCInformation req;
}

on key 'd' {
    long result;
    result = diagSetParameter(req, "DTCStatusMask", 0xFF);
    if (result < 0) {
        write("Parameter error: %d", result);
        return;
    }
    result = diagSendRequest(req);
    write("Send request returned: %d", result);
}

on diagResponse MyECU.ReadDTCInformation {
    // Inspect the response and any negative-response code.
    // Receiving a response alone does not prove success.
    write("Diagnostic response received");
}

总线分析与日志

监控总线流量、过滤特定消息并记录数据用于调试和性能分析。

bus_monitor.can

教学片段,未在您的配置中编译验证。请核对工具版本、节点类型及数据库。

// Simulation node: CAN1, hexadecimal ID 0x200.
on message CAN1.0x200 {
    byte cmd;
    byte param;

    // TIME is in 10-microsecond ticks; convert to seconds.
    write("Time: %.5f s | ID: 0x%X | DLC: %d",
          this.TIME / 100000.0, this.ID, this.DLC);
    if (this.DLC < 2) return;

    cmd = this.byte(0);
    param = this.byte(1);
    if (cmd == 0x05 && param > 0x10) {
        write("Command matches the example filter");
    }
}

核心概念

事件驱动

CAPL 程序等待事件(消息、定时器、按键)而不是顺序运行。每个处理程序响应一个特定的触发器。

总线导向

像 message 和 signal 这样的内置数据类型使直接读取和写入 CAN/LIN/FlexRay 数据变得很自然。

基于 C 的语法

如果您了解 C 语言,您会有宾至如归的感觉。变量、函数、控制流——都很熟悉,但针对总线特定任务进行了简化。

与 CANoe 集成

可用的仿真、测试和诊断能力取决于产品、版本、运行上下文与许可。