【DBC专题】-11-使用Cantools将CAN/CANFD DBC自动生成C语言代码

2024-03-19 23:20

本文主要是介绍【DBC专题】-11-使用Cantools将CAN/CANFD DBC自动生成C语言代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

1 安装Python和Cantools

1.1 查看Python已安装的Package包

1.2 在Python中安装Cantools插件包

1.3 获取更多Cantools工具的更新动态

2 经典CAN/CANFD DBC自动生成C语言代码

2.1 批处理文件CAN_DBC_To_C.bat内容说明

2.2 经典CAN/CANFD DBC文件要求

2.3 如何使用生成的C语言代码

3 附录:CAN DBC文件

4 附录:生成的can_dbc_demo.c文件

5 附录:生成的can_dbc_demo.h文件

结尾


优质博文推荐阅读(单击下方链接,即可跳转):

Vector工具链

CAN Matrix DBC

CAN Matrix Arxml

1 安装Python和Cantools

在Python Releases for Windows | Python.org链接中,找到你需要的Python版本(建议使用3.7及以上版本)下载,并安装。

1.1 查看Python已安装的Package包

快捷键Win + R,打开运行对话框,在其中输入CMD,单击确定

在弹出的“Windows 命令提示符”窗口,输入命令pip list,可以查看Python已安装的包,及其版本。

1.2 在Python中安装Cantools插件包

在弹出的“Windows 命令提示符”窗口,输入命令pip install cantools,安装Cantools插件包。

Cantools成功安装后提示

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

使用命令pip list,可以获取Cantools的版本。

1.3 获取更多Cantools工具的更新动态

在GitHub - cantools/cantools: CAN bus tools.链接中,可以关注Cantools该插件最新的更新动态。

2 经典CAN/CANFD DBC自动生成C语言代码

双击运行批处理文件CAN_DBC_To_C.bat,自动生成两个can_dbc_demo.c和can_dbc_demo.h,将这两个文件添加到你的编译环境中,并调用生成的API接口即可。

2.1 批处理文件CAN_DBC_To_C.bat内容说明

CAN_DBC_To_C.bat内容摘录如下,使用该批处理文件需要注意以下四点:

  • CAN DBC文件与批处理文件在同一个目录下;
  • CAN DBC文件不能数字,或特殊字符开头,否则生成的代码,不符合C语言语法;
  • 使用文本编辑工具,打开批处理文件,将CAN_DBC_DEMO替换成同目录下的DBC文件名称;
  • 使用文本编辑工具,打开批处理文件,将DCDC替换成DBC中你所使用的发送节点名称。
@echo off::切换至当前目录
cd.::经典CAN / CANFD DBC自动生成C语言代码 
python -m cantools  generate_c_source -e "gb18030" CAN_DBC_DEMO.dbc --node DCDC

2.2 经典CAN/CANFD DBC文件要求

这个很重要,决定了能否生成C代码文件,以及生成C代码文件的质量。

以使用的CAN_DBC_DEMO.dbc为例,该批处理文件自动生成的是节点DCDC所收/发的帧和信号的C语言代码。故没有在Tx Messages,Rx Messages,Mapped Tx Signals,Mapped Rx Signals出现的帧和信号,不会生成代码。

小结:在编辑MessageSignal时,需要明确该帧的“发送节点”,以及帧内信号的“接收节点”

如何准确的制作一个DBC文件,参考博文【DBC专题】-1-如何使用CANdb++ Editor创建并制作一个DBC_dbc文件中信号的值描述如何修改添加-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/qfmzhu/article/details/111403266

2.3 如何使用生成的C语言代码

API接口****_pack(例如can_dbc_demo_dcdc_tx_msg_0x200_pack)表示将本节点发送的信号的结构体(传参struct can_dbc_demo_dcdc_tx_msg_0x200_t *src_p)拼接成“字节数组”;

API接口****_unpack (例如can_dbc_demo_vcu_tx_msg_0x100_unpack) 表示将本节点接收的信号的结构体(传参struct can_dbc_demo_vcu_tx_msg_0x100_t *dst_p,),由“字节数组”进行解包得到。

