Hello everyone, we meet again, I am your friend Quanzhangjun.
CAPL Programming of CANoe (CANoe Series Part 3)
1. Overview of CAPL
Same as Vspy's "C Code Interface"; in the use of CANoe, it also provides us with a tool for secondary programming development - "CAPL Browser". Through CAPL programming, we can complete more complex functional requirements on nodes. The operation is as follows: In the network node on the left under the "Simulation Setup" interface of the CANoe project, click the icon in the shape of a pencil to enter the CAPL editing interface (if the current node has not created a corresponding CAPL program, you will be prompted to enter CAPL program name, and save it as a file with a .can suffix)

1.1, CAPL language features
- The CAPL (Communication Access Programming Laguage) language is a C-like language. The syntax is actually very similar to the C language, but it also includes some C++ features, such as this pointer, events, etc.;
- The programming applied to the Vector CAN tool node is a language based on event modeling;
- You can use the write() function for debugging, which is used to output the debugging information to the write window of CANoe;
- Send the specified message through the output() function;
- Usually, the environment variable event is associated with the CANoe panel to achieve interaction;
- Provide a method to call the dll file (for operation, see " About the calling operation of dll in CAPL "One article); this ensures the calling of program modules encapsulated by other languages;
1.2, CAPL program structure
As follows, the structure of a complete CAPL program includes header files, global variables, event functions, and user-defined functions; of course, not every factor is required, depending on the specific program function.

1.3, CAPL data type

1.4. Overview of CAPL event types
CAPL is an event-based modeling language. From the introduction to the program structure of CAPL in Section 1.2, it can be seen that the use of CAPL is mainly to be familiar with the use of its events; its commonly used events are as follows:

