網站首頁 編程語言 正文
CAN基礎知識
標準的CAN 數據為8字節,即64位,但是CAN FD的最大數據可為64字節,為512位,其中的幀ID分為標準幀和擴展幀,其中用11位標準幀,用29位表示擴展幀。
CAN 信號
信號具體指的是CAN數據的多少位到多少位間代表一個具體的信號,如5位到16位表示車輛的行駛速度,即完整的CAN數據可以表示多個信號。
can信號獲取:
#include <iostream>
#include <array>
unsigned char msbmask[] = {
0xFF, 0xFE, 0xFC, 0xF8,
0xF0, 0xE0, 0xC0, 0x80
};
unsigned char lsbmask[] = {
0x01, 0x03, 0x07, 0x0F,
0x1F, 0x3F, 0x7F, 0xFF
};
#define BITSET(p,n) ((p) |= (1u <<(n)))
#define BITCLR(p,n) ((p) &= ~(1u <<(n)))
#define BITGET(i,n) ((i) & (1u << (n)))
typedef struct {
unsigned char* can_data_ptr;
int len;
int msb_pos;
int lsb_pos;
}can_signal;
static can_signal cansingal;
int can_data_assignment(unsigned char* candata, int msbpos, int lsbpos, int lens)
{
cansingal.can_data_ptr = (unsigned char*)malloc(lens);
memcpy((void *)cansingal.can_data_ptr, (const void *)candata,lens);
cansingal.len = lens;
cansingal.msb_pos = msbpos;
cansingal.lsb_pos = lsbpos;
return 0;
}
unsigned int can_data_transfer_signal()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
unsigned int singal = 0;
printf("%d %d\n", cansingal.lsb_pos, cansingal.msb_pos);
printf("%02x %02x %02x %02x\n", cansingal.can_data_ptr[0], cansingal.can_data_ptr[1], cansingal.can_data_ptr[2], cansingal.can_data_ptr[3]);
a = cansingal.lsb_pos / 8;
b = cansingal.lsb_pos % 8;
printf("a %d b %d\n", a, b);
cansingal.can_data_ptr[a] = cansingal.can_data_ptr[a] & msbmask[b];
c= cansingal.msb_pos / 8;
d = cansingal.msb_pos % 8;
printf("c %d d %d\n", c, d);
cansingal.can_data_ptr[c] = cansingal.can_data_ptr[c] & lsbmask[d];
printf("%02x %02x %02x %02x\n", cansingal.can_data_ptr[0], cansingal.can_data_ptr[1], cansingal.can_data_ptr[2], cansingal.can_data_ptr[3]);
for (int i = cansingal.lsb_pos, j = 0; i <= cansingal.msb_pos; ++i, ++j)
{
a = i / 8;
b = i % 8;
if ( BITGET(cansingal.can_data_ptr[a], b) )
{
BITSET(singal, j);
}
else
{
BITCLR(singal,j);
}
}
return singal;
}
void can_data_free(void)
{
free(cansingal.can_data_ptr);
cansingal.len = 0;
cansingal.lsb_pos = 0;
cansingal.msb_pos = 0;
return;
}
int main(int argc, char* argv[])
{
unsigned char candata[4] = { 0x44, 0xFE, 0x23, 0x81};
printf("%02x %02x %02x %02x\n", candata[0], candata[1], candata[2], candata[3]);
can_data_assignment(candata,31,14,4);
unsigned int c = can_data_transfer_signal();
can_data_free();
printf("%d\n", c);
system("pause");
return 0;
}
如上圖,can數據的其中4字節為0x44,0xFE,0x23,0x81, 分別對應0到32位的數據,現在獲取14位到31位的數據,形成具體的信號值。
運行結果:
C語言涉及到知識
位操作、指針與數組的操作、MSB LSB的表索引。
數組與指針關系:
指針操作 +1 即 p + 1是指向下一位的地址,若p指向的類型為int類型,則p+1 指向下一個int類型數據的地址,若p指向的是個結構體,則p+1指向相對應結構體下一個元素的地址。
其中p[i] = *(p+i)
#include <stdio.h>
int main(int argc, char *argv[]){
int a[] = {1, 3, 5, 7, 9};
int *p, i, n;
p = a;
n = sizeof(a) / sizeof(int);
printf("%p %p %p\n", a, a+1, a+2);
for(i = 0; i < n; i++){
printf("%d %d %d\n", a[i], *(p+i), *(a+i), p[i]);
}
puts("");
return 0;
}
//打印出來的結果如下
0xbf92c324 0xbf92c328 0xbf92c32c
1 1 1
3 3 3
5 5 5
7 7 7
9 9 9
原文鏈接:https://blog.csdn.net/wanglei_11/article/details/128432066
相關推薦
- 2022-11-07 React?Hook父組件如何獲取子組件的數據/函數_React
- 2024-03-13 TypeError: Cannot read property ‘get‘ of undefined
- 2024-02-26 Cannot execute binary file 之原因
- 2022-04-27 C語言模擬實現密碼輸入的示例代碼_C 語言
- 2022-04-12 ASP動態include文件_ASP基礎
- 2022-10-10 matplotlib中plt.hist()參數解釋及應用實例_python
- 2022-08-26 Jquery實現下拉菜單案例_jquery
- 2023-12-06 Warn: Could not find @TableId
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支