Advertisement
Responsive Ad Slot (Vector Toolchains & Embedded Certifications)
🚗 Automotive System Focus: Instrument Cluster (IC)

Multi-Trigger Asynchronous Event Handling

An instrument cluster ECU must concurrently respond to diverse asynchronous triggers: vehicle speed broadcast frames arriving every 20ms, driver trip reset button presses, warning chime expiration timers, and CAN bus error frames. CAPL's event-driven architecture handles each trigger cleanly through independent event handlers.

Overview & Key Concepts

In CAPL (CAN Application Programming Language) scripting, events are mechanisms that allow you to react to specific triggers or conditions within the system. Events enable CAPL programs to respond to CAN bus traffic, user inputs, and time-based or condition-based occurrences. These events are crucial in handling asynchronous and real-time communication in embedded systems, such as automotive ECUs (Electronic Control Units), network monitoring, or simulation setups.

Types of Events in CAPL

There are several types of events in CAPL scripting, and each serves a different purpose. They include:

Message Events

Timer Events

User Input Events

State Machine Events

Error Events

Let's go through these events in detail with examples.

Message Events

These events are triggered when a message is received on the CAN bus. In CAPL, you can write functions that respond when certain CAN messages are received or transmitted. These functions are called message event handlers.

Example: Message Event Handler

CAPL CAPL - Example: Message Event Handler
on message EngineSpeed {
// This event triggers when an "EngineSpeed" message is received on the CAN bus
output("Engine speed message received.");
int speed = EngineSpeed.Speed;  // Access the "Speed" signal within the message
if (speed > 3000) {
output("Engine speed is high!");
}
}
Explanation:

The on message keyword defines a handler for a specific message, in this case, EngineSpeed.

Whenever an EngineSpeed message is received, the event triggers the block of code, where we access the signal Speed and check if it is above a certain threshold.

Timer Events

Timers are used to schedule periodic events in CAPL scripts. Timers allow you to execute certain actions after a specified delay or at periodic intervals.

Example: Timer Event

CAPL CAPL - Example: Timer Event
variables
{
msTimer myTimer;  // Declare a timer variable
}
on start
{
setTimer(myTimer, 1000);  // Set the timer to 1000ms (1 second)
}
on timer myTimer
{
output("1 second has passed!");
setTimer(myTimer, 1000);  // Reset the timer to trigger again after 1 second
}
Explanation:
msTimer myTimer; declares a timer variable.
The setTimer() function is used to set the timer to a specific delay (1000ms in this case).

When the timer expires, the on timer event handler triggers, and the specified code is executed. The timer is then reset to continue triggering periodically.

User Input Events

User input events are triggered by actions from the user, such as pressing a button or typing in a control panel. These inputs are often used in simulation environments or testing tools.

Example: User Input Event

CAPL CAPL - Example: User Input Event
on key 'a'
{
output("The 'A' key was pressed.");
}
on key 'b'
{
output("The 'B' key was pressed.");
}
Explanation:

The on key event is triggered when the specified key is pressed on the keyboard.

In this example, pressing the 'A' key will output a message saying "The 'A' key was pressed" and similarly for the 'B' key.

State Machine Events

CAPL also supports state machine events, which are useful for modeling systems that operate in various states. These states can be changed based on certain conditions or events.

Example: State Machine Event

CAPL CAPL - Example: State Machine Event
variables
{
state systemState = Idle;  // Declare a state variable
}
enum state { Idle, Running, Stopped }
on message StartEngine
{
if (systemState == Idle) {
systemState = Running;
output("Engine is now Running.");
}
}
on message StopEngine
{
if (systemState == Running) {
systemState = Stopped;
output("Engine has Stopped.");
}
}
Explanation:
The enum is used to define states (Idle, Running, Stopped).
The systemState variable holds the current state of the system.

The state changes are triggered by receiving certain messages, such as StartEngine or StopEngine.

The state transitions are controlled within the on message event handlers, and messages are output based on the new state.

Error Events

Error events in CAPL allow you to monitor the status of the CAN bus or specific devices and react accordingly when errors occur.

Example: Error Event Handler

CAPL CAPL - Example: Error Event Handler
on error
{
output("An error occurred on the CAN bus!");
}
Explanation:

The on error event handler triggers when an error is detected in the CAN bus communication.

The code inside the handler executes whenever the error event is triggered, such as reporting an issue or taking corrective action.

Conclusion

Events in CAPL scripting provide a powerful way to respond to changes or triggers in your embedded systems or simulations. By using message handlers, timers, user inputs, state machines, and error detection, you can create responsive and robust applications that interact with CAN bus systems or other hardware in real time. Each type of event serves a specific role, and understanding how to use them effectively will help you build better scripts and simulations.