Next, we further introduce several event types of CAPL
2. CAPL event type
2.1. System events
There are four types of CAPL system events: preStart, start, preStop, and stopMeasurement. We can define the desired operations in the corresponding system event function interface as needed; when the project is running, the sequence of occurrence of the following system events is
copypreStart-->start-->preStop-->stopMeasurement
The definition format of system events is as follows:
copyon preStart /*System events, executed during initialization*/ { resetCan(); /*CAPL Interface function for resetting the CAN controller*/ } on start /*System event, executed when the project starts*/ { write("Just A Try"); /*write()The function outputs the string information in the "write" window*/ } on preStop /*System event, executed when the project is ready to stop; occurs before the stopMeasurement event*/ { write("The Project Will Stop!"); } on stopMeasurement /*System event, executed when the project stops*/ { write("The End!\n"); }
2.2, CAN controller events
Execute when the hardware detects the corresponding action to the CAN controller; taking the BusOff event as an example, the format is as follows:
copyon busOff /*CAN Controller event: Executed when the hardware detects BusOff*/ { write("BusOff Error!"); }
2.3, CAN message event
The message event is defined by "on message", which will be called when the specified message is received. An example of the definition format of a message event is as follows:
copyon message 123 /*Execute when a message with the ID 123 (decimal) is received*/ on message 0x441 /*Execute when receiving a message with the ID 0x441 (hexadecimal)*/ on message BCM /*Execute when the message BCM (message name in the project dbc file) is received*/ on message* /*Execute when any message is received (note that there is no space between * and message)*/ on message 0x300-0x444 /*Execute when an ID packet within this range is received*/ { write("Received %x",this.id); /*Print received message id*/ write("Received Message %d in total!",count); }
The above is the definition format of the message event, and the index and sending operation of the message are introduced through the following example: Suppose VoiceStatus is a message defined in our project dbc file, which includes VoiceType and VoiceOperation. Among them, the variable VoiceType occupies the first byte; VoiceOperation occupies the second and third bytes; then Regarding the index of the message, the operation through the signal of the message (such as msg.VoiceType) is as follows:
copyvoid TxMsg_VoiceStatus(void) { message VoiceStatus msg; /*Name the VoiceStatus message defined in dbc in the project msg*/ msg.VoiceType = @VoiceType; /*Corresponding to the signal assigned to the message, called out by the message alias "msg."*/ msg.VoiceOperation = @VoiceOperation; output(msg); /*Send the message through the output command*/ }
It can also be operated directly by following the data type (msg.byte(0)). At this time, the operation is as follows:
copyvoid TxMsg_VoiceStatus(void) { message VoiceStatus msg; /*Name the VoiceStatus message defined in dbc in the project msg*/ msg.byte(0) = @VoiceType; /*The first data byte of the message*/ msg.word(1) = @VoiceOperation; ; /*A word (2 bytes) starting from the first byte of the message*/ output(msg); /*Send the message through the output command*/ }
2.4. Keyboard events
The keyboard event is defined by "on key", which will be executed when we press the specified key; an example of the definition format of the keyboard event is as follows:
copyon key 'a' /*In the lowercase input method, it will be executed when the 'A' key of the keyboard is pressed*/ on key 'A' /*In the uppercase input method, it will be executed when the 'A' key of the keyboard is pressed*/ on key ' ' /*Executed when pressing the space bar on the keyboard, note that there is a space between the single quotation marks*/ on key 0x20 /*Executed when the keyboard's space bar is pressed*/ on key F2 /*Executed when the keyboard's 'F2' key is pressed*/ on key CtrlF3 /*Executed when the keyboard's 'Ctrl' key and 'F3' key are pressed simultaneously*/ on key* /*It will be executed when any key on the keyboard is pressed (note that there is no space between * and key) */ { write("The Key Is Press"); }
2.5. Time events
The time event is defined by "on timer"; the event will be executed when the set time arrives. The definition format and usage examples of time events are as follows:
copyvariables { msTimer Timer1; /*Declare a timer variable Timer1 in ms in variables*/ } on start { setTimer(Timer1,100); /*Set the timing time of Timer1 to 100ms and start it*/ } on timer Timer1 /*The defined Timer1 time event is executed every 100ms*/ { setTimer(Timer1,100); /*start the next cycle*/ } on key 'a' /*Keyboard event, executed when the keyboard 'A' key is pressed*/ { cancelTimer(Timer1); /*Stop Timer1, a timer that executes once every 100ms*/ }
2.6. Error frame event
The error frame event is defined by "on errorFrame"; this event will be executed when the hardware detects an error frame. An example of the definition format of the error frame event is as follows:
copyon errorFrame /*Error frame event: executed when the hardware detects an error frame*/ { write("The error has occur"); }
2.7. Environment variable events
The environment variable event is defined by "on envVar"; this event will be executed when the specified environment variable value has a new input (environment variables are often used to associate the previous panel control, when we operate the control, the corresponding changes to the associated The value of the environment variable; at this time, our event about the environment variable in CAPL will be called; in this way, the interactive operation is completed). An example of the definition format of an environment variable event is as follows:
copyon envVar BCM_HightBeamAlarm /*Environment variable event: execute when the specified environment variable value has input*/ { byte num=0; num = getValue(this); /*You can use getValue( environment variable name/this keyword) to get the value of the specified environment variable*/ if(num == 1) { write("The envVar is %d",@BCM_HightBeamAlarm); } else { putValue(this,1);/*Use putValue (environment variable name/this keyword, set value) to change the value of the specified environment variable; for direct assignment, the format is @BCM_HighBeamAlarm = 1; */ write("Change envVar to %d",@BCM_HightBeamAlarm); } }
Regarding the operation of environment variables in CAPL, getValue() and putValue() are commonly used interface functions. Its function format is as follows. The specific introduction and examples can also be used to call the help document through the magic key "F1", and learn in the relevant chapters of "CAPL".


Ps: The definition of environment variables is done in the dbc file; the CANoe project imports the dbc file to use the defined environment variables. Environment variables are created as follows:

2.8. System variable events
Define the system variable event through "on sysvar"; this event will be executed when the specified system variable value has a new input, and its format and usage are basically the same as the environment variable in the previous section; the only difference is that the environment variable is in the dbc file defined in ; and system variables are defined as follows: Click "System Variables" under "Environment" on the toolbar; at this time, the interface is as follows, right-click on the blank space, and select "New" to create a new one; set the parameters of the newly created system variable in the pop-up window.

An example of the definition format of system variable events is as follows:
copyon sysvar SysVar1 /*System variable event: execute when the specified system variable value has new input*/ { write("The SysVar1 is %d",@SysVar1); }
Copyright statement: The content of this article is contributed spontaneously by Internet users, and the opinions of this article only represent the author himself. This site only provides information storage space services, does not own ownership, and does not assume relevant legal responsibilities. If you find any suspected infringement/violation of laws and regulations on this site, please send an email to report, once verified, this site will be deleted immediately.
Publisher: Full-stack Programmer Stack Manager, please indicate the source for reprinting: https://javaforall.cn/210047.html Original link: https://javaforall.cn