CAPL Script

Setting Up the Development Environment

Learning Objectives

After completing this article, you will be able to:

  • Understand the basic structure of CANoe projects
  • Familiarize yourself with the CAPL Browser interface layout and core functions
  • Independently create, write, and compile a CAPL program
  • Add CAPL nodes to the simulation environment and run them

Preparation

Before we begin, please ensure you have installed CANoe. If you haven't installed it yet, please contact your IT department or obtain a trial version from the Vector website.

What is a CANoe Project?

A CANoe project (.cfg file) is the container for all your work. It contains:

  • Network configuration: Defines which buses are used (CAN, LIN, FlexRay, etc.)
  • Database: .dbc files that define the meaning of messages and signals
  • Simulation nodes: Your CAPL programs will run as simulation nodes
  • Test configuration: Automated test modules and test cases

You can think of a CANoe project as a "virtual car" - it contains various ECUs (simulation nodes) that communicate with each other through buses (networks).

[!SCREENSHOT]
Location: CANoe main interface
Content: Shows a typical CANoe project structure
Annotation: Circle the Simulation Setup and Configuration panels


Getting to Know CAPL Browser

CAPL Browser is the Integrated Development Environment (IDE) for CAPL. You will write, compile, and debug all CAPL code here.

Opening CAPL Browser

There are two ways to open CAPL Browser:

Method 1: From CANoe main interface

  1. Open a CANoe project
  2. Select ViewCAPL Browser from the menu bar

Method 2: From a simulation node

  1. In the Simulation Setup window, double-click a CAPL node
  2. CAPL Browser will automatically open the node's source code

[!SCREENSHOT]
Location: CANoe menu bar
Content: View menu expanded, showing the CAPL Browser option
Annotation: Circle the CAPL Browser menu item with a red box

CAPL Browser Interface Layout

After opening CAPL Browser, you will see the following core areas:

Area Function
Ribbon Top toolbar containing file operations, compilation, debugging, and other functions
Text Editor Central area for writing CAPL code
Navigator Left panel for quickly jumping to functions and events in the code
CAPL Function Explorer Right panel showing all available CAPL functions
Symbol Explorer Displays messages and signals defined in the database
Output Window Bottom area showing compilation results and error information

[!SCREENSHOT]
Location: CAPL Browser complete interface
Content: Shows all the areas mentioned above
Annotation: Number the areas (1-Ribbon, 2-Editor, 3-Navigator, 4-Function Explorer, 5-Output Window)

Ribbon Functional Areas

CAPL Browser's Ribbon is divided into several main tabs:

  • File: File operations (New, Open, Save)
  • Home: Compilation, editing tools, environment import
  • Filter: Filter displayed functions and symbols
  • Debug: Breakpoint management, runtime error checking

Among these, the Home tab is what you'll use most frequently, especially the Compile button.


Hands-On Practice: Hello, CANoe!

Now, let's create your first CAPL program together!

Step 1: Create a New CAPL File

  1. In CAPL Browser, click the File tab
  2. Click the New button
  3. Select CAPL File
  4. The system will open a new blank tab page in the editor

[!SCREENSHOT]
Location: CAPL Browser → File tab
Content: Shows the New button and dropdown menu
Annotation: Circle the "CAPL File" option

At this point, you'll see a blank editing area ready to accept your code.

Step 2: Write Your First Event

Enter the following code in the editor:

on key 'a'
{
    write("Hello, CANoe!");
}

Let's understand this code line by line:

on key 'a'      // When the user presses the 'a' key on the keyboard
{
    write("Hello, CANoe!");  // Print this message to the Write window
}
  • on key 'a': This is an event handler, meaning "when the 'a' key on the keyboard is pressed"
  • write(): This is CAPL's built-in function for outputting text to CANoe's Write window

Step 3: Save the File

  1. Press Ctrl + S or click the save icon in the toolbar
  2. Choose a save location (recommended to save in the CANoe project directory)
  3. Enter a filename, such as HelloWorld.can

