libusb API入门

我在http://sourceforge.net/projects/libusb/?source=typ_redirect  下载了libusb-1.0.9

libusb API文档:http://libusb.sourceforge.net/api-1.0/modules.html


libusb编译

将源码下载下来后,进入目录执行

./configure --prefix=/usr/local  //最终生成的文件放在/usr/local/的bin、include、lib目录下

make

sudo make install  便产生了如下这些文件

libusb API入门_第1张图片

sudo make uninstall  将这些产生的文件删除掉



libusb API编程

synchronous I/O interface

你只要一个函数就能进行USB传输,但是有不足的地方,

1)当你调用libusb_bulk_transfer,如果他一直没有完成,那么你的应用程序会一直睡在那边,直到他完成并返回。

2)另外一个不足的地方是调用synchronous函数后总是处在那个进程传输中,无法同时操作多个端点或者多个设备,除非你对每一个传输创建一个线程。

3)请求一旦被提交后无法再撤销了。


Devices and interfaces

libusb_device  代表一个usb设备

libusb_device_handle  如果你想在一个device上使用多个interface,那么你应该多次打开设备来得到这个设备上的多个interface


我们使用vendor ID and product ID等其他设备属性来找到特定的设备


基础操作步骤:

1)initialize the library by calling the function libusb_init and creating a session

2)Call the function libusb_get_device_list to get a list of connected devices. This creates an array oflibusb_device containing all usb devices connected to the system.

3)Loop through all these devices and check their options

4)Discover the one and open the device either by libusb_open or libusb_open_device_with_vid_pid(when you know vendor and product id of the device) to open the device

5)Clear the list you got from libusb_get_device_list by using libusb_free_device_list

6)Claim the interface with libusb_claim_interface (requires you to know the interface numbers of device)

7)Do desired I/O

8)Release the device by using libusb_release_interface

9)Close the device you openedbefore, by using libusb_close

10)Close the session by using libusb_exit

如果执行libusb_claim_interface失败,在这种情况下你应该首先判断是否存在设备驱动:libusb_kernel_driver_active,如果存在,那么移除设备的驱动,libusb_detach_kernel_driver


下面是读取mouse的一个简单的小程序generic_mouse.c,帮助你使用libusb API

/*
2014.09.17
g++ generic_mouse.c -o generic_mouse `PKG_CONFIG_PATH=/home/lei_wang/usr_lib/lib/pkgconfig/ pkg-config --cflags --libs libusb-1.0`
sudo ./generic_mouse

the code below is to read mouse data,it works fine.
run the program under sudo, and move the mouse, can see the data print out.
*/
#include <iostream>
#include <stdio.h>
#include <libusb.h>
using namespace std;

int main() {
	libusb_device **devs;          //pointer to pointer of device, used to retrieve a list of devices
	libusb_context *ctx = NULL;    //a libusb session
	int r;                         //for return values
	ssize_t cnt;                   //holding number of devices in list
    r = libusb_init(&ctx);         //initialize a library session
    if(r < 0) {
        cout<<"Init Error "<<r<<endl; //there was an error
        return 1;
    }
    libusb_set_debug(ctx, 3); //set verbosity level to 3, as suggested in the documentation
    cnt = libusb_get_device_list(ctx, &devs); //get the list of devices
	if(cnt < 0) {
		cout<<"Get Device Error"<<endl;   //there was an error
	}

	libusb_device_handle *dev_handle;         //a device handle
	dev_handle = libusb_open_device_with_vid_pid(ctx, 0x0461, 0x4d22); //open mouse
	if(dev_handle == NULL) {
	        cout<<"Cannot open device"<<endl;
	    	libusb_free_device_list(devs, 1); //free the list, unref the devices in it
	    	libusb_exit(ctx);                 //close the session
	    	return 0;
	} else {
		cout<<"Device Opened"<<endl;
		libusb_free_device_list(devs, 1);                     //free the list, unref the devices in it
		if(libusb_kernel_driver_active(dev_handle, 0) == 1) { //find out if kernel driver is attached
			cout<<"Kernel Driver Active"<<endl;
			if(libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it
				cout<<"Kernel Driver Detached!"<<endl;
		}
		r = libusb_claim_interface(dev_handle, 0);            //claim interface 0 (the first) of device (mine had jsut 1)
		if(r < 0) {
			cout<<"Cannot Claim Interface"<<endl;
			return 1;
		}
		cout<<"Claimed Interface"<<endl;
		int size;
		unsigned char datain[1024]="\0";
		for(int i=0;i<300;i++)
		{
			int rr = libusb_interrupt_transfer(dev_handle,
					0x81,
					datain,
					0x0004,
					&size,
					1000);
			std::cout<<"libusb_interrupt_transfer rr: "<<rr<<endl;
			cout<<"size: "<<size<<endl;
			printf("data: ");
			for(int j=0; j<size; j++)
				  printf("%02x ", (unsigned char)(datain[j]));
			printf("\n");
		}

		r = libusb_release_interface(dev_handle, 0); //release the claimed interface
		if(r!=0) {
			cout<<"Cannot Release Interface"<<endl;
			return 1;
		}
                cout<<"Released Interface"<<endl;

                libusb_attach_kernel_driver(dev_handle, 0);
                libusb_close(dev_handle);
                libusb_exit(ctx); //close the session
                return 0;
         }
}

 
 
 
 
 
 
 
 


部分参考:http://www.dreamincode.net/forums/topic/148707-introduction-to-using-libusb-10/

你可能感兴趣的:(libusb API入门)