1. USBCAN分析仪国内现状
这是目前国内市场上的USBCAN分析仪现状。
2. 创芯科技产品
????????创芯科技的这个红色盒子是我比较下来综合性价比最高的。同时支持windows和linux的设备只要320元左右。你既可以用可视化界面发送/接收报文,也可以二次开发,设置复杂的收发以及数据处理的逻辑。可视化界面操作很简单,我就不赘述了,主要谈谈怎么基于linux二次开发。
2.1 设备自测
设备拿到时最好进行自测,判断设备是否可用。
自测时设备线路连接如下:
PC上virtualbox安装ubuntu虚拟机的USB接口选择
执行自测用例
root@zyy-VirtualBox:/home/zyy/controlcan# ls
controlcan.h hello_cpp libcontrolcan.so main_bak.cpp main.cpp Makefile
root@zyy-VirtualBox:/home/zyy/controlcan# rm -r hello_cpp
root@zyy-VirtualBox:/home/zyy/controlcan# make clean
rm -f *.o hello
root@zyy-VirtualBox:/home/zyy/controlcan# make
g++ -o hello_cpp main.cpp /home/zyy/controlcan/libcontrolcan.so -lpthread
root@zyy-VirtualBox:/home/zyy/controlcan1# ./hello_cpp
>>this is hello !
>>USBCAN DEVICE NUM:1 PCS
Device:0
>>Get VCI_ReadBoardInfo success!
>>Serial_Num:31F100016D6
>>hw_Type:CAN-Linux
>>Firmware Version:V3.40
>>
>>
>>
>>open deivce success!
>>Get VCI_ReadBoardInfo success!
>>Serial_Num:31F100016D6
>>hw_Type:CAN-Linux
>>Firmware Version:V3.40
Index:0000 CAN1 TX ID:0x00000000 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07
Index:0001 CAN2 TX ID:0x00000001 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07
Index:0002 CAN2 RX ID:0x00000000 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07 TimeStamp:0x0009D81E
Index:0003 CAN1 TX ID:0x00000002 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07
Index:0004 CAN1 RX ID:0x00000001 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07 TimeStamp:0x0009D85C
Index:0005 CAN2 TX ID:0x00000003 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07
Index:0006 CAN2 RX ID:0x00000002 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07 TimeStamp:0x0009D890
Index:0007 CAN1 TX ID:0x00000004 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07
Index:0008 CAN1 RX ID:0x00000003 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07 TimeStamp:0x0009D8B6
Index:0009 CAN2 TX ID:0x00000005 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07
Index:0010 CAN2 RX ID:0x00000004 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07 TimeStamp:0x0009D8E8
Index:0011 CAN1 TX ID:0x00000006 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07
Index:0012 CAN1 RX ID:0x00000005 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07 TimeStamp:0x0009D912
Index:0013 CAN2 TX ID:0x00000007 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07
Index:0014 CAN2 RX ID:0x00000006 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07 TimeStamp:0x0009D93D
Index:0015 CAN1 TX ID:0x00000008 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07
Index:0016 CAN1 RX ID:0x00000007 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07 TimeStamp:0x0009D96F
Index:0017 CAN2 TX ID:0x00000009 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07
Index:0018 CAN2 RX ID:0x00000008 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07 TimeStamp:0x0009D99F
Index:0019 CAN1 RX ID:0x00000009 Extend Data DLC:0x08 data:0x 00 01 02 03 04 05 06 07 TimeStamp:0x0009D9DC
run thread exit
2.2 基于实际场景的二次开发
2.2.1 一种测试场景
将目标ECU的CAN引脚和USBCAN分析仪相连, 目标ECU的电源引脚接到相应电压的供电电源上。
本测试场景分三步
1. 进入扩展会话模式,并获得正面响应。
2. tester向ECU请求seed,并获得seed。
3. 根据seed计算key, tester向ECU发送key,并获得正面响应。
2.2.2 编写代码以及调试
编写代码如下:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include "controlcan.h"
#include <ctime>
#include <cstdlib>
#include "unistd.h"
#define NO_RES_COUNT_MAX 5
VCI_BOARD_INFO pInfo;//用来获取设备信息。
int count=0;//数据列表中,用来存储列表序号。
VCI_BOARD_INFO pInfo1 [50];
int num=0;
//重要全局变量
int i;
int STEP=0X01;
unsigned int Ecu_DialogId= 0x777;
int Fun_Id = 0x7df;
DWORD seed0 =0;
DWORD seed1 =0;
DWORD seed2 =0;
DWORD seed3 =0;
DWORD seed0_tmp;
DWORD seed1_tmp;
DWORD seed2_tmp;
DWORD seed3_tmp;
DWORD seed;
DWORD key;
int NoResCount=0;
int LogAllRecv =1;
void *receive_func(void* param) //接收线程。
{
int reclen=0;
VCI_CAN_OBJ rec[3000];//接收缓存,设为3000为佳。
int i,j;
int *run=(int*)param;//线程启动,退出控制。
int ind=0;
while((*run)&0x0f)
{
usleep(300000);//延时300ms。
if((reclen=VCI_Receive(VCI_USBCAN2,0,ind,rec,3000,100))>0)//调用接收函数,如果有数据,进行数据处理显示。
{
for(j=0;j<reclen;j++)
{
if(LogAllRecv)
{
printf("Index:%04d ",count);count++;//序号递增
printf("CAN%d RX ID:0x%08X", ind+1, rec[j].ID);//ID
if(rec[j].ExternFlag==0) printf(" Standard ");//帧格式:标准帧
if(rec[j].ExternFlag==1) printf(" Extend ");//帧格式:扩展帧
if(rec[j].RemoteFlag==0) printf(" Data ");//帧类型:数据帧
if(rec[j].RemoteFlag==1) printf(" Remote ");//帧类型:远程帧
printf("DLC:0x%02X",rec[j].DataLen);//帧长度
printf(" data:0x"); //数据
for(i = 0; i < rec[j].DataLen; i++)
{
printf(" %02X", rec[j].Data[i]);
}
printf(" TimeStamp:0x%08X",rec[j].TimeStamp);//时间标识。
printf("\n");
}
if(0 == rec[j].ExternFlag && 0x50 == rec[j].Data[1] && 0x03 == rec[j].Data[2])
{
printf("STEP[0X01] Session extention resp received!\n");
STEP =0x02;
}
else if(0 == rec[j].ExternFlag && 0x67 == rec[j].Data[1] && 0x01 == rec[j].Data[2])
{
printf("STEP[0X07] Ask seed resp received!\n");
seed0 = rec[j].Data[3];
seed1 = rec[j].Data[4];
seed2 = rec[j].Data[5];
seed3 = rec[j].Data[6];
STEP = 0X03;
}
else if(0 == rec[j].ExternFlag && 0x67 == rec[j].Data[1] && 0x02 == rec[j].Data[2])
{
printf("STEP[0X71] Send key resp received!\n");
STEP = 0xFF;
}
}
}
}
printf("run thread exit\n");//退出接收线程
pthread_exit(0);
}
int main()
{
printf(">>this is hello !\r\n");//指示程序已运行
num=VCI_FindUsbDevice2(pInfo1);
printf(">>USBCAN DEVICE NUM:");printf("%d", num);printf(" PCS");printf("\n");
for(int i=0;i<num;i++)
{
printf("Device:");printf("%d", i);printf("\n");
printf(">>Get VCI_ReadBoardInfo success!\n");
printf(">>Serial_Num:%c", pInfo1[i].str_Serial_Num[0]);
printf("%c", pInfo1[i].str_Serial_Num[1]);
printf("%c", pInfo1[i].str_Serial_Num[2]);
printf("%c", pInfo1[i].str_Serial_Num[3]);
printf("%c", pInfo1[i].str_Serial_Num[4]);
printf("%c", pInfo1[i].str_Serial_Num[5]);
printf("%c", pInfo1[i].str_Serial_Num[6]);
printf("%c", pInfo1[i].str_Serial_Num[7]);
printf("%c", pInfo1[i].str_Serial_Num[8]);
printf("%c", pInfo1[i].str_Serial_Num[9]);
printf("%c", pInfo1[i].str_Serial_Num[10]);
printf("%c", pInfo1[i].str_Serial_Num[11]);
printf("%c", pInfo1[i].str_Serial_Num[12]);
printf("%c", pInfo1[i].str_Serial_Num[13]);
printf("%c", pInfo1[i].str_Serial_Num[14]);
printf("%c", pInfo1[i].str_Serial_Num[15]);
printf("%c", pInfo1[i].str_Serial_Num[16]);
printf("%c", pInfo1[i].str_Serial_Num[17]);
printf("%c", pInfo1[i].str_Serial_Num[18]);
printf("%c", pInfo1[i].str_Serial_Num[19]);printf("\n");
printf(">>hw_Type:%c", pInfo1[i].str_hw_Type[0]);
printf("%c", pInfo1[i].str_hw_Type[1]);
printf("%c", pInfo1[i].str_hw_Type[2]);
printf("%c", pInfo1[i].str_hw_Type[3]);
printf("%c", pInfo1[i].str_hw_Type[4]);
printf("%c", pInfo1[i].str_hw_Type[5]);
printf("%c", pInfo1[i].str_hw_Type[6]);
printf("%c", pInfo1[i].str_hw_Type[7]);
printf("%c", pInfo1[i].str_hw_Type[8]);
printf("%c", pInfo1[i].str_hw_Type[9]);printf("\n");
printf(">>Firmware Version:V");
printf("%x", (pInfo1[i].fw_Version&0xF00)>>8);
printf(".");
printf("%x", (pInfo1[i].fw_Version&0xF0)>>4);
printf("%x", pInfo1[i].fw_Version&0xF);
printf("\n");
}
printf(">>\n");
printf(">>\n");
printf(">>\n");
if(VCI_OpenDevice(VCI_USBCAN2,0,0)==1)//打开设备
{
printf(">>open deivce success!\n");//打开设备成功
}
else
{
printf(">>open deivce error!\n");
exit(1);
}
if(VCI_ReadBoardInfo(VCI_USBCAN2,0,&pInfo)==1)//读取设备序列号、版本等信息。
{
printf(">>Get VCI_ReadBoardInfo success!\n");
//printf(" %08X", pInfo.hw_Version);printf("\n");
//printf(" %08X", pInfo.fw_Version);printf("\n");
//printf(" %08X", pInfo.dr_Version);printf("\n");
//printf(" %08X", pInfo.in_Version);printf("\n");
//printf(" %08X", pInfo.irq_Num);printf("\n");
//printf(" %08X", pInfo.can_Num);printf("\n");
printf(">>Serial_Num:%c", pInfo.str_Serial_Num[0]);
printf("%c", pInfo.str_Serial_Num[1]);
printf("%c", pInfo.str_Serial_Num[2]);
printf("%c", pInfo.str_Serial_Num[3]);
printf("%c", pInfo.str_Serial_Num[4]);
printf("%c", pInfo.str_Serial_Num[5]);
printf("%c", pInfo.str_Serial_Num[6]);
printf("%c", pInfo.str_Serial_Num[7]);
printf("%c", pInfo.str_Serial_Num[8]);
printf("%c", pInfo.str_Serial_Num[9]);
printf("%c", pInfo.str_Serial_Num[10]);
printf("%c", pInfo.str_Serial_Num[11]);
printf("%c", pInfo.str_Serial_Num[12]);
printf("%c", pInfo.str_Serial_Num[13]);
printf("%c", pInfo.str_Serial_Num[14]);
printf("%c", pInfo.str_Serial_Num[15]);
printf("%c", pInfo.str_Serial_Num[16]);
printf("%c", pInfo.str_Serial_Num[17]);
printf("%c", pInfo.str_Serial_Num[18]);
printf("%c", pInfo.str_Serial_Num[19]);printf("\n");
printf(">>hw_Type:%c", pInfo.str_hw_Type[0]);
printf("%c", pInfo.str_hw_Type[1]);
printf("%c", pInfo.str_hw_Type[2]);
printf("%c", pInfo.str_hw_Type[3]);
printf("%c", pInfo.str_hw_Type[4]);
printf("%c", pInfo.str_hw_Type[5]);
printf("%c", pInfo.str_hw_Type[6]);
printf("%c", pInfo.str_hw_Type[7]);
printf("%c", pInfo.str_hw_Type[8]);
printf("%c", pInfo.str_hw_Type[9]);printf("\n");
printf(">>Firmware Version:V");
printf("%x", (pInfo.fw_Version&0xF00)>>8);
printf(".");
printf("%x", (pInfo.fw_Version&0xF0)>>4);
printf("%x", pInfo.fw_Version&0xF);
printf("\n");
}
else
{
printf(">>Get VCI_ReadBoardInfo error!\n");
exit(1);
}
//初始化参数,严格参数二次开发函数库说明书。
VCI_INIT_CONFIG config;
config.AccCode=0xeee00000;// 0X777 << 21; AccCode=0 AccMask=0xFFFFFFFF表示都接收
config.AccMask=0x00000000;//AccMask的值0x00000000表示所有位均为相关位
config.Filter=1;//1:接收所有帧 2:只接收标准帧 3:只接收扩展帧
config.Timing0=0x00;/*波特率125 Kbps 0x03 0x1C ||| 波特率500 Kbps 0x00 0x1C*/
config.Timing1=0x1C;
config.Mode=0;//正常模式
if(VCI_InitCAN(VCI_USBCAN2,0,0,&config)!=1)
{
printf(">>Init CAN1 error\n");
VCI_CloseDevice(VCI_USBCAN2,0);
}
if(VCI_StartCAN(VCI_USBCAN2,0,0)!=1)
{
printf(">>Start CAN1 error\n");
VCI_CloseDevice(VCI_USBCAN2,0);
}
//创建接收线程,持续接收数据包
int m_run0=1;
pthread_t threadid;
int ret;
ret=pthread_create(&threadid,NULL,receive_func,&m_run0);
VCI_CAN_OBJ send[1];
while(1)
{
//STEP0x00:还没有收到ECU回复
//step0x01:session extention
//step0x02:ask seed
//step0x03:send key
//step0xFF:所有流程结束
switch(STEP)
{
case 0x01:
//需要发送的帧,结构体设置
send[0].ID= Ecu_DialogId;
send[0].SendType=0;
send[0].RemoteFlag=0;
send[0].ExternFlag=0;
send[0].DataLen=8;
send[0].Data[0] = 0x02;
send[0].Data[1] = 0x10;
send[0].Data[2] = 0x03;
send[0].Data[3] = 0x55;
send[0].Data[4] = 0x55;
send[0].Data[5] = 0x55;
send[0].Data[6] = 0x55;
send[0].Data[7] = 0x55;
if(VCI_Transmit(VCI_USBCAN2, 0, 0, send, 1) == 1)
{
printf("STEP[0X%02x] Session extention SEND SUCCESS!",STEP);
printf("Index:%04d ",count);count++;
printf(" CAN1 TX ID:0x%08X",send[0].ID);
if(send[0].ExternFlag==0) printf(" Standard ");
if(send[0].ExternFlag==1) printf(" Extend ");
if(send[0].RemoteFlag==0) printf(" Data ");
if(send[0].RemoteFlag==1) printf(" Remote ");
printf("DLC:0x%02X",send[0].DataLen);
printf(" data:0x");
for(i=0;i<send[0].DataLen;i++)
{
printf(" %02X",send[0].Data[i]);
}
printf("\n");
}
else
{
printf("STEP[%02x] VCI_Transmit failed!",STEP);
goto SHUTDOWN;
}
STEP = 0x00;
usleep(200000);//延时200ms
break;
case 0x02:
//需要发送的帧,结构体设置
send[0].ID= Ecu_DialogId;
send[0].SendType=0;
send[0].RemoteFlag=0;
send[0].ExternFlag=0;
send[0].DataLen=8;
send[0].Data[0] = 0x02;
send[0].Data[1] = 0x27;
send[0].Data[2] = 0x01;
send[0].Data[3] = 0x55;
send[0].Data[4] = 0x55;
send[0].Data[5] = 0x55;
send[0].Data[6] = 0x55;
send[0].Data[7] = 0x55;
if(VCI_Transmit(VCI_USBCAN2, 0, 0, send, 1) == 1)
{
printf("STEP[0X%02x] ask seed SEND SUCCESS!",STEP);
printf("Index:%04d ",count);count++;
printf(" CAN1 TX ID:0x%08X",send[0].ID);
if(send[0].ExternFlag==0) printf(" Standard ");
if(send[0].ExternFlag==1) printf(" Extend ");
if(send[0].RemoteFlag==0) printf(" Data ");
if(send[0].RemoteFlag==1) printf(" Remote ");
printf("DLC:0x%02X",send[0].DataLen);
printf(" data:0x");
for(i=0;i<send[0].DataLen;i++)
{
printf(" %02X",send[0].Data[i]);
}
printf("\n");
}
else
{
printf("STEP[%02x] VCI_Transmit failed!",STEP);
goto SHUTDOWN;
}
STEP = 0x00;
usleep(300000);//延时300ms
break;
case 0x03:
//需要发送的帧,结构体设置
send[0].ID= Ecu_DialogId;
send[0].SendType=0;
send[0].RemoteFlag=0;
send[0].ExternFlag=0;
send[0].DataLen=8;
seed0_tmp = seed0 << 3*8;
seed1_tmp = seed1 << 2*8;
seed2_tmp = seed2 << 8;
seed3_tmp = seed3;
seed = seed0_tmp + seed1_tmp + seed2_tmp + seed3_tmp;
printf("seed is 0x%08x\n",seed);
key = seed; //根据实际情况设计seed->key的计算方法
printf("key is 0x%08x\n",key);
send[0].Data[0] = 0x06;
send[0].Data[1] = 0x27;
send[0].Data[2] = 0x02;
send[0].Data[3] = (key & 0xff000000) >> 3*8;
send[0].Data[4] = (key & 0x00ff0000) >> 2*8;
send[0].Data[5] = (key & 0x0000ff00) >> 8;
send[0].Data[6] = (key & 0x000000ff);
send[0].Data[7] = 0x55;
if(VCI_Transmit(VCI_USBCAN2, 0, 0, send, 1) == 1)
{
printf("STEP[0X%02x] send key SEND SUCCESS!",STEP);
printf("Index:%04d ",count);count++;
printf(" CAN1 TX ID:0x%08X",send[0].ID);
if(send[0].ExternFlag==0) printf(" Standard ");
if(send[0].ExternFlag==1) printf(" Extend ");
if(send[0].RemoteFlag==0) printf(" Data ");
if(send[0].RemoteFlag==1) printf(" Remote ");
printf("DLC:0x%02X",send[0].DataLen);
printf(" data:0x");
for(i=0;i<send[0].DataLen;i++)
{
printf(" %02X",send[0].Data[i]);
}
printf("\n");
}
else
{
printf("STEP[%02x] VCI_Transmit failed!\n",STEP);
goto SHUTDOWN;
}
STEP = 0x00;
usleep(500000);//延时500ms
break;
case 0x00:
//还没有收到ECU回复
printf("[WAITING RESPONSE]VCI_Receive have not found corresponding ECU response!\n");
if(NoResCount < NO_RES_COUNT_MAX)
{
NoResCount++;
}
else
{
printf("VCI_Receive have not found corresponding ECU response > %d times,test failed!!!",NO_RES_COUNT_MAX);
goto SHUTDOWN;
}
usleep(1000000);//延时1000ms
break;
case 0xFF:
//所有流程结束
printf("VCI_Receive have found all corresponding ECU response!\n test SUCESS!!!\n");
goto SHUTDOWN;
break;
default:
printf("unknown step\n");
goto SHUTDOWN;
}
}// while end
SHUTDOWN:
m_run0=0;//线程关闭指令。
pthread_join(threadid,NULL);//等待线程关闭。
usleep(100000);//延时100ms。
VCI_ResetCAN(VCI_USBCAN2, 0, 0);//复位CAN1通道。
usleep(100000);//延时100ms。
VCI_CloseDevice(VCI_USBCAN2,0);//关闭设备。
//除收发函数外,其它的函数调用前后,最好加个毫秒级的延时,即不影响程序的运行,又可以让USBCAN设备有充分的时间处理指令。
//goto ext;
}
代码执行结果:
root@zyy-VirtualBox:/home/zyy/controlcan# ./hello_cpp
>>this is hello !
>>USBCAN DEVICE NUM:1 PCS
Device:0
>>Get VCI_ReadBoardInfo success!
>>Serial_Num:31F010318A1
>>hw_Type:CAN-Linux
>>Firmware Version:V3.39
>>
>>
>>
>>open deivce success!
>>Get VCI_ReadBoardInfo success!
>>Serial_Num:31F010318A1
>>hw_Type:CAN-Linux
>>Firmware Version:V3.39
STEP[0X01] Session extention SEND SUCCESS!Index:0000 CAN1 TX ID:0x00000777 Standard Data DLC:0x08 data:0x 02 10 03 55 55 55 55 55
Index:0001 CAN1 RX ID:0x0000077F Standard Data DLC:0x08 data:0x 06 50 03 00 32 00 C8 AA TimeStamp:0x000159E3
STEP[0X01] Session extention resp received!
STEP[0X02] ask seed SEND SUCCESS!Index:0008 CAN1 TX ID:0x00000777 Standard Data DLC:0x08 data:0x 02 27 01 55 55 55 55 55
Index:0002 CAN1 RX ID:0x0000077F Standard Data DLC:0x08 data:0x 06 67 01 ee ee ee ee aa TimeStamp:0x0001CADA
STEP[0X02] Ask seed resp received!
STEP[0X03] Send key SEND SUCCESS!Index:0008 CAN1 TX ID:0x00000777 Standard Data DLC:0x08 data:0x 06 27 02 ee ee ee ee 55
Index:0003 CAN1 RX ID:0x0000077F Standard Data DLC:0x08 data:0x 02 67 02 aa aa aa aa aa TimeStamp:0x0001CADA
STEP[0X03] Send key resp received!
VCI_Receive have found all corresponding ECU response!
test SUCESS!!!
3. 总结
? ? ? ? 这款CAN分析仪性价比不错,功能也够强大,如果你想使用,可以参考本博客试试。
|