Learn CAPL Script for Automotive Testing
CAPL (Communication Access Programming Language) is a C-based language that lets you control simulation, testing, and diagnostics on CAN, LIN, and FlexRay buses.
What is CAPL?
Imagine you're developing a door controller ECU. The controller needs to receive lock/unlock commands from the central system, control window movement, and send status feedback. But during development, the other ECUs aren't ready yet.
CAPL bridges this gap. It lets you create "virtual" ECUs that send test commands, simulate bus traffic, and verify your ECU's behavior — all before hardware arrives.
CAPL is event-driven: instead of a main() function that runs continuously, you define handlers that react to messages, timers, keyboard input, and more. It seamlessly integrates with CANoe/CANalyzer for comprehensive automotive testing.
What You Can Build
Simulate Missing ECUs
Create virtual ECUs that behave like the real thing. Perfect for software-in-the-loop testing before hardware is available.
// Simulate a temperature sensor sending data every 100ms
variables {
msTimer tempTimer;
float currentTemp;
}
on start {
currentTemp = 20.0;
setTimer(tempTimer, 100);
}
on timer tempTimer {
currentTemp = currentTemp + (random() % 10 - 5) * 0.1;
message 0x3A tempMsg;
tempMsg.byte(0) = (byte)(currentTemp * 10);
output(tempMsg);
setTimer(tempTimer, 100);
}
Automated Test Cases
Write test cases that automatically verify ECU responses. Check timing, data values, and bus behavior without manual inspection.
// Test case: Verify door lock response time
testcase TC_LockResponseTime()
{
message 0x200 cmd;
cmd.byte(0) = 0x01;
output(cmd);
// Wait for response with 1 second timeout
testWaitForMessage(0x201, 1000);
if (responseReceived) {
testStepPass("Response within time limit");
} else {
testStepFail("No response received");
}
}
Diagnostic Services
Communicate with ECUs using standardized diagnostic protocols. Read fault codes, configure parameters, and perform routines.
// Read DTCs (Diagnostic Trouble Codes)
variables {
DiagRequest NRC_DTCs.GetDTCInformation req;
}
on start {
req = DiagRequest NRC_DTCs.GetDTCInformation;
req.SetParameter("DTCStatusMask", 0xFF);
req.Send(1000);
if (req.WaitForResponse(2000)) {
write("DTC Read Successful");
}
}
Bus Analysis & Logging
Monitor bus traffic, filter specific messages, and log data for debugging and performance analysis.
// Monitor and log specific CAN messages
on message CAN1.200 {
write("Time: %1.3f | ID: 0x%X | DLC: %d",
this.Time, this.ID, this.DLC);
byte cmd = this.byte(0);
byte param = this.byte(1);
if (cmd == 0x05 && param > 0x10) {
write("High priority command!");
}
}
Key Concepts
Event-Driven
CAPL programs wait for events (messages, timers, keys) rather than running sequentially. Each handler responds to one specific trigger.
Bus-Oriented
Built-in data types like message and signal make it natural to read and write CAN/LIN/FlexRay data directly.
C-Based Syntax
If you know C, you'll feel at home. Variables, functions, control flow — all familiar, but simpler for bus-specific tasks.
Integrated with CANoe
CAPL runs inside CANoe/CANalyzer, giving you full access to simulation, test, and diagnostic features.
Your Learning Path
Follow this step-by-step tutorial to master CAPL programming from basics to advanced topics.
Understand CAPL's role in automotive electronics, its four core application scenarios (simulation, testing, diagnostics, analysis), and event-driven programming concepts.
Install CANoe, understand the CANoe project structure (.cfg files, database, simulation nodes), and learn to use the CAPL Browser IDE for writing and compiling programs.
Master CAPL-specific data types (message, timer, msTimer), operators, control flow, and functions. Learn the differences from C language that make CAPL unique.
Learn the CAPL program lifecycle (on preStart, on start, on preStop, on stopMeasurement), master on message events, the this keyword, and output() function for sending CAN messages.
Master three essential debugging weapons: write() function for log-based debugging, breakpoints for inspecting execution state, and Watch Window for monitoring variable changes in real-time.
Learn state machine concepts and build a complete DoorModule simulation node with command response, internal state management, automatic behaviors (timers), and status feedback.
Transition from simulation to automated testing. Learn testcase structure, Test Service Library (TSL) for checks and stimulus functions, and generate test reports automatically.
Integrate simulation nodes and test modules in one CANoe project. Learn manual testing with Interactive Generator and automated testing with Test Reports for complete verification.