您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“C/C++如何獲取CAN信號(hào)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“C/C++如何獲取CAN信號(hào)”吧!
標(biāo)準(zhǔn)的CAN 數(shù)據(jù)為8字節(jié),即64位,但是CAN FD的最大數(shù)據(jù)可為64字節(jié),為512位,其中的幀ID分為標(biāo)準(zhǔn)幀和擴(kuò)展幀,其中用11位標(biāo)準(zhǔn)幀,用29位表示擴(kuò)展幀。
信號(hào)具體指的是CAN數(shù)據(jù)的多少位到多少位間代表一個(gè)具體的信號(hào),如5位到16位表示車輛的行駛速度,即完整的CAN數(shù)據(jù)可以表示多個(gè)信號(hào)。
can信號(hào)獲取:
#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數(shù)據(jù)的其中4字節(jié)為0x44,0xFE,0x23,0x81, 分別對(duì)應(yīng)0到32位的數(shù)據(jù),現(xiàn)在獲取14位到31位的數(shù)據(jù),形成具體的信號(hào)值。
運(yùn)行結(jié)果:
位操作、指針與數(shù)組的操作、MSB LSB的表索引。
數(shù)組與指針關(guān)系:
指針操作 +1 即 p + 1是指向下一位的地址,若p指向的類型為int類型,則p+1 指向下一個(gè)int類型數(shù)據(jù)的地址,若p指向的是個(gè)結(jié)構(gòu)體,則p+1指向相對(duì)應(yīng)結(jié)構(gòu)體下一個(gè)元素的地址。
其中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; }
//打印出來的結(jié)果如下
0xbf92c324 0xbf92c328 0xbf92c32c
1 1 1
3 3 3
5 5 5
7 7 7
9 9 9
到此,相信大家對(duì)“C/C++如何獲取CAN信號(hào)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。