3 附录:CAN DBC文件

将该内容复制到txt文件中,并将后缀txt修改为dbc,即可得到该DEMO DBC文件。

VERSION ""NS_ : NS_DESC_CM_BA_DEF_BA_VAL_CAT_DEF_CAT_FILTERBA_DEF_DEF_EV_DATA_ENVVAR_DATA_SGTYPE_SGTYPE_VAL_BA_DEF_SGTYPE_BA_SGTYPE_SIG_TYPE_REF_VAL_TABLE_SIG_GROUP_SIG_VALTYPE_SIGTYPE_VALTYPE_BO_TX_BU_BA_DEF_REL_BA_REL_BA_DEF_DEF_REL_BU_SG_REL_BU_EV_REL_BU_BO_REL_SG_MUL_VAL_BS_:BU_: DCDC VCUBO_ 512 DCDC_Tx_Msg_0x200: 8 DCDCSG_ DCDC_Signal_2 : 9|4@0+ (1,0) [0|0] "" Vector__XXXSG_ DCDC_Signal_1 : 7|14@0+ (1,0) [0|0] "" Vector__XXXBO_ 256 VCU_Tx_Msg_0x100: 8 VCUSG_ VCU_Signal_3 : 15|8@0+ (1,0) [0|0] "" Vector__XXXSG_ VCU_Signal_2 : 3|2@0+ (1,0) [0|0] ""  DCDCSG_ VCU_Signal_1 : 1|2@0+ (1,0) [0|0] ""  DCDCBA_DEF_  "BusType" STRING ;
BA_DEF_DEF_  "BusType" "CAN";

4 附录:生成的can_dbc_demo.c文件