Event Type Syntax Example
Message Event on message <MessageName> { // Code to execute }
Timer Event on timer <TimerName> { // Code to execute }
User Input Event on key '<key>' { // Code to execute }
State Machine Event on <event> { // Code to execute }
Error Event on error { // Code to handle error }
⚠️ Common Pitfalls & CANoe Troubleshooting Tips
  • Never assume the arrival order of asynchronous events. Use state flags and timestamps rather than implicit execution ordering.
  • Handler execution must be lightweight: long computational routines should be divided into timer chunks to maintain real-time simulation fidelity.
📝 Concept Mastery Assessment
12 Questions Score: 0 / 0

Select your answer for each question below to receive immediate correctness evaluation and in-depth technical rationale.

Question 1 of 12
Which event handler executes exactly once at the very start of measurement in CANoe?
Explanation
'on start' triggers once when measurement starts, after all simulation nodes have finished preStart initialization.
Question 2 of 12
What is the difference between 'on preStart' and 'on start'?
Explanation
'on preStart' allows nodes to initialize internal variables, system variables, and DLL handles before active bus traffic commences.
Question 3 of 12
Which event handler executes when measurement is stopped by the user or script?
Explanation
'on stop' (or 'on stopMeasurement') runs when the simulation measurement ends, ideal for saving test reports and closing file handles.
Question 4 of 12
How do you define an event handler that triggers whenever ANY message is received on CAN channel 1?
Explanation
'on message CAN1.*' triggers on reception of any CAN frame on CAN channel 1.
Question 5 of 12
Which event handler triggers when the user presses the 'k' key on the physical keyboard in CANoe?
Explanation
'on key 'k'' executes when the 'k' key is pressed, commonly used for interactive fault injection or triggering test routines.
Question 6 of 12
What keyword represents the current incoming message instance inside an 'on message' event handler?
Explanation
Inside an 'on message' handler, the keyword 'this' references the incoming CAN message object (e.g. this.id, this.byte(0)).
Question 7 of 12
Can you filter an 'on message' handler to trigger strictly for a specific CAN message ID (e.g. 0x100)?
Explanation
'on message 0x100' or 'on message EngineStatus' (symbolic name from DBC) filters execution strictly to that ID.
Question 8 of 12
What is the execution model of CAPL event handlers?
Explanation
CAPL is event-driven and deterministic. Handlers run to completion sequentially in response to discrete simulation events.
Question 9 of 12
What happens if a long while loop (e.g. while(1)) without exit is executed inside a CAPL event handler?
Explanation
Because CAPL event handlers run in the simulation thread, blocking loops freeze simulation time and halt CANoe.
Question 10 of 12
Which event handler triggers whenever a specified timer object expires?
Explanation
'on timer timerName' executes when the named timer or msTimer reaches zero.
Question 11 of 12
Which event handler triggers when a specific system variable's value changes?
Explanation
'on sysvar Namespace::Variable' triggers whenever the specified system variable is updated or written to.
Question 12 of 12
Can an event handler trigger when an error frame occurs on the CAN bus?
Explanation
'on errorFrame' executes whenever an active or passive error frame is detected by the CAN controller on the bus.
0 / 0 (0%)
Great job! Review the questions above and proceed when ready.
🛠️ Hands-on Automotive Challenge: Interactive Trip Meter Controller

Create an event-driven CAPL node with two event handlers: 1) 'on key 'r'' which resets a global float `gTripDistanceKm` to 0.0, and 2) 'on message 0x120' (VehicleSpeedMsg) that adds distance to `gTripDistanceKm` based on speed and delta time.

Verified CAPL implementation:

CAPL Solution - Interactive Trip Meter Controller
variables {
  float gTripDistanceKm = 0.0;
  dword gLastTimestamp = 0;
}

on start {
  gLastTimestamp = timeNow();
  write("Trip Meter initialized.");
}

on key 'r' {
  gTripDistanceKm = 0.0;
  write("Trip Meter manually reset to 0.0 km.");
}

on message 0x120 {
  dword currentTime;
  float deltaHours;
  float currentSpeedKmh;

  currentTime = timeNow();
  // CANoe timeNow() returns time in 10 microsecond units (or 100us depending on config)
  deltaHours = (currentTime - gLastTimestamp) / 360000000.0;
  
  // Extract speed from payload byte 0 and 1
  currentSpeedKmh = (this.byte(0) * 256 + this.byte(1)) * 0.01;
  gTripDistanceKm += (currentSpeedKmh * deltaHours);
  gLastTimestamp = currentTime;
}
Advertisement
Responsive Ad Slot (Automotive Embedded Systems Career Resources)