【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

相关文章

C语言中联合体union的使用

本文编辑整理自: http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=179471 一、前言 “联合体”(union)与“结构体”(struct)有一些相似之处。但两者有本质上的不同。在结构体中,各成员有各自的内存空间, 一个结构变量的总长度是各成员长度之和。而在“联合”中,各成员共享一段内存空间, 一个联合变量

Tolua使用笔记(上)

目录   1.准备工作 2.运行例子 01.HelloWorld:在C#中,创建和销毁Lua虚拟机 和 简单调用。 02.ScriptsFromFile:在C#中,对一个lua文件的执行调用 03.CallLuaFunction:在C#中,对lua函数的操作 04.AccessingLuaVariables:在C#中,对lua变量的操作 05.LuaCoroutine:在Lua中,

uniapp接入微信小程序原生代码配置方案(优化版)

uniapp项目需要把微信小程序原生语法的功能代码嵌套过来,无需把原生代码转换为uniapp,可以配置拷贝的方式集成过来 1、拷贝代码包到src目录 2、vue.config.js中配置原生代码包直接拷贝到编译目录中 3、pages.json中配置分包目录,原生入口组件的路径 4、manifest.json中配置分包,使用原生组件 5、需要把原生代码包里的页面修改成组件的方

Vim使用基础篇

本文内容大部分来自 vimtutor,自带的教程的总结。在终端输入vimtutor 即可进入教程。 先总结一下,然后再分别介绍正常模式,插入模式,和可视模式三种模式下的命令。 目录 看完以后的汇总 1.正常模式(Normal模式) 1.移动光标 2.删除 3.【:】输入符 4.撤销 5.替换 6.重复命令【. ; ,】 7.复制粘贴 8.缩进 2.插入模式 INSERT

公共筛选组件(二次封装antd)支持代码提示

如果项目是基于antd组件库为基础搭建,可使用此公共筛选组件 使用到的库 npm i antdnpm i lodash-esnpm i @types/lodash-es -D /components/CommonSearch index.tsx import React from 'react';import { Button, Card, Form } from 'antd'

Lipowerline5.0 雷达电力应用软件下载使用

1.配网数据处理分析 针对配网线路点云数据,优化了分类算法,支持杆塔、导线、交跨线、建筑物、地面点和其他线路的自动分类;一键生成危险点报告和交跨报告;还能生成点云数据采集航线和自主巡检航线。 获取软件安装包联系邮箱:2895356150@qq.com,资源源于网络,本介绍用于学习使用,如有侵权请您联系删除! 2.新增快速版,简洁易上手 支持快速版和专业版切换使用,快速版界面简洁,保留主

如何免费的去使用connectedpapers?

免费使用connectedpapers 1. 打开谷歌浏览器2. 按住ctrl+shift+N,进入无痕模式3. 不需要登录(也就是访客模式)4. 两次用完,关闭无痕模式(继续重复步骤 2 - 4) 1. 打开谷歌浏览器 2. 按住ctrl+shift+N,进入无痕模式 输入网址:https://www.connectedpapers.com/ 3. 不需要登录(也就是

17.用300行代码手写初体验Spring V1.0版本

1.1.课程目标 1、了解看源码最有效的方式,先猜测后验证,不要一开始就去调试代码。 2、浓缩就是精华,用 300行最简洁的代码 提炼Spring的基本设计思想。 3、掌握Spring框架的基本脉络。 1.2.内容定位 1、 具有1年以上的SpringMVC使用经验。 2、 希望深入了解Spring源码的人群,对 Spring有一个整体的宏观感受。 3、 全程手写实现SpringM

大语言模型(LLMs)能够进行推理和规划吗?

大语言模型(LLMs),基本上是经过强化训练的 n-gram 模型,它们在网络规模的语言语料库(实际上,可以说是我们文明的知识库)上进行了训练,展现出了一种超乎预期的语言行为,引发了我们的广泛关注。从训练和操作的角度来看,LLMs 可以被认为是一种巨大的、非真实的记忆库,相当于为我们所有人提供了一个外部的系统 1(见图 1)。然而,它们表面上的多功能性让许多研究者好奇,这些模型是否也能在通常需要系

代码随想录算法训练营:12/60

非科班学习算法day12 | LeetCode150:逆波兰表达式 ,Leetcode239: 滑动窗口最大值  目录 介绍 一、基础概念补充: 1.c++字符串转为数字 1. std::stoi, std::stol, std::stoll, std::stoul, std::stoull(最常用) 2. std::stringstream 3. std::atoi, std