/*** The MIT License (MIT)** Copyright (c) 2018-2019 Erik Moqvist** Permission is hereby granted, free of charge, to any person* obtaining a copy of this software and associated documentation* files (the "Software"), to deal in the Software without* restriction, including without limitation the rights to use, copy,* modify, merge, publish, distribute, sublicense, and/or sell copies* of the Software, and to permit persons to whom the Software is* furnished to do so, subject to the following conditions:** The above copyright notice and this permission notice shall be* included in all copies or substantial portions of the Software.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE* SOFTWARE.*//*** This file was generated by cantools version 39.4.5 Tue Mar 19 21:28:58 2024.*/#include <string.h>#include "can_dbc_demo.h"static inline uint8_t pack_left_shift_u8(uint8_t value,uint8_t shift,uint8_t mask)
{return (uint8_t)((uint8_t)(value << shift) & mask);
}static inline uint8_t pack_left_shift_u16(uint16_t value,uint8_t shift,uint8_t mask)
{return (uint8_t)((uint8_t)(value << shift) & mask);
}static inline uint8_t pack_right_shift_u8(uint8_t value,uint8_t shift,uint8_t mask)
{return (uint8_t)((uint8_t)(value >> shift) & mask);
}static inline uint8_t pack_right_shift_u16(uint16_t value,uint8_t shift,uint8_t mask)
{return (uint8_t)((uint8_t)(value >> shift) & mask);
}static inline uint8_t unpack_right_shift_u8(uint8_t value,uint8_t shift,uint8_t mask)
{return (uint8_t)((uint8_t)(value & mask) >> shift);
}int can_dbc_demo_dcdc_tx_msg_0x200_pack(uint8_t *dst_p,const struct can_dbc_demo_dcdc_tx_msg_0x200_t *src_p,size_t size)
{if (size < 8u) {return (-EINVAL);}memset(&dst_p[0], 0, 8);dst_p[0] |= pack_right_shift_u16(src_p->dcdc_signal_1, 6u, 0xffu);dst_p[1] |= pack_left_shift_u16(src_p->dcdc_signal_1, 2u, 0xfcu);dst_p[1] |= pack_right_shift_u8(src_p->dcdc_signal_2, 2u, 0x03u);dst_p[2] |= pack_left_shift_u8(src_p->dcdc_signal_2, 6u, 0xc0u);return (8);
}int can_dbc_demo_dcdc_tx_msg_0x200_init(struct can_dbc_demo_dcdc_tx_msg_0x200_t *msg_p)
{if (msg_p == NULL) return -1;memset(msg_p, 0, sizeof(struct can_dbc_demo_dcdc_tx_msg_0x200_t));return 0;
}uint16_t can_dbc_demo_dcdc_tx_msg_0x200_dcdc_signal_1_encode(double value)
{return (uint16_t)(value);
}bool can_dbc_demo_dcdc_tx_msg_0x200_dcdc_signal_1_is_in_range(uint16_t value)
{return (value <= 16383u);
}uint8_t can_dbc_demo_dcdc_tx_msg_0x200_dcdc_signal_2_encode(double value)
{return (uint8_t)(value);
}bool can_dbc_demo_dcdc_tx_msg_0x200_dcdc_signal_2_is_in_range(uint8_t value)
{return (value <= 15u);
}int can_dbc_demo_vcu_tx_msg_0x100_unpack(struct can_dbc_demo_vcu_tx_msg_0x100_t *dst_p,const uint8_t *src_p,size_t size)
{if (size < 8u) {return (-EINVAL);}dst_p->vcu_signal_2 = unpack_right_shift_u8(src_p[0], 2u, 0x0cu);dst_p->vcu_signal_1 = unpack_right_shift_u8(src_p[0], 0u, 0x03u);return (0);
}int can_dbc_demo_vcu_tx_msg_0x100_init(struct can_dbc_demo_vcu_tx_msg_0x100_t *msg_p)
{if (msg_p == NULL) return -1;memset(msg_p, 0, sizeof(struct can_dbc_demo_vcu_tx_msg_0x100_t));return 0;
}double can_dbc_demo_vcu_tx_msg_0x100_vcu_signal_2_decode(uint8_t value)
{return ((double)value);
}bool can_dbc_demo_vcu_tx_msg_0x100_vcu_signal_2_is_in_range(uint8_t value)
{return (value <= 3u);
}double can_dbc_demo_vcu_tx_msg_0x100_vcu_signal_1_decode(uint8_t value)
{return ((double)value);
}bool can_dbc_demo_vcu_tx_msg_0x100_vcu_signal_1_is_in_range(uint8_t value)
{return (value <= 3u);
}

5 附录:生成的can_dbc_demo.h文件

