一、libusb 下载
在官网http://libusb.info/可以下载到
二、libusb的安装
1 解压
tar -jxvf libusb-1.0.22.tar.bz2
执行命令,你会发现执行报错,configure: error: udev support requested but libudev header not installed
解决办法:执行 ./configure 命令,注意该命令可以选择编译平台,编译的库版本也即运行平台,编译工具链,安装路径等,所以不是默认配置的用户需要在此项自行配置,常见配置选项有:
--build=i686-linux指明软件的编译平台
--host=arm-linux 指明软件的运行平台
--prefix 指明软件的安装目录
所以,如果在本机PC上使用,那么需要进行如下配置:
综上,命令是: ./configure--build=x86_64-linux --disable-udev
2.make install
三 测试
static libusb_device_handle *dev_handle = NULL;
unsigned char find_dev() { int ret; ret = libusb_init(NULL); if(ret < 0) { fprintf(stderr, "failed to initialise libusb\n"); return 1; } dev_handle = libusb_open_device_with_vid_pid(NULL, 0x1992, 0x0668); if(dev_handle == NULL){ perror("Cannot open device\n"); }else{ printf("Device Opened\n"); } if(libusb_kernel_driver_active(dev_handle, 0) == 1) { printf("Kernel Driver Active\n"); if(libusb_detach_kernel_driver(dev_handle, 0) == 0){ printf("Kernel Driver Detached!\n"); } } ret = libusb_claim_interface(dev_handle, 0); if(ret < 0) { perror("Cannot Claim Interface\n"); return 1; } }
ret = libusb_bulk_transfer(dev_handle,0x02, (unsigned char *)&l_PrepareMeasData, pmsize,&transferred, 0);//发送命令
printf("ret=%d transferred=%d\r\n",ret,transferred);
if(ret==0 && transferred==pmsize)
{
printf("write Successful!\n");
}
else{
printf("write error!\n");
}
ret = libusb_bulk_transfer(dev_handle, 0x86, rcvPreRcv, 6, &transferred, 0);//接收命令
if (ret != 0) {
printf("failed to read \n");
}
编译:
加上:-I/usr/local/include/libusb-1.0/ -L/usr/local/lib -lusb-1.0 -lrt
-lrt 不加会报错。