Capl Basics
Capl Basics
CAPL
CAN Access Programming Language
CAPL Programming
The CAN Access Programming Language CAPL is a C-like programming
language,
which allows you to program CANoe for individual applications.
Introduction to CAPL
CAPL, the CAN Access Programming Language, allows you to quickly
develop code that makes CANalyzer or CANoe simulations more powerful.
CAPL is a procedural language whereby the execution of program blocks is
con-trolled by events. These program blocks are known as event procedures.
The program code that you define in event procedures is executed when the event
occurs.
For example, you can send a message on the bus in response to a key press (on
key), track the occurrence of messages on the bus (on message), or execute certain
actions cyclically (on timer).
Introduction to CAPL
A CAPL program consists of two parts:
1. Declare and define global variables
2. Declare and define user-defined functions and event procedures
CAPL Variables
Data types available for variables include integers
(dword, long, word, int, byte, char), floating point numbers (float anddouble),
CAN messages (message) and timers (timer or msTimer). Except for the timers, all
other variables can be initial-ized in their declarations.
variables {
int msgCount; // Is set to 0 at measurement start
message 34 sendMsg = { // Declare message with Id 34
dlc = 1, // Set Data Length Code = 1
byte(0) = 1 // Set 1st data byte = 1
};
}
CAPL Variables
Variables can be initialized when they are declared, whereby you can use either
simple notation or brackets {}. With the exception of timers, the compiler
initializes all variables with default values (unless otherwise defined: 0).
CAPL permits the declaration of arrays (arrays, vectors, matrices), analogous to
their declaration in the C programming language.
variables {
int vector[5] = {1,2,3,4,5};
int matrix[2][2] = {{11,12},{21,22}};
char progname[10] = “CANoe“;
}
CAPL Variables
Messages to be output from the CAPL program are declared with the
key word message. The complete declaration includes the message identifier or -
when work-ing with symbolic databases - the message name. For example, you
might write the following to output messages on the bus that have identifier A
(hex) or 100 (dec) or the message EngineDatadefined in the database.
Declaration of Messages
It is possible to access control information for the CAN message objects using the
following component selectors:
ID Message identifier
CAN Chip number
DLC Data Length Code
DIR Direction of transmission, possible values: RX, TX,
TXREQUEST.
RTR Remote Transmission Request; possible values: 0 (No
RTR), 1 (RTR)
TYPE Combination of DIR and RTR for efficient
evaluation.
(TYPE = (RTR << 8) | DIR )
TIME Time point, units: 10 microseconds
Event Procedures
You can react to the following events in CAPL using event procedures:
Event Event procedure
Receipt of a CAN message on message{}
Press of a key on key{}
Initialization of measurement (before meas-urement start)
on preStart{}
Measurement start on start{}
End of measurement on stopMeasurement{}
CAN controller goes to ErrorActive on errorActive{}
CAN controller goes to ErrorPassive on errorPassive{}
CAN controller reaches the warning limit on warningLimit{}
CAN controller goes to Bus Off on busOff{}
Elapse of a timer on timer{}
Occurrence of an error frame on errorFrame{}
Environment variable change on envVar{}
React to Messages
The event procedure type on message is provided to react to the receipt of
CAN messages in the CAPL nodes.
on message 123 React to message 123 (dec),
Receiving chip is not considered
on message 0x123 React to message 123 (hex);
receive chip is not considered
on message EngineData React to message EngineData
on message CAN1.123 React to message 123,
if it is received by chip CAN1
on message * React to all messages
on message CAN2.* React to all messages
that are received by chip CAN2
example:
on envVar Switch {
int val;
val = getValue(this);
// Read value of Switch into val
}
React to Time Events
CAPL allows you to create timers for seconds (Timer) or milliseconds (msTimer).
After thesetimers have been set and expire, the corresponding “on timer” event
procedure is executed.
Thisfacility can be used to create a cyclic event if you reset the timer at the end of
the timer event procedure. Timers can also be used to respond to an event after a
delay.
The setTimer() function takes two parameters, the name of the timer and the length
of time to setthe timer. The length of time parameter has different units depending
on what kind of timer youare using. For a Timer, the units are seconds; for an
msTimer, the units are milliseconds.
Themaximum values are 1799 seconds and 65,535 milliseconds, respectively. The
cancelTimer()function can be called on a timer before it has expired to prevent the
timer event from triggering.Calling the cancelTimer() function has no effect if the
timer is not set or has already expired.
The preStart, start, and stopMeasurement events are used to perform actions
before, at the start of, and after CANalyzer or CANoe measurements. If they are
defined, each is called once permeasurement. When the “Go” button is pressed in
CANalyzer or CANoe, the preStart event procedure is executed (if one exists).
You use this procedure to read data from files, initializevariables, or write to the
Write window. Other actions, such as outputting a message onto thebus, are not
available in the preStart event. Generally, actions that are invalid in the preStart
event procedure can be moved to the start event procedure.
After the preStart event procedure has completed executing, the start event
procedure is executed (if one exists). The start event procedure can be used to
initialize environmental variables, set timers, and output messages onto the bus.
The measurement is also started at this time.
React to System Events
When you press the Stop button in CANalyzer or CANoe, the stopMeasurement
event procedure is executed (if one exists). You can use this procedure to print
statistics in the Write window, output messages onto the bus, or write to a log file.
After this event has finished executing, the measurement is stopped.
React to CAN Controller Events
on errorPassive {
...
write("CAN Controller ist in errorPassive")
write(" errorCountTX = %d", this.errorCountTX);
write(" errorCountRX = %d", this.errorCountRX);
};
Expressions in CAPL
The key word this is used to refer to the the data structure of an object within an
event procedure for receiving a CAN object or environment variable. For example,
the following accesses the first data byte of message 100 which is just being
received:
on message 100 {
byte byte_0;
byte_0 = this.byte(0);
...
}
When information only needs to be transferred on an event basis, the event
message is used.
This sample program uses the pressing of the ‘b’ key on the PC keyboard to
initiate a single CAN message transmission.
variables
{
message 0x555 msg1 = {dlc=1};
}
on key ‘b’
{
msg1.byte(0)=0xAA;
output(msg1);
}