/*** The MIT License (MIT)** Copyright (c) 2018-2019 Erik Moqvist** Permission is hereby granted, free of charge, to any person* obtaining a copy of this software and associated documentation* files (the "Software"), to deal in the Software without* restriction, including without limitation the rights to use, copy,* modify, merge, publish, distribute, sublicense, and/or sell copies* of the Software, and to permit persons to whom the Software is* furnished to do so, subject to the following conditions:** The above copyright notice and this permission notice shall be* included in all copies or substantial portions of the Software.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE* SOFTWARE.*//*** This file was generated by cantools version 39.4.5 Tue Mar 19 21:28:58 2024.*/#ifndef CAN_DBC_DEMO_H
#define CAN_DBC_DEMO_H#ifdef __cplusplus
extern "C" {
#endif#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>#ifndef EINVAL
#    define EINVAL 22
#endif/* Frame ids. */
#define CAN_DBC_DEMO_DCDC_TX_MSG_0X200_FRAME_ID (0x200u)
#define CAN_DBC_DEMO_VCU_TX_MSG_0X100_FRAME_ID (0x100u)/* Frame lengths in bytes. */
#define CAN_DBC_DEMO_DCDC_TX_MSG_0X200_LENGTH (8u)
#define CAN_DBC_DEMO_VCU_TX_MSG_0X100_LENGTH (8u)/* Extended or standard frame types. */
#define CAN_DBC_DEMO_DCDC_TX_MSG_0X200_IS_EXTENDED (0)
#define CAN_DBC_DEMO_VCU_TX_MSG_0X100_IS_EXTENDED (0)/* Frame cycle times in milliseconds. *//* Signal choices. *//* Frame Names. */
#define CAN_DBC_DEMO_DCDC_TX_MSG_0X200_NAME "DCDC_Tx_Msg_0x200"
#define CAN_DBC_DEMO_VCU_TX_MSG_0X100_NAME "VCU_Tx_Msg_0x100"/* Signal Names. */
#define CAN_DBC_DEMO_DCDC_TX_MSG_0X200_DCDC_SIGNAL_1_NAME "DCDC_Signal_1"
#define CAN_DBC_DEMO_DCDC_TX_MSG_0X200_DCDC_SIGNAL_2_NAME "DCDC_Signal_2"
#define CAN_DBC_DEMO_VCU_TX_MSG_0X100_VCU_SIGNAL_2_NAME "VCU_Signal_2"
#define CAN_DBC_DEMO_VCU_TX_MSG_0X100_VCU_SIGNAL_1_NAME "VCU_Signal_1"
#define CAN_DBC_DEMO_VCU_TX_MSG_0X100_VCU_SIGNAL_3_NAME "VCU_Signal_3"/*** Signals in message DCDC_Tx_Msg_0x200.** All signal values are as on the CAN bus.*/
struct can_dbc_demo_dcdc_tx_msg_0x200_t {/*** Range: -* Scale: 1* Offset: 0*/uint16_t dcdc_signal_1;/*** Range: -* Scale: 1* Offset: 0*/uint8_t dcdc_signal_2;
};/*** Signals in message VCU_Tx_Msg_0x100.** All signal values are as on the CAN bus.*/
struct can_dbc_demo_vcu_tx_msg_0x100_t {/*** Range: -* Scale: 1* Offset: 0*/uint8_t vcu_signal_2;/*** Range: -* Scale: 1* Offset: 0*/uint8_t vcu_signal_1;/*** Range: -* Scale: 1* Offset: 0*/uint8_t vcu_signal_3;
};/*** Pack message DCDC_Tx_Msg_0x200.** @param[out] dst_p Buffer to pack the message into.* @param[in] src_p Data to pack.* @param[in] size Size of dst_p.** @return Size of packed data, or negative error code.*/
int can_dbc_demo_dcdc_tx_msg_0x200_pack(uint8_t *dst_p,const struct can_dbc_demo_dcdc_tx_msg_0x200_t *src_p,size_t size);/*** Init message fields to default values from DCDC_Tx_Msg_0x200.** @param[in] msg_p Message to init.** @return zero(0) on success or (-1) in case of nullptr argument.*/
int can_dbc_demo_dcdc_tx_msg_0x200_init(struct can_dbc_demo_dcdc_tx_msg_0x200_t *msg_p);/*** Encode given signal by applying scaling and offset.** @param[in] value Signal to encode.** @return Encoded signal.*/
uint16_t can_dbc_demo_dcdc_tx_msg_0x200_dcdc_signal_1_encode(double value);/*** Check that given signal is in allowed range.** @param[in] value Signal to check.** @return true if in range, false otherwise.*/
bool can_dbc_demo_dcdc_tx_msg_0x200_dcdc_signal_1_is_in_range(uint16_t value);/*** Encode given signal by applying scaling and offset.** @param[in] value Signal to encode.** @return Encoded signal.*/
uint8_t can_dbc_demo_dcdc_tx_msg_0x200_dcdc_signal_2_encode(double value);/*** Check that given signal is in allowed range.** @param[in] value Signal to check.** @return true if in range, false otherwise.*/
bool can_dbc_demo_dcdc_tx_msg_0x200_dcdc_signal_2_is_in_range(uint8_t value);/*** Unpack message VCU_Tx_Msg_0x100.** @param[out] dst_p Object to unpack the message into.* @param[in] src_p Message to unpack.* @param[in] size Size of src_p.** @return zero(0) or negative error code.*/
int can_dbc_demo_vcu_tx_msg_0x100_unpack(struct can_dbc_demo_vcu_tx_msg_0x100_t *dst_p,const uint8_t *src_p,size_t size);/*** Init message fields to default values from VCU_Tx_Msg_0x100.** @param[in] msg_p Message to init.** @return zero(0) on success or (-1) in case of nullptr argument.*/
int can_dbc_demo_vcu_tx_msg_0x100_init(struct can_dbc_demo_vcu_tx_msg_0x100_t *msg_p);/*** Decode given signal by applying scaling and offset.** @param[in] value Signal to decode.** @return Decoded signal.*/
double can_dbc_demo_vcu_tx_msg_0x100_vcu_signal_2_decode(uint8_t value);/*** Check that given signal is in allowed range.** @param[in] value Signal to check.** @return true if in range, false otherwise.*/
bool can_dbc_demo_vcu_tx_msg_0x100_vcu_signal_2_is_in_range(uint8_t value);/*** Decode given signal by applying scaling and offset.** @param[in] value Signal to decode.** @return Decoded signal.*/
double can_dbc_demo_vcu_tx_msg_0x100_vcu_signal_1_decode(uint8_t value);/*** Check that given signal is in allowed range.** @param[in] value Signal to check.** @return true if in range, false otherwise.*/
bool can_dbc_demo_vcu_tx_msg_0x100_vcu_signal_1_is_in_range(uint8_t value);#ifdef __cplusplus
}
#endif#endif

