C/C++关于实现CAN信号的获取方法
水火汪 人气:0CAN基础知识
标准的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
加载全部内容