前言
最近了解了一些socket can的知识点,本文主要介绍如何将数据转换为CAN报文,前提是已经确定CAN的传输协议。
本文使用的CAN报文共有22条,这些报文共用一个can id,每条报文使用序号标志;
报文协议
转换数据程序
char* convert_buf(vector<Point>& probp, vector<Distance>& pdist, vector<int>& plabel)
{
//convert-can-frame.
char buffer[][] = { };
for (int i = ; i < ; i++)
{
double lat_factor = 0.1;
double long_factor = 0.2; unsigned int lat01 = static_cast<unsigned int>(pdist[ * i].x_dis / lat_factor);//unit-2dm-8bits.
unsigned int long01 = static_cast<unsigned int>(pdist[ * i].y_dis / long_factor);//unit-2dm-9bits.
int label01 = plabel[i*]; if (i == )
{
buffer[i][] = (((i+) & 0xFF) << ) + ((long01 & 0x1FF) >> );
buffer[i][] = ((long01 & 0x1FF) << ) + ((lat01 & 0xFF) >> );
buffer[i][] = ((lat01 & 0xFF) << ) + (label01 & 0x03);
break;
} unsigned int lat02 = static_cast<unsigned int>(pdist[ * i + ].x_dis / lat_factor);//unit-2dm-8bits.
unsigned int long02 = static_cast<unsigned int>(pdist[ * i + ].y_dis / long_factor);//unit-2dm-9bits.
int label02 = plabel[ * i + ]; unsigned int lat03 = static_cast<unsigned int>(pdist[ * i + ].x_dis / lat_factor);//unit-2dm-8bits.
unsigned int long03 = static_cast<unsigned int>(pdist[ * i + ].y_dis / long_factor);//unit-2dm-9bits.
int label03 = plabel[ * i + ]; std::cout << "long01 --- " << long01 << "-- long02 --- " << long02 << "--- long03 --- " << long03 << std::endl;
buffer[i][] = (((i + ) & 0xFF) << ) + ((long01 & 0x1FF) >> );
buffer[i][] = ((long01 & 0x1FF) << ) + ((lat01 & 0xFF) >> );
buffer[i][] = ((lat01 & 0xFF) << ) + (label01 & 0x03);
buffer[i][] = ((long02 & 0x1FF) >> );
buffer[i][] = ((long02 & 0x1FF) << ) + ((lat02 & 0xFF) >> );
buffer[i][] = ((lat02 & 0xFF) << ) + ((label02 & 0x03) << ) + ((long03 & 0x1FF) >> );
buffer[i][] = ((long03 & 0x1FF) << ) + ((lat03 & 0xFF) >> );
buffer[i][] = ((lat03 & 0xFF) << ) + ((label03 & 0x03) << ); printf("buffer[0]: --- %x\n", (byte)buffer[i][]);
printf("buffer[1]: --- %x\n", (byte)buffer[i][]);
printf("buffer[2]: --- %x\n", (byte)buffer[i][]);
printf("buffer[3]: --- %x\n", (byte)buffer[i][]);
printf("buffer[4]: --- %x\n", (byte)buffer[i][]);
printf("buffer[5]: --- %x\n", (byte)buffer[i][]);
printf("buffer[6]: --- %x\n", (byte)buffer[i][]);
printf("buffer[7]: --- %x\n", (byte)buffer[i][]); }
cout << "sizeof(buffer): " << sizeof(buffer) << " Byte..." << endl;
return buffer[]; }
注意
1.如何确定CAN协议的格式,特别是信号的范围、精度以及比特位数;
2.浮点型数据如何变换为可以进行逻辑运算和位移操作的数据类型;
3.如何按照报文格式得到每个字节的内容,特别是位移操作;
4.如何将报文内容正确输出方便查看;
5.原始数据可能越界,应该在转换为报文数据之前对原始数据进行越界处理;
6.转换为CAN报文过程中的各个操作符号的优先级;
完