Simulation Lifecycle & System Variable Integration
In modern Vector CANoe configurations, tests interact with System Variables (`@sysvar`) to control measurement states, GUI panels, and virtual I/O channels. Functions like `stop()`, `start()`, `output()`, and `trace()` give scripts comprehensive control over the simulation environment.
Overview & Key Concepts
In CAPL (CANoe/CANalyzer Programming Language) scripting, specific functions are provided to interact with and control CANoe or CANalyzer environments. CAPL is a high-level programming language used to simulate, test, and diagnose automotive networks like CAN (Controller Area Network), LIN, Ethernet, and FlexRay in CANoe or CANalyzer software. CAPL scripting allows you to automate tasks, process signals, interact with external systems, and implement more complex test logic.
Overview of CANoe/CANalyzer-Specific Functions in CAPL
These functions in CAPL enable you to interact with CANoe or CANalyzer features directly. They are essential for tasks such as simulating nodes, sending/receiving messages, working with CAN signals, and synchronizing events within the simulation environment.
Some of the key categories of functions include
CAN Bus Communication
Signal Processing
Logging and Monitoring
Timing and Synchronization
Environment Control
CAN Bus Communication Functions
CAPL provides several functions to handle CAN bus communication, such as sending and receiving messages, as well as manipulating bus parameters.
a) output()
Sends a message on the CAN bus.
Example
message myMessage;
myMessage.byte(0) = 0x01; // Setting the first byte of the message
output(myMessage); // Sending the message
This sends a message called myMessage to the CAN network with data that is manipulated inside the script.
b) setTimer()
Sets a timer to trigger after a specified period. Often used in combination with event-driven simulations.
Example
variables
{
msTimer myTimer;
}
on start
{
setTimer(myTimer, 1000); // Set the timer for 1000 ms (1 second)
}
on timer myTimer
{
output(myMessage); // Send the message after 1 second
}
Here, after 1 second, the message is sent to the bus.
c) getMessage()
Receives messages from the CAN bus.
Example
message receivedMessage;
on message CAN1
{
receivedMessage = this; // Store the received message in receivedMessage variable
write("Received message: %s", receivedMessage);
}
This example listens for messages on the CAN1 channel, stores them in the receivedMessage variable, and writes the message content.
Signal Processing Functions
These functions help interact with signals within a CAN message, which are typically used to control or monitor device states in automotive networks.
a) signal
You can use signals to read or modify individual values inside a CAN message.
Example
message myMessage;
on start
{
myMessage.byte(0) = 0x55; // Set byte 0 of myMessage to 0x55
myMessage.signal("mySignal") = 0x01; // Set a specific signal within the message to 0x01
output(myMessage);
}
This modifies the signal inside the CAN message before sending it to the bus.
b) signal with conditional check
You can also perform conditional checks on signals to react to specific values.
Example
on message CAN1
{
if (this.signal("engineSpeed") > 2000)
{
output(engineControlMessage);
}
}
In this example, the script checks if the engine speed exceeds 2000 RPM (engineSpeed signal) and sends a control message accordingly.
Logging and Monitoring Functions
CAPL allows you to interact with log files or monitors for debugging or testing purposes.
a) write()
Writes output to the log window, which is useful for debugging.
Example
on start
{
write("Simulation Started!");
}
This writes "Simulation Started!" to the log when the simulation starts.
b) trace()
Traces a specific signal value.
Example
on message CAN1
{
trace(this.signal("engineSpeed"));
}
This function traces the value of the engineSpeed signal from the received message and displays it in the trace window.
c) log()
Logs a specific value to a log file for analysis.
Example
on message CAN1
{
log(this.signal("engineSpeed"));
}
This writes the value of the engineSpeed signal into the log file every time the message is received.
Timing and Synchronization Functions
For simulations that require time-based synchronization, such as real-time behavior testing, CAPL provides timing functions.
a) wait()
Waits for a specific amount of time before proceeding with the script's execution.
Example
on start
{
wait(1000); // Wait for 1 second
output(myMessage);
}
The script waits for 1 second before sending the message.
b) setTimer() and stopTimer()
Sets a timer and stops it when needed.
Example
variables
{
msTimer timer1;
}
on start
{
setTimer(timer1, 500); // Set a timer for 500 ms
}
on timer timer1
{
output(myMessage);
stopTimer(timer1); // Stop the timer after sending the message
}
This sets a timer for 500 ms and sends the message after the timer expires, then stops the timer.
Environment Control Functions
CAPL also allows controlling the CANoe/CANalyzer environment, for instance, controlling the network traffic, adjusting simulation parameters, and more.
a) start()
Starts a specific simulation node (e.g., a CAN node).
Example
on start
{
start("CAN_Node"); // Start the simulation for the CAN_Node
}
This command starts a CAN node defined in your CANoe environment.
b) stop()
Stops a simulation node or a process.
Example
on stop
{
stop("CAN_Node"); // Stop the CAN_Node simulation
}
This command stops the CAN node during the script execution.
c) suspend() and resume()
These functions allow you to suspend or resume a simulation.
Example
on timer myTimer
{
suspend(); // Suspend the simulation
write("Simulation suspended.");
}
on key 'r' // Resume the simulation with a key press
{
resume(); // Resume the simulation
write("Simulation resumed.");
}
This example suspends the simulation after a timer expires and allows it to be resumed with a key press.
Conclusion
CAPL scripting in CANoe/CANalyzer provides a rich set of functions to interact with and manipulate CAN networks, simulate complex behaviors, control environment parameters, and process data. The functions described above are just a few examples, and there are many more available for handling different types of communication protocols and system interactions in the automotive industry. These capabilities make CAPL a powerful tool for automating test scenarios, debugging, and simulating real-time network behavior in CANoe or CANalyzer.
- Accessing System Variables requires proper namespace syntax: `@sysvar::Namespace::VariableName` or using `sysGetVariableInt()` APIs.
- Calling `stop()` halts simulation immediately; ensure all required log files are flushed before calling it.
Select your answer for each question below to receive immediate correctness evaluation and in-depth technical rationale.
Write a script that monitors a system variable `@sysvar::TestSuite::TestErrorCount`. If the error count reaches 3, print a test aborted banner to the Write window and stop the CANoe measurement automatically using `stop()`.
Verified CAPL implementation:
on sysvar TestSuite::TestErrorCount {
if (@TestSuite::TestErrorCount >= 3)
{
writeLineEx(0, 3, "===============================================");
writeLineEx(0, 3, "TEST ABORTED: Critical error threshold reached!");
writeLineEx(0, 3, "Total Errors: %d. Terminating measurement run.", @TestSuite::TestErrorCount);
writeLineEx(0, 3, "===============================================");
stop(); // Halt CANoe simulation
}
}