结尾

获取更多“汽车电子资讯”和“工具链使用”,

请关注CSDN博客“汽车电子助手”,做您的好助手。

这篇关于【DBC专题】-11-使用Cantools将CAN/CANFD DBC自动生成C语言代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/827551

相关文章

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

Springboot 中使用Sentinel的详细步骤

《Springboot中使用Sentinel的详细步骤》文章介绍了如何在SpringBoot中使用Sentinel进行限流和熔断降级,首先添加依赖,配置Sentinel控制台地址,定义受保护的资源,... 目录步骤 1: 添加 Sentinel 依赖步骤 2: 配置 Sentinel步骤 3: 定义受保护的

Python中Markdown库的使用示例详解

《Python中Markdown库的使用示例详解》Markdown库是一个用于处理Markdown文本的Python工具,这篇文章主要为大家详细介绍了Markdown库的具体使用,感兴趣的... 目录一、背景二、什么是 Markdown 库三、如何安装这个库四、库函数使用方法1. markdown.mark

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

MySQL数据库函数之JSON_EXTRACT示例代码

《MySQL数据库函数之JSON_EXTRACT示例代码》:本文主要介绍MySQL数据库函数之JSON_EXTRACT的相关资料,JSON_EXTRACT()函数用于从JSON文档中提取值,支持对... 目录前言基本语法路径表达式示例示例 1: 提取简单值示例 2: 提取嵌套值示例 3: 提取数组中的值注意

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

Go Mongox轻松实现MongoDB的时间字段自动填充

《GoMongox轻松实现MongoDB的时间字段自动填充》这篇文章主要为大家详细介绍了Go语言如何使用mongox库,在插入和更新数据时自动填充时间字段,从而提升开发效率并减少重复代码,需要的可以... 目录前言时间字段填充规则Mongox 的安装使用 Mongox 进行插入操作使用 Mongox 进行更

如何使用Spring boot的@Transactional进行事务管理

《如何使用Springboot的@Transactional进行事务管理》这篇文章介绍了SpringBoot中使用@Transactional注解进行声明式事务管理的详细信息,包括基本用法、核心配置... 目录一、前置条件二、基本用法1. 在方法上添加注解2. 在类上添加注解三、核心配置参数1. 传播行为(

在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程

《在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程》本文介绍了在Java中使用ModelMapper库简化Shapefile属性转JavaBean的过程,对比... 目录前言一、原始的处理办法1、使用Set方法来转换2、使用构造方法转换二、基于ModelMapper

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.