libusb学习笔记1——libusb的安装
来源:互联网 发布:淘宝卖家快递单号打印 编辑:程序博客网 时间:2024/06/08 06:33
libusb的用处蛮多的,libusb从入门开始学起,也是作为学习开源库的一点经验。
libusb的安装网络上大把的,这里为了先熟悉库,采用了偷懒的办法:直接apt-get install .
直接在终端输入:
sudo apt-get install libusb-dev
sudo apt-get install libusb-1.0-0-dev
完成后,新建一个C文件:
#include <unistd.h>#include <stdio.h>#include <libusb-1.0/libusb.h>// First, use "lsusb" see vid and pid.// there is my printer(hp deskjet 1010) vid and pid.#define VID 0x05e3#define PID 0x0723static int device_satus(libusb_device_handle *hd){int interface = 0;unsigned char byte;libusb_control_transfer(hd, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE,LIBUSB_REQUEST_CLEAR_FEATURE,0,interface,&byte, 1, 5000);printf("status:0x%x\n", byte);/** * byte * normal:0x18 * other :0x10 */return 0;}int main() {libusb_device **devs; //pointer to pointer of device, used to retrieve a list of deviceslibusb_device_handle *dev_handle; //a device handlelibusb_context *ctx = NULL; //a libusb sessionint r; //for return valuesssize_t cnt; //holding number of devices in listr = libusb_init(&ctx); //initialize the library for the session we just declaredif(r < 0) {perror("Init Error\n"); //there was an errorreturn 1;}libusb_set_debug(ctx, LIBUSB_LOG_LEVEL_INFO); //set verbosity level to 3, as suggested in the documentationcnt = libusb_get_device_list(ctx, &devs); //get the list of devicesif(cnt < 0) {perror("Get Device Error\n"); //there was an errorreturn 1;}printf("%d Devices in list.\n", cnt);dev_handle = libusb_open_device_with_vid_pid(ctx, VID, PID); //these are vendorID and productID I found for my usb deviceif(dev_handle == NULL)perror("Cannot open device\n");elseprintf("Device Opened\n");libusb_free_device_list(devs, 1); //free the list, unref the devices in itif(libusb_kernel_driver_active(dev_handle, 0) == 1) { //find out if kernel driver is attachedprintf("Kernel Driver Active\n");if(libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach itprintf("Kernel Driver Detached!\n");}r = libusb_claim_interface(dev_handle, 0); //claim interface 0 (the first) of device (mine had jsut 1)if(r < 0) {perror("Cannot Claim Interface\n");return 1;}printf("Claimed Interface\n");device_satus(dev_handle);libusb_close(dev_handle); //close the device we openedlibusb_exit(ctx); //needs to be called to end thereturn 0;}
注意上述代码中的VID和PID分别是制造商和产品ID:可以通过
cd /sys/kernel/debugcat usb/devices
Ubuntu上输出如下:
T: Bus=01 Lev=01 Prnt=01 Port=01 Cnt=02 Dev#= 7 Spd=480 MxCh= 0D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1P: Vendor=05e3 ProdID=0723 Rev=94.51S: Manufacturer=Generic S: Product=USB StorageS: SerialNumber=000000009451C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=500mAI:* If#= 0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storageE: Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0msE: Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms第三行的P:
Vendor就是VID, ProdID就是PID
P: Vendor=05e3 ProdID=0723
修正代码中的VID和PID就可以编译了。
注意编译的时候需要链接库 -lusb-1.0:
gcc usb.c -lusb-1.0
0 0
- libusb学习笔记1——libusb的安装
- libusb学习笔记
- libusb的安装版本
- libusb学习-1
- 安装libusb
- LIBUSB
- libUSB
- libusb
- libusb
- libusb
- libusb的安装文档翻译
- libusb-win32学习笔记(二)
- linux下libusb的安装与测试
- linux下libusb的安装与测试
- linux下libusb的安装与测试
- 使用libusb库笔记
- 为ubuntu 安装libusb
- libusb学习-2
- uvc 协议分析
- 数据类型
- Maven常用插件介绍
- 框架搭建之CommonTitle
- 最优贸易-tarjan+拓扑更新
- libusb学习笔记1——libusb的安装
- 2017.7.22模拟赛被虐记录
- 利用Digester把XML转换为Java对象
- iOS字符串转数组,数组转字符串
- 有关xml中的xmlns
- 布隆过滤器
- 彻底解决Dynamic Web Module 3.0 or 2.5 requires Java 1.6 or newer.
- Web服务请求异步化
- 苹果手机,电话号码(类似数字)显示为蓝色