Lv.的博客

libusb获取usb设备的idVendor(vid),idProduct(pid),以及Serial Number

 

发表于2015/6/23 21:55:11  4594人阅读

 

      最近在做关于usb设备的项目,用到了libusb,发现关于这个的函数库的介绍,讲解很少,下面仅仅是简单展示一些基本的使用方法,以备后用。

本人的系统ubuntu,首先

 

sudo apt-get install libusb-dev
sudo apt-get install libusb-1.0-0-dev

安装libusb开发环境,这个函数库的函数基本上定义在libusb-1.0/libusb.h里,因此,使用时需要引用头文件#include <libusb-1.0/libusb.h>,另外,使用gcc编译时需要加上 `pkg-config --libs --cflags libusb-1.0`,例如

 

 

gcc findusb.c -o findusb `pkg-config --libs --cflags libusb-1.0`

下面给出两个函数,获取usb设备的idVendor(vid),idProduct(pid),以及Serial Number

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libusb-1.0/libusb.h>

#define CHAR_MAX_LEN 256
#define BUF_MAX_LEN INT_MAX


struct usb_st{
	char sn[CHAR_MAX_LEN];
	char product[CHAR_MAX_LEN];
	uint16_t pid;
	uint16_t vid;
};


/*
return 0 means don't equal;return 1 means equal.
*/
int chk_serial_number(uint16_t pid,uint16_t vid,char *sn){
	//usb information index
	struct libusb_device_descriptor usb_info;
	//usb device
	struct libusb_device *usb_d=NULL;
	//the handle of the opened usb device
	struct libusb_device_handle *usb_p=NULL;
	//buffer
	char buf[CHAR_MAX_LEN]={0};

	usb_p=libusb_open_device_with_vid_pid(NULL,pid,vid);
	if(usb_p!=0){
		//find information index
		usb_d=libusb_get_device(usb_p);
		if(libusb_get_device_descriptor(usb_d,&usb_info)!=0){
			perror("can't find usb device's information");
			libusb_close(usb_p);
			return 0;
		}
		//find SerialNumber
		libusb_get_string_descriptor_ascii(usb_p,usb_info.iSerialNumber,buf,CHAR_MAX_LEN);
		return (strcmp(buf,sn)==0);
	}else{
		perror("can't find usb device");
		libusb_close(usb_p);
		return 0;
	}
	libusb_close(usb_p);
	return 1;
}


/*
fill the usb devices information list
*/
ssize_t ls_usb_device(struct usb_st *st){
	ssize_t usb_list_len=0,idx=0;
	struct libusb_device **list=NULL;
	struct libusb_device *dev=NULL;
	struct libusb_device_descriptor desc={0};
	struct libusb_device_handle *usb_p=NULL;
	char buf[CHAR_MAX_LEN]={0};

	//get the list
	usb_list_len=libusb_get_device_list(NULL,&list);
	if(usb_list_len==0){
		perror("can't find usb list");
		return 0;
	}
	//access the members of the list
	for(idx=0;idx!=usb_list_len;idx++){
		memset(&desc,sizeof(struct libusb_device_descriptor),0);
		if(libusb_get_device_descriptor(list[idx],&desc)!=0){
			perror("can't find usb list information");
			return 0;
		}
		//copy usb device's information to the st
		libusb_open(list[idx],&usb_p);
		libusb_get_string_descriptor_ascii(usb_p,desc.iProduct,st[idx].product,CHAR_MAX_LEN);
		libusb_get_string_descriptor_ascii(usb_p,desc.iSerialNumber,st[idx].sn,CHAR_MAX_LEN);
		st[idx].pid=desc.idProduct;
		st[idx].vid=desc.idVendor;
		libusb_close(usb_p);
	}
	//return the lenght of list
	return usb_list_len;
}


/*
initialization
*/
void t_init(){
	libusb_init(NULL);
}


/*
when app distory
*/
void t_exit(){
	libusb_exit(NULL);
}


main(){
	struct usb_st st[CHAR_MAX_LEN];
	ssize_t usb_list_len=0;
	int i=0;
	t_init();
	printf("%d",chk_serial_number(0x8829,0x0010,"9053053023054027"));
	usb_list_len=ls_usb_device(st);
	for (i = 0; i < usb_list_len; ++i)
	{
		printf("product:%s\nsn:%s\npid:%x\nvid:%x\n\n",st[i].product,st[i].sn,st[i].pid,st[i].vid);
	}
	t_exit();
}

     首先必须使用libusb_init()初始化,运行结束需要libusb_exit(),这两个函数,一个是chk_serial_number(),传入pid,vid,和序列号,功能是检测当前是否存在传入的pid,vid与序列号相等的usb设备,存在则返回1。

 

     而ls_usb_device将当前系统所有的usb设备信息(pid,vid,序列号)放到st数组里。

     欲了解更多函数功能请移步http://libusb.sourceforge.net/api-1.0/api.html,里面的讲解十分全

 

posted @ 2017-12-11 19:29  Avatarx  阅读(3086)  评论(0编辑  收藏  举报