Tip: CAPL source files have the .can extension, and after compilation, a .cbf file (CAPL Binary Format) will be generated.

Step 4: Compile the Program

  1. Click the Compile button in the Home tab (or press F9)
  2. Check the Output Window at the bottom

If everything is normal, you'll see output similar to this:

Compiling HelloWorld.can...
0 error(s), 0 warning(s)

[!SCREENSHOT]
Location: CAPL Browser → Output Window
Content: Shows compilation success message
Annotation: Circle "0 error(s), 0 warning(s)"

Congratulations! Your first CAPL program has compiled successfully.

Step 5: Add CAPL Node to Simulation Environment

Successful compilation is only the first step. To make the program actually run, you need to add it to CANoe's simulation environment:

  1. Return to the CANoe main interface
  2. Open the Simulation Setup window (ViewSimulation Setup)
  3. Right-click in the network node tree and select Insert Network Node
  4. In the dialog that opens, browse and select the HelloWorld.can file you just saved
  5. Click OK, and the node will appear in the simulation environment

[!SCREENSHOT]
Location: CANoe Simulation Setup window
Content: Right-click menu showing Insert Network Node option
Annotation: Circle the menu item and the newly added node

Step 6: Run and Test

  1. Ensure the Write window is open (ViewWrite)
  2. Click the Start (Start Measurement) button in the toolbar, or press F9
  3. Press the a key on the keyboard
  4. Observe the Write window

You should see:

Hello, CANoe!

[!SCREENSHOT]
Location: CANoe Write window
Content: Shows "Hello, CANoe!" output
Annotation: Circle the output text and timestamp

Awesome! You have successfully created and run your first CAPL program!


Troubleshooting Common Issues

During this process, you may encounter some problems. Here are the most common situations and solutions:

Issue 1: Compilation Error

Phenomenon: Output Window shows red error messages.

Troubleshooting steps:

  1. Double-click the error message, and the editor will automatically jump to the erroneous code line
  2. Check if the syntax is correct (brackets match, semicolons not missing)
  3. Ensure strings use English double quotes ", not Chinese quotation marks

Common error examples:

// Wrong: Missing semicolon
write("Hello")

// Correct:
write("Hello");

Issue 2: Key Has No Response

Phenomenon: Pressing the a key produces no output in the Write window.

Troubleshooting steps:

  1. Confirm that measurement has started (toolbar shows "running" status)
  2. Confirm that the CAPL node has been correctly added to Simulation Setup
  3. Confirm that the node is enabled (node icon is not crossed out)
  4. Confirm that the focus is on the CANoe main window (not another application)

Issue 3: Can't Find Write Window

Solution:

  • Click menu ViewWrite
  • Or select Write from the Window dropdown menu in the toolbar

Issue 4: Special Characters Causing Compilation Failure

Important note: Special characters are not allowed in CAPL code (outside quotes), including:

  • Chinese characters
  • German umlauts (ä, ö, ü)
  • Some special space characters

If your comments need to use Chinese, ensure the file is saved with UTF-8 encoding, and place Chinese inside quoted strings or use English comments entirely.


Summary

Through this article, we have accomplished:

  • Understanding the basic structure and concepts of CANoe projects
  • Familiarizing ourselves with CAPL Browser's interface layout (editor, navigator, output window, etc.)
  • Hands-on creation of the first CAPL program: responding to key events and outputting text
  • Learning the complete process of compiling programs, adding simulation nodes, and running measurements
  • Mastering troubleshooting methods for several common issues

Exercises

  1. Extended Exercise: Modify the program so that pressing the b key outputs "Goodbye, CANoe!" Hint: You'll need to add another on key event.

  2. Exploration Exercise: Try using the writeEx() function instead of write() and observe the different output.

  3. Challenge Exercise: Create a program that outputs "One" when pressing the 1 key, "Two" when pressing the 2 key, and "Three" when pressing the 3 key.