CAN信号的c语言解析代码
CAN数据帧的格式,比特位排列格式:
字节|7 6 5 4 3 2 1 0 (字节比特位)
—–|—————————
0 |7 6 5 4 3 2 1 0
1 |15 14 13 12 11 10 9 8
2 |23 22 21 20 19 18 17 16
3 |31 30 29 28 27 26 25 24
4 |39 38 37 36 35 34 33 32
5 |47 46 45 44 43 42 41 40
6 |55 54 53 52 51 50 49 48
7 |63 62 61 60 59 58 57 56
注意:如果是摩托罗拉格式,比如从第10比特开始的共9比特,占用的就是0,1,2,10,11,12,13,14,15这9个比特,计算时第2比特为高位msb, 第10比特为低位lsb。
如果是Intel格式,比如从第10比特开始的共9比特,则占用的就是10,11,12,13,14,15,16,17,18这9个比特,计算时第18比特为高位msb,第10比特为低位lsb。
参数: motoOrIntel: 1为摩托罗拉格式,0为intel格式。
canData: 输入的can数据,一般为8字节或64字节。
lsb:为本信号的最低位。
bitNum:为本信号的比特数。
返回值: 异常返回INT_MAX,否则返回计算后的值。
解析函数:
static unsigned int CanSignalParse(int motoOrIntel, unsigned char *canData, int lsb, int bitNum){
unsigned int value = 0;
int dataIdx = lsb / 8;
int byteRightPos = lsb % 8;
int byteLeftPos;
unsigned char mask;
if(bitNum > 8 – byteRightPos){
byteLeftPos = 7;
value = (canData[dataIdx] >> byteRightPos);
}else{
byteLeftPos = bitNum + byteRightPos – 1;
mask = 0xFF << (7-byteLeftPos);
mask = mask >> (7-byteLeftPos);
mask = mask >> byteRightPos;
value = (canData[dataIdx] >> byteRightPos) & mask;
return value;
}
bitNum -= (8-byteRightPos);
for(int loop=1; bitNum >0; loop++){
if(motoOrIntel == 1){ //motorola style
dataIdx -= 1;
if(dataIdx < 0){
qDebug(“CanSignalParse: ERROR, dataIdx < 0.\n”);
return INT_MAX;
}
}else{ //intel style
dataIdx += 1;
if(dataIdx > 7){
qDebug(“CanSignalParse: ERROR, dataIdx > 7.\n”);
return INT_MAX;
}
}
if(bitNum > 8){
byteLeftPos = 7;
value += (canData[dataIdx] << (loop*8-byteRightPos));
bitNum -= 8;
}else{
byteLeftPos = bitNum – 1;
mask = 0xFF << (7-byteLeftPos);
mask = mask >> (7-byteLeftPos);
value += ((canData[dataIdx] & mask) << (loop*8-byteRightPos));
return value;
}
}
return value;
}
测试代码:
unsigned int canInt;
unsigned char can[8] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
canInt = CanSignalParse(1, can, 8, 16);
qDebug(“SettingsItf::SettingsItf(): 8,16: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 0, 1);
qDebug(“SettingsItf::SettingsItf(): 0,1: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 7, 1);
qDebug(“SettingsItf::SettingsItf(): 7,1: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 8, 1);
qDebug(“SettingsItf::SettingsItf(): 8,1: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 15, 1);
qDebug(“SettingsItf::SettingsItf(): 15,1: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 56, 1);
qDebug(“SettingsItf::SettingsItf(): 56,1: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 63, 1);
qDebug(“SettingsItf::SettingsItf(): 63,1: canInt: %u\n”, canInt);
qDebug(“=================\n”);
canInt = CanSignalParse(1, can, 0, 4);
qDebug(“SettingsItf::SettingsItf(): 0,4: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 2, 4);
qDebug(“SettingsItf::SettingsItf(): 2,4: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 4, 4);
qDebug(“SettingsItf::SettingsItf(): 4,4: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 56, 4);
qDebug(“SettingsItf::SettingsItf(): 56,4: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 58, 4);
qDebug(“SettingsItf::SettingsItf(): 58,4: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 60, 4);
qDebug(“SettingsItf::SettingsItf(): 60,4: canInt: %u\n”, canInt);
qDebug(“=================\n”);
canInt = CanSignalParse(1, can, 56, 16);
qDebug(“SettingsItf::SettingsItf(): 56,16: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 48, 16);
qDebug(“SettingsItf::SettingsItf(): 48,16: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 10, 8);
qDebug(“SettingsItf::SettingsItf(): 10,8: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 58, 8);
qDebug(“SettingsItf::SettingsItf(): 58,8: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 10, 14);
qDebug(“SettingsItf::SettingsItf(): 10,14: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 10, 7);
qDebug(“SettingsItf::SettingsItf(): 10,7: canInt: %u\n”, canInt);
qDebug(“=================\n”);
canInt = CanSignalParse(1, can, 16, 24);
qDebug(“SettingsItf::SettingsItf(): 16,24: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 56, 24);
qDebug(“SettingsItf::SettingsItf(): 56,24: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 18, 17);
qDebug(“SettingsItf::SettingsItf(): 18,17: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 58, 17);
qDebug(“SettingsItf::SettingsItf(): 58,17: canInt: %u\n”, canInt);
qDebug(“=================\n”);
canInt = CanSignalParse(1, can, 24, 32);
qDebug(“SettingsItf::SettingsItf(): 24,32: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 56, 32);
qDebug(“SettingsItf::SettingsItf(): 56,32: canInt: %u\n”, canInt);
canInt = CanSignalParse(1, can, 34, 28);
qDebug(“SettingsItf::SettingsItf(): 34,28: canInt: %u\n”, canInt);
qDebug(“=================\n”);
qDebug(“=================\n”);
canInt = CanSignalParse(0, can, 0, 16);
qDebug(“SettingsItf::SettingsItf(): 0,16: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 0, 1);
qDebug(“SettingsItf::SettingsItf(): 0,1: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 7, 1);
qDebug(“SettingsItf::SettingsItf(): 7,1: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 8, 1);
qDebug(“SettingsItf::SettingsItf(): 8,1: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 15, 1);
qDebug(“SettingsItf::SettingsItf(): 15,1: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 56, 1);
qDebug(“SettingsItf::SettingsItf(): 56,1: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 63, 1);
qDebug(“SettingsItf::SettingsItf(): 63,1: canInt: %u\n”, canInt);
qDebug(“=================\n”);
canInt = CanSignalParse(0, can, 0, 4);
qDebug(“SettingsItf::SettingsItf(): 0,4: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 2, 4);
qDebug(“SettingsItf::SettingsItf(): 2,4: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 4, 4);
qDebug(“SettingsItf::SettingsItf(): 4,4: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 56, 4);
qDebug(“SettingsItf::SettingsItf(): 56,4: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 58, 4);
qDebug(“SettingsItf::SettingsItf(): 58,4: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 60, 4);
qDebug(“SettingsItf::SettingsItf(): 60,4: canInt: %u\n”, canInt);
qDebug(“=================\n”);
canInt = CanSignalParse(0, can, 48, 16);
qDebug(“SettingsItf::SettingsItf(): 48,16: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 8, 16);
qDebug(“SettingsItf::SettingsItf(): 8,16: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 10, 8);
qDebug(“SettingsItf::SettingsItf(): 10,8: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 50, 8);
qDebug(“SettingsItf::SettingsItf(): 50,8: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 10, 14);
qDebug(“SettingsItf::SettingsItf(): 10,14: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 10, 7);
qDebug(“SettingsItf::SettingsItf(): 10,7: canInt: %u\n”, canInt);
qDebug(“=================\n”);
canInt = CanSignalParse(0, can, 0, 24);
qDebug(“SettingsItf::SettingsItf(): 0,24: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 40, 24);
qDebug(“SettingsItf::SettingsItf(): 40,24: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 18, 17);
qDebug(“SettingsItf::SettingsItf(): 18,17: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 42, 17);
qDebug(“SettingsItf::SettingsItf(): 42,17: canInt: %u\n”, canInt);
qDebug(“=================\n”);
canInt = CanSignalParse(0, can, 0, 32);
qDebug(“SettingsItf::SettingsItf(): 0,32: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 32, 32);
qDebug(“SettingsItf::SettingsItf(): 32,32: canInt: %u\n”, canInt);
canInt = CanSignalParse(0, can, 18, 28);
qDebug(“SettingsItf::SettingsItf(): 18,28: canInt: %u\n”, canInt);
qDebug(“=================\n”);