首頁 > 軟體

C/C++關於實現CAN訊號的獲取方法

2023-02-05 14:02:05

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 %dn", cansingal.lsb_pos, cansingal.msb_pos);
	printf("%02x %02x %02x %02xn", 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 %dn", 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 %dn", c, d);
	cansingal.can_data_ptr[c] = cansingal.can_data_ptr[c] & lsbmask[d];
	printf("%02x %02x %02x %02xn", 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 %02xn", 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("%dn", 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 %pn", a, a+1, a+2);
    for(i = 0; i < n; i++){
        printf("%d %d %dn", 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訊號的獲取方法的文章就介紹到這了,更多相關C++ CAN訊號內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com