【DBC专题】-10-CAN DBC转换C语言代码Demo_接收Rx报文篇
创始人
2024-05-31 03:56:16
0

案例背景(共15页精讲):
该篇博文将告诉您,CAN DBC转换C语言代码Demo,只需传递对应CAN信号关联参数,无需每个信号"左移"和"右移",并举例介绍:在CANoe/Canalyzer中CAPL中的应用:对接收报文,进行解包。其它场景的应用,也可参考该篇。

目录

1 背景:CAN信号在CAN 报文/Frame中位置的决定因素

2 讲解:CAN DBC转换C语言代码Demo

2.1 定义一些宏定义

2.2 定义一些结构体

2.3 API接口函数原型

2.4 对CANFD DBC的支持

2.5 如何使用该API接口

3 举例:在CANoe/Canalyzer中CAPL中的应用:对接收报文,进行解包

3.1 信号的字节顺序Byte Order: Intel小端模式

3.1.1 CAPL文件can的完整代码

3.1.2 系统变量定义

3.1.3 测试结果

3.2信号的字节顺序Byte Order: Motorola大端模式

3.2.1 CAPL文件can的完整代码

3.2.2 系统变量定义

3.2.3 测试结果

结尾


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

Vector工具链

CAN Matrix DBC

CAN Matrix Arxml

1 背景:CAN信号在CAN 报文/Frame中位置的决定因素

一个CAN信号在CAN 报文/Frame中的位置,由3个条件决定,见图1-1:

  • 信号的起始位Startbit:默认为信号的Lsb;
  • 信号的长度Length
  • 信号的字节顺序Byte Order: Intel小端模式和Motorola大端模式。

详见博文“【DBC专题】-4-DBC文件中的Signal信号字节顺序Motorola和Intel介绍icon-default.png?t=N176https://blog.csdn.net/qfmzhu/article/details/111561710”,这里不再重复讲解。

下面我们根据这个背景,来设计一个通用算法,封装成一个函数,只需输入几个参数,即可得到对应的信号值。

图1-1

 

2 讲解:CAN DBC转换C语言代码Demo

2.1 定义一些宏定义

#define FRAME_TJW_ZERO_DLC  0   // unit:Byte
#define FRAME_TJW_MAX_DLC  8    // unit:Byte
#define SIGNAL_TJW_ZERO_LENGTH  0    // unit:Bit
#define SIGNAL_TJW_MAX_LENGTH  64    // unit:Bit
#define SIGNAL_TJW_INIT_VALUE  0
#define SIGNAL_TJW_INIT_MASK  0#define SIGNAL_TJW_BYTE_ORDER_INTEL  0
#define SIGNAL_TJW_BYTE_ORDER_MOTOROLA  1#define CAN_SIGNAL_TJW_NORMAL  0
#define CAN_SIGNAL_TJW_NO_VALUE  1
#define CAN_FRAME_TJW_DLC_OUT_OF_RANGE  2
#define INVALID_INPUT_PARAMETERS_TJW  3/** 使用该Demo需注明出处,以表对作者的尊重 **/
/** 版权归CSDN博客“汽车电子助手”所有,https://blog.csdn.net/qfmzhu **/

2.2 定义一些结构体

typedef struct
{uint8 Signal_State;      // Describes the state of a signaluint64 Signal_Value;     // Describes the value of a signal
} Unpack_CAN_Signal;
/** 使用该Demo需注明出处,以表对作者的尊重 **/
/** 版权归CSDN博客“汽车电子助手”所有,https://blog.csdn.net/qfmzhu **/

2.3 API接口函数原型

/** 使用该Demo需注明出处,以表对作者的尊重 **/
/** 版权归CSDN博客“汽车电子助手”所有,https://blog.csdn.net/qfmzhu **/Unpack_CAN_Signal Rx_CAN_Frame_Data_Buff_to_CAN_Signal_Hex_Fun(
uint8* data_buff,          /**Byte array of CAN data field**/
uint8 signal_start_bit,    /**Rang:0~(FRAME_TJW_MAX_DLC * 8 - 1)**/
uint8 signal_length,       /**Rang:1~SIGNAL_TJW_MAX_LENGTH**/
boolean signal_byte_order, /**0:Intel;1:Motorola**/
uint8 frame_length)        /**Rang:1~FRAME_TJW_MAX_DLC**/
{Unpack_CAN_Signal can_signal;uint8 i = 0;uint8 byte_num = 0;uint8 low_data_byte = 0;uint8 high_data_byte = 0;uint8 low_data_byte_position = 0;uint8 high_data_byte_position = 0;uint8 remaining_bit_number = 0;uint8 remaining_byte_number = 0;	uint64 can_signal_mask = SIGNAL_TJW_INIT_MASK;can_signal.Signal_State = CAN_SIGNAL_TJW_NORMAL;can_signal.Signal_Value = SIGNAL_TJW_INIT_VALUE;if(frame_length == FRAME_TJW_ZERO_DLC)            /**DLC of CAN frame is 0**/{can_signal.Signal_State = CAN_SIGNAL_TJW_NO_VALUE;}else if(frame_length > FRAME_TJW_MAX_DLC)         /**DLC of CAN frame is out of range**/{can_signal.Signal_State = CAN_FRAME_TJW_DLC_OUT_OF_RANGE;}else if((signal_length > (frame_length * 8))         /**Invalid signal_length: The length of the signal is not in the range of DLC**/|| (signal_length > SIGNAL_TJW_MAX_LENGTH)          /**Invalid signal_length: The length of the signal exceeds the maximum Unsigned of C language**/|| (signal_length == SIGNAL_TJW_ZERO_LENGTH)        /**Invalid signal_length: The length of the signal is not equal to 0**/				){can_signal.Signal_State = INVALID_INPUT_PARAMETERS_TJW;}else{		/**Byte Order: Intel**/if(signal_byte_order == SIGNAL_TJW_BYTE_ORDER_INTEL)  {/**Bytes used by CAN signal,Byte[0] is low byte,Byte[frame_length] is high byte**/low_data_byte = signal_start_bit / 8;high_data_byte = (signal_start_bit + signal_length - 1) / 8;  // 2 + 6 - 1 /8 = 0;  2 + 7 - 1 /8 = 1 ;  2 + 14 - 1 /8 = 1 ;  2 + 15 - 1 /8 = 2if((high_data_byte + 1) > frame_length)  /** CAN signal is not in the range of DLC**/{can_signal.Signal_State = CAN_FRAME_TJW_DLC_OUT_OF_RANGE;}else{for(i = 0;i < signal_length;i++)  /**Generate signal mask according to signal length**/{can_signal_mask |= 0x0000000000000001 << i;}low_data_byte_position = signal_start_bit % 8;  /**Distance from the lowest bit of each byte**//**CAN Signal is in a bytes**/if(low_data_byte == high_data_byte) {					can_signal.Signal_Value = (((uint64)data_buff[low_data_byte]) >> low_data_byte_position) & can_signal_mask;				}/**CAN Signal in multiple bytes**/else{					for(byte_num = low_data_byte;byte_num <= high_data_byte;byte_num++){if(byte_num == low_data_byte){can_signal.Signal_Value |= ((uint64)data_buff[low_data_byte]) >> low_data_byte_position;}else{can_signal.Signal_Value |= ((uint64)data_buff[byte_num]) << ((byte_num - low_data_byte - 1) * 8 + (8 - low_data_byte_position));}						}can_signal.Signal_Value = can_signal.Signal_Value & can_signal_mask;					}				}}/**Byte Order: Motorola**/else  /**signal_byte_order == SIGNAL_TJW_BYTE_ORDER_MOTOROLA**/{/**Bytes used by CAN signal,Byte[0] is low byte,Byte[frame_length] is high byte**/high_data_byte = signal_start_bit / 8;	if((high_data_byte + 1) > frame_length)        /** CAN signal is not in the range of DLC**/{can_signal.Signal_State = CAN_FRAME_TJW_DLC_OUT_OF_RANGE;}else{for(i = 0;i < signal_length;i++)  /**Generate signal mask according to signal length**/{can_signal_mask |= 0x0000000000000001 << i;}high_data_byte_position = signal_start_bit % 8;  /**Distance from the lowest bit of each byte**//**CAN Signal is in a bytes**/if(signal_length <= (8 - high_data_byte_position)){				can_signal.Signal_Value = (((uint64)data_buff[high_data_byte]) >> high_data_byte_position) & can_signal_mask;					}/**CAN Signal in multiple bytes**/else{			remaining_bit_number = signal_length - (8 - high_data_byte_position); // 剩余bit = 12 - (8 - 2) = 6if((remaining_bit_number % 8) != 0)     {remaining_byte_number = remaining_bit_number / 8 + 1;          // remaining_byte_number = 1}else{remaining_byte_number = remaining_bit_number / 8;}for(byte_num = high_data_byte;byte_num >= (high_data_byte - remaining_byte_number);byte_num--)  // high_data_byte = 2  remaining_byte_number = 1{						if(byte_num == high_data_byte){can_signal.Signal_Value |= ((uint64)data_buff[high_data_byte]) >> high_data_byte_position;}else{can_signal.Signal_Value |= ((uint64)data_buff[byte_num]) << ((high_data_byte - byte_num - 1) * 8 + (8 - high_data_byte_position));}		if(byte_num == 0)	{break;}						}can_signal.Signal_Value = can_signal.Signal_Value & can_signal_mask;									}			}				}}return can_signal;
}
/** 使用该Demo需注明出处,以表对作者的尊重 **/
/** 版权归CSDN博客“汽车电子助手”所有,https://blog.csdn.net/qfmzhu **/

2.4 对CANFD DBC的支持

只需将宏定义FRAME_TJW_MAX_DLC的值由8调整为64,即可适配CANFD DBC。

2.5 如何使用该API接口

1、提供的demo,使用了uint8,uint64,boolean类型,在将该代码集成至开发环境中,需要注意这些类型的替换:

  • typedef unsigned char               uint8
  • typedef unsigned long long           uint64

2、调用函数Rx_CAN_Frame_Data_Buff_to_CAN_Signal_Hex_Fun时,需要确认传参data_buff,signal_start_bit,signal_length,signal_byte_order,frame_length的准确性,需与DBC保持一致,这样您可以得到对应信号的状态与值;

3 举例:在CANoe/Canalyzer中CAPL中的应用:对接收报文,进行解包

CAPL语法和C语言有一些差异,我们将第2.3章节中的demo稍作修改,以满足测需要。其它场景的应用与其类似。

3.1 信号的字节顺序Byte Order: Intel小端模式

3.1.1 CAPL文件can的完整代码

/** 使用该Demo需注明出处,以表对作者的尊重 **/
/** 版权归CSDN博客“汽车电子助手”所有,https://blog.csdn.net/qfmzhu **/
/*@!Encoding:936*/
includes
{}variables
{byte CAN_Data_Byte[8];byte Signal_State;word Signal_Value;
}on message 0x100
{CAN_Data_Byte[0] = this.byte(0);CAN_Data_Byte[1] = this.byte(1);CAN_Data_Byte[2] = this.byte(2);CAN_Data_Byte[3] = this.byte(3);CAN_Data_Byte[4] = this.byte(4); CAN_Data_Byte[5] = this.byte(5);  CAN_Data_Byte[6] = this.byte(6);CAN_Data_Byte[7] = this.byte(7);@sysvar::CAN_Frame::sysvar_Test_Signal_1 = Rx_CAN_Frame_Data_Buff_to_CAN_Signal_Hex(0,2,8);@sysvar::CAN_Frame::sysvar_Test_Signal_2 = Rx_CAN_Frame_Data_Buff_to_CAN_Signal_Hex(50,12,8);@sysvar::CAN_Frame::sysvar_Test_Signal_3 = Rx_CAN_Frame_Data_Buff_to_CAN_Signal_Hex(62,2,8);
}word Rx_CAN_Frame_Data_Buff_to_CAN_Signal_Hex(
byte signal_start_bit,    /**Rang:0~(8 * 8 - 1)**/
byte signal_length,       /**Rang:1~64**/
byte frame_length)        /**Rang:1~8**/
{byte i = 0;byte byte_num = 0;byte low_data_byte = 0;byte high_data_byte = 0;byte low_data_byte_position = 0;byte high_data_byte_position = 0;byte remaining_bit_number = 0;byte remaining_byte_number = 0;	word can_signal_mask = 0;Signal_State = 0;Signal_Value = 0;if(frame_length == 0)            /**DLC of CAN frame is 0**/{Signal_State = 1;}else if(frame_length > 8)         /**DLC of CAN frame is out of range**/{Signal_State = 2;}else if((signal_length > (frame_length * 8))         /**Invalid signal_length: The length of the signal is not in the range of DLC**/|| (signal_length > 16)          /**Invalid signal_length: The length of the signal exceeds the maximum Unsigned of C language**/|| (signal_length == 0)        /**Invalid signal_length: The length of the signal is not equal to 0**/				){Signal_State = 3;}else{/**Byte Order: Intel**/{/**Bytes used by CAN signal,Byte[0] is low byte,Byte[frame_length] is high byte**/low_data_byte = signal_start_bit / 8;high_data_byte = (signal_start_bit + signal_length - 1) / 8;  // 2 + 6 - 1 /8 = 0;  2 + 7 - 1 /8 = 1 ;  2 + 14 - 1 /8 = 1 ;  2 + 15 - 1 /8 = 2if((high_data_byte + 1) > frame_length)  /** CAN signal is not in the range of DLC**/{Signal_State = 2;}else{		for(i = 0;i < signal_length;i++)  /**Generate signal mask according to signal length**/{can_signal_mask |= 0x0000000000000001 << i;}low_data_byte_position = signal_start_bit % 8;  /**Distance from the lowest bit of each byte**//**CAN Signal is in a bytes**/if(low_data_byte == high_data_byte) {			Signal_Value = (((word)CAN_Data_Byte[low_data_byte]) >> low_data_byte_position) & can_signal_mask;				}/**CAN Signal in multiple bytes**/else{			for(byte_num = low_data_byte;byte_num <= high_data_byte;byte_num++){				if(byte_num == low_data_byte){		Signal_Value |= ((word)CAN_Data_Byte[low_data_byte]) >> low_data_byte_position;}else{			Signal_Value |= ((word)CAN_Data_Byte[byte_num]) << ((byte_num - low_data_byte - 1) * 8 + (8 - low_data_byte_position));}				}Signal_Value = Signal_Value & can_signal_mask;					}				}}}return Signal_Value;
}
/** 使用该Demo需注明出处,以表对作者的尊重 **/
/** 版权归CSDN博客“汽车电子助手”所有,https://blog.csdn.net/qfmzhu **/

3.1.2 系统变量定义

3.1.3 测试结果

使用的DBC:

CAN DBC中信号与系统变量的解析结果一致:

3.2信号的字节顺序Byte Order: Motorola大端模式

3.2.1 CAPL文件can的完整代码

/** 使用该Demo需注明出处,以表对作者的尊重 **/
/** 版权归CSDN博客“汽车电子助手”所有,https://blog.csdn.net/qfmzhu **/
/*@!Encoding:936*/
includes
{}variables
{byte CAN_Data_Byte[8];byte Signal_State;word Signal_Value;
}on message 0x100
{CAN_Data_Byte[0] = this.byte(0);CAN_Data_Byte[1] = this.byte(1);CAN_Data_Byte[2] = this.byte(2);CAN_Data_Byte[3] = this.byte(3);CAN_Data_Byte[4] = this.byte(4); CAN_Data_Byte[5] = this.byte(5);  CAN_Data_Byte[6] = this.byte(6);CAN_Data_Byte[7] = this.byte(7);  @sysvar::CAN_Frame::sysvar_Test_Signal_4 = Rx_CAN_Frame_Data_Buff_to_CAN_Signal_Hex(6,2,8);@sysvar::CAN_Frame::sysvar_Test_Signal_5 = Rx_CAN_Frame_Data_Buff_to_CAN_Signal_Hex(10,12,8);@sysvar::CAN_Frame::sysvar_Test_Signal_6 = Rx_CAN_Frame_Data_Buff_to_CAN_Signal_Hex(56,2,8);
}word Rx_CAN_Frame_Data_Buff_to_CAN_Signal_Hex(
byte signal_start_bit,    /**Rang:0~(8 * 8 - 1)**/
byte signal_length,       /**Rang:1~64**/
byte frame_length)        /**Rang:1~8**/
{byte i = 0;byte byte_num = 0;byte low_data_byte = 0;byte high_data_byte = 0;byte low_data_byte_position = 0;byte high_data_byte_position = 0;byte remaining_bit_number = 0;byte remaining_byte_number = 0;	word can_signal_mask = 0;Signal_State = 0;Signal_Value = 0;if(frame_length == 0)            /**DLC of CAN frame is 0**/{Signal_State = 1;}else if(frame_length > 8)         /**DLC of CAN frame is out of range**/{Signal_State = 2;}else if((signal_length > (frame_length * 8))         /**Invalid signal_length: The length of the signal is not in the range of DLC**/|| (signal_length > 16)          /**Invalid signal_length: The length of the signal exceeds the maximum Unsigned of C language**/|| (signal_length == 0)        /**Invalid signal_length: The length of the signal is not equal to 0**/				){Signal_State = 3;}else{/**Byte Order: Motorola**/{/**Bytes used by CAN signal,Byte[0] is low byte,Byte[frame_length] is high byte**/high_data_byte = signal_start_bit / 8;	if((high_data_byte + 1) > frame_length)        /** CAN signal is not in the range of DLC**/{Signal_State = 2;}else{for(i = 0;i < signal_length;i++)  /**Generate signal mask according to signal length**/{can_signal_mask |= 0x0000000000000001 << i;}high_data_byte_position = signal_start_bit % 8;  /**Distance from the lowest bit of each byte**//**CAN Signal is in a bytes**/if(signal_length <= (8 - high_data_byte_position)){				Signal_Value = (((word)CAN_Data_Byte[high_data_byte]) >> high_data_byte_position) & can_signal_mask;					}/**CAN Signal in multiple bytes**/else{			remaining_bit_number = signal_length - (8 - high_data_byte_position); // 剩余bit = 12 - (8 - 2) = 6if((remaining_bit_number % 8) != 0)     {remaining_byte_number = remaining_bit_number / 8 + 1;          // remaining_byte_number = 1}else{remaining_byte_number = remaining_bit_number / 8;}for(byte_num = high_data_byte;byte_num >= (high_data_byte - remaining_byte_number);byte_num--)  // high_data_byte = 2  remaining_byte_number = 1{						if(byte_num == high_data_byte){Signal_Value |= ((word)CAN_Data_Byte[high_data_byte]) >> high_data_byte_position;}else{Signal_Value |= ((word)CAN_Data_Byte[byte_num]) << ((high_data_byte - byte_num - 1) * 8 + (8 - high_data_byte_position));}		if(byte_num == 0)	{break;}						}Signal_Value = Signal_Value & can_signal_mask;									}			}				}}return Signal_Value;
}
/** 使用该Demo需注明出处,以表对作者的尊重 **/
/** 版权归CSDN博客“汽车电子助手”所有,https://blog.csdn.net/qfmzhu **/

3.2.2 系统变量定义

3.2.3 测试结果

使用的DBC

CAN DBC中信号与系统变量的解析结果一致:

结尾

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

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

相关内容

热门资讯

格式化 恢复目录-格式化恢复目... 哎呀,说到“格式化恢复目录”,我这心里就五味杂陈啊!想当年,电脑出问题,目录乱七八糟,真是让人头疼。...
android手机模型-安卓手... 哎呀,说到安卓手机模型,我这心里就五味杂陈啊!你知道吗,每次看到那些新出的手机模型,我这小心脏就扑通...
完全性卒中:生活的急刹车,我们... 完全性卒中,这个词听起来就像是从天而降的噩梦,不是吗?想象一下,就在你以为一切如常,生活按部就班的日...
linux下制作u盘启动盘-在... 嘿,各位小伙伴们,今天咱们来点不一样的!你是不是还在为怎么在Linux下制作U盘启动盘而头疼?别担心...
帝国cms 新闻触屏模板-帝国... 哎呀呀,说到这个帝国CMS的新闻触屏模板,我简直要跳起来了!这可不是一般的模板,它是新闻界的小鲜肉,...
windows xp sp3日... 哎呀,说到WindowsXPSP3的日文补丁,我这心里就五味杂陈啊!想当年,这可是我们电脑小白的救星...
雨林xp sp3 序列号-雨林... 哎呀,说到雨林XPSP3序列号,我这心里就激动得不得了!你知道吗,每次看到那些闪闪发光的数字和字母组...
苹果wwdc ppt-苹果 W... 哎呀,说到苹果的WWDC,那真是让人激动得不得了!每次看到那些闪闪发光的PPT,我就感觉自己像是走进...
杭州花圃的老种绿云:传承百年的... 哎呀,说到杭州的花圃啊,我这心肝儿就扑通扑通跳得厉害!特别是那老种绿云,真是让人一见倾心。这花啊,不...
中央15音乐频道节目表-中央 ... 哟,说到中央15音乐频道,那可是我每天的精神食粮啊!从早到晚,这个频道的节目表简直就是一场不停歇的音...
win8系统cad-Win8 ... 哎呀呀,说到这个Win8系统上用CAD,我这心里啊,五味杂陈!你懂的,CAD这玩意儿,对我们这些搞设...
svchostexe内存错误-... 哎呀,说到这个“svchost.exe”内存错误,真是让人头疼得不得了!每次一打开电脑,它就开始闹小...
克罗恩不治能活多久-克罗恩病虽... 克罗恩病,这个名字听起来就让人心生畏惧。我不是医生,也不是研究员,我只是一个经历过克罗恩病痛苦的人。...
神秘的 statreport.... 哎呀,说到这个statreport.dll,我就觉得它有点神秘兮兮的。每次打开电脑,它就在那里,默默...
格式化手机能恢复吗-手机格式化... 哎呀呀,真是急死人了!手机突然就格式化了,里面的照片、聊天记录、那些珍贵的回忆,一下子全都不见了!这...
中医治疗胃溃疡:整体调理与平衡... 哎呀,说到胃溃疡,这可不是什么小病啊!每次胃痛起来,简直就像是有个小人在你肚子里打鼓,痛得你直冒冷汗...
win10专业版u盘安装-Wi... 哎呀,说到这个Win10专业版的U盘安装,真是让我又爱又恨啊!你想想,每次电脑卡得像乌龟爬,心里那个...
笔记本用u盘安装win7系统-... 嘿,大家好!今天我要和大家分享一个超实用的小技巧——用U盘给笔记本安装Win7系统。是不是觉得听起来...
libeay32dll下载 微... 哎呀,今天真是倒霉透顶了!想打开电脑玩游戏,结果蹦出来一个大大的错误提示,说什么“找不到Libeay...
超凡蜘蛛侠2反派-超凡蜘蛛侠 ... 哟,大家好,我今天得好好聊聊《超凡蜘蛛侠2》里那些让人又爱又恨的反派角色。这部电影,不吹不黑,反派们...