目录
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出现的帧和信号,不会生成代码。
小结:在编辑Message和Signal时,需要明确该帧的“发送节点”,以及帧内信号的“接收节点”。
如何准确的制作一个DBC文件,参考博文【DBC专题】-1-如何使用CANdb++ Editor创建并制作一个DBC_dbc文件中信号的值描述如何修改添加-CSDN博客https://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博客“汽车电子助手”,做您的好助手。