【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

相关文章

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

精选20个好玩又实用的的Python实战项目(有图文代码)

《精选20个好玩又实用的的Python实战项目(有图文代码)》文章介绍了20个实用Python项目,涵盖游戏开发、工具应用、图像处理、机器学习等,使用Tkinter、PIL、OpenCV、Kivy等库... 目录① 猜字游戏② 闹钟③ 骰子模拟器④ 二维码⑤ 语言检测⑥ 加密和解密⑦ URL缩短⑧ 音乐播放

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

C语言中%zu的用法解读

《C语言中%zu的用法解读》size_t是无符号整数类型,用于表示对象大小或内存操作结果,%zu是C99标准中专为size_t设计的printf占位符,避免因类型不匹配导致错误,使用%u或%d可能引发... 目录size_t 类型与 %zu 占位符%zu 的用途替代占位符的风险兼容性说明其他相关占位符验证示

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.