Automatic C Code Generation from Classic CAN/CANFD DBC

Table of Contents

1 Install Python and Cantools

1.1 Check Installed Python Packages

1.2 Install Cantools Plugin in Python

1.3 Get More Updates on Cantools

2 Automatic C Code Generation from Classic CAN/CANFD DBC

2.1 Explanation of the Batch File CAN_DBC_To_C.bat

2.2 Requirements for Classic CAN/CANFD DBC Files

2.3 How to Use the Generated C Code

Click below to join the WeChat group for “NXP S32K3 Series Embedded Development Group 1”

Official Account: Automotive Electronics Assistant welcomes you to join the 【NXP S32K3 Series Embedded Development Group 1】
1 Install Python and Cantools

Go to the link for Python Releases for Windows | Python.org, find the version of Python you need (it is recommended to use version 3.7 or above) and download it.

Automatic C Code Generation from Classic CAN/CANFD DBC

1.1 Check Installed Python Packages

Press Win + R to open the Run dialog, type CMD, and click OK.

Automatic C Code Generation from Classic CAN/CANFD DBC

In the popped-up “Windows Command Prompt” window, type the command pip list to view the installed Python packages and their versions.

Automatic C Code Generation from Classic CAN/CANFD DBC

1.2 Install Cantools Plugin in Python

In the popped-up “Windows Command Prompt” window, type the command pip install cantools to install the Cantools plugin package.

Automatic C Code Generation from Classic CAN/CANFD DBC

After successful installation, you will see the message:

Successfully installed argparse-addons-0.12.0 bitstruct-8.19.0 cantools-39.4.5 crccheck-1.3.0 diskcache-5.6.3 packaging-24.0 python-can-4.3.1 pywin32-306 textparser-0.24.0 typing-extensions-4.10.0 wrapt-1.16.0

Use the command pip list to check the version of Cantools.

Automatic C Code Generation from Classic CAN/CANFD DBC

1.3 Get More Updates on Cantools

Follow the link on GitHub – cantools/cantools: CAN bus tools. to keep track of the latest updates on the Cantools plugin.

Automatic C Code Generation from Classic CAN/CANFD DBC

2 Automatic C Code Generation from Classic CAN/CANFD DBC

Double-click to run the batch file CAN_DBC_To_C.bat, which will automatically generate two files: can_dbc_demo.c and can_dbc_demo.h. Add these files to your compilation environment and call the generated API interfaces.

Automatic C Code Generation from Classic CAN/CANFD DBC

2.1 Explanation of the Batch File CAN_DBC_To_C.bat

The content of CAN_DBC_To_C.bat is as follows. Please note the following four points when using this batch file:

lThe CAN DBC file must be in the same directory as the batch file;

lThe CAN DBC file cannot start with a number or special character, otherwise the generated code will not comply with C language syntax;

lUse a text editor to open the batch file and replace CAN_DBC_DEMO with the name of the DBC file in the same directory;

lUse a text editor to open the batch file and replace DCDC with the name of the sending node used in the DBC.

@echo off ::Change to the current directorycd. ::Automatically generate C code from classic CAN / CANFD DBC python -m cantools generate_c_source -e "gb18030" CAN_DBC_DEMO.dbc --node DCDC

Automatic C Code Generation from Classic CAN/CANFD DBC

2.2 Requirements for Classic CAN/CANFD DBC Files

This is very important as it determines whether C code files can be generated, and the quality of the generated C code files.

Taking the used CAN_DBC_DEMO.dbc as an example, the batch file will automatically generate C code for the frames and signals received/sent by the node DCDC. Therefore, frames and signals that do not appear in Tx Messages, Rx Messages, Mapped Tx Signals, or Mapped Rx Signals will not generate code.

Summary: When editing Messages and Signals, it is essential to clarify the “sending node” of the frame and the “receiving node” of the signals within the frame.

Automatic C Code Generation from Classic CAN/CANFD DBC

For how to accurately create a DBC file, refer to the blog post DBC Topic】-1-How to use CANdb++ Editor to create and modify the value descriptions of signals in a DBC_dbc file – CSDN Blog

2.3 How to Use the Generated C Code

The API interface ****_pack (e.g., can_dbc_demo_dcdc_tx_msg_0x200_pack) indicates that the structure of the signals sent by this node (parameter struct can_dbc_demo_dcdc_tx_msg_0x200_t *src_p) is packed into a “byte array”;

The API interface ****_unpack (e.g., can_dbc_demo_vcu_tx_msg_0x100_unpack) indicates that the structure of the signals received by this node (parameter struct can_dbc_demo_vcu_tx_msg_0x100_t *dst_p) is unpacked from the “byte array”.

END

For more “Automotive Electronics News” and “Toolchain Usage”, please follow “Automotive Electronics Assistant, your good assistant!

Automatic C Code Generation from Classic CAN/CANFD DBC

>>>Undertake: Autosar Project Development Outsourcing+ Autosar Training<<<

Automatic C Code Generation from Classic CAN/CANFD DBC

Leave a Comment