您好,登錄后才能下訂單哦!
本篇內容主要講解“C/C++如何獲取CAN信號”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C/C++如何獲取CAN信號”吧!
標準的CAN 數據為8字節,即64位,但是CAN FD的最大數據可為64字節,為512位,其中的幀ID分為標準幀和擴展幀,其中用11位標準幀,用29位表示擴展幀。
信號具體指的是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位的數據,形成具體的信號值。
運行結果:
位操作、指針與數組的操作、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
到此,相信大家對“C/C++如何獲取CAN信號”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。