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,里面的讲解十分全
libusb获取usb设备的idVendor(vid),idProduct(pid),以及Serial Number的更多相关文章
- 基于libUSB的USB设备固件更新程序(下载数据)(转)
源:基于libUSB的USB设备固件更新程序(下载数据) 本文紧接上一篇日志:基于libUSB-Win32的USB设备固件更新程序(前言),相关背景以及起因等,此处不再赘述,如感兴趣请移步. libU ...
- C# 获取USB设备信息
C# 获取USB设备信息WMI方式 using System; using System.Management; using System.Text.RegularExpressions; using ...
- Qt 获取usb设备信息 hacking
/************************************************************************** * Qt 获取usb设备信息 hacking * ...
- android获取USB设备的名称
1.注释内 .是三星设备可能不支持,需要更换的代码. 2.mUsbManager.是getSystemService(Context.USB_SERVICE)获的. 3. 从stackoverflow ...
- 获取usb设备父系或子代
/// <summary> /// 获取设备父系 /// </summary> /// <param name="driver"></pa ...
- C#:基于WMI查询USB设备信息 及 Android设备厂商VID列表
/* ---------------------------------------------------------- 文件名称:WMIUsbQuery.cs 作者:秦建辉 MSN:splashc ...
- Windows下USB磁盘开发系列三:枚举系统中U盘、并获取其设备信息
前面我们介绍了枚举系统中的U盘盘符(见<Windows下USB磁盘开发系列一:枚举系统中U盘的盘符>).以及获取USB设备的信息(见<Windows下USB磁盘开发系列二:枚举系统中 ...
- 浅谈USB设备的VID和PID
根据USB规范的规定,所有的USB设备都有供应商ID(VID)和产品识别码(PID),主机通过不同的VID和PID来区别不同的设备,VID和PID都是两个字节长,其中,供应商ID(VID)由供应商向U ...
- usb设备驱动描述,王明学learn
usb设备驱动 本章主要内容包含以下:USB总线介绍,USB协议分析,USB系统架构 一.USB总线介绍 1.1USB发展史 USB(Universal Serial Bus)通用串行总线,是一种外部 ...
随机推荐
- 使用Idea部署SSM项目后,访问路径为url:8080/项目名_war_exploded的解决方案
在tomcat配置页的Deployment下,修改Application context为/,即可直接使用url:8080访问项目主页.
- luoguP3261_[JLOI2015]城池攻占
题意 有一棵树\(n\)个节点,每个节点有一个防御值,以及两个属性,表示一个骑士占领该节点后攻击值是加还是乘,有\(m\)个骑士,有初始位置和初始攻击值,如果攻击值大于该节点的防御值,就能占领该节点, ...
- 在docker容器下利用数据卷实现在删除了mysql容器或者镜像的情况下恢复数据
当把mysql容器销毁,在新建一个容器,进行之前的数据恢复. 因为之前建立了数据卷,那么现在就可以利用这个数据卷进行数据恢复. 使用docker volume create volume_name命令 ...
- TypeError: esri.layers.WMSLayer is not a constructor
最近加载wms地图后,总是报这个错误,因为错误,导致后续的代码无法加载,导致无法功能使用. 原因是,由于方法公用,有的新功能在使用时,引用依赖包时,未引用完整,导致加载此处加载wms图层的时候, 报此 ...
- express做登录判断
1)JWT试试 https://github.com/penguinab/express-jwt 2)express session https://github.com/whevether/reac ...
- linux ssh连接超时断连设置
以下均针对redhat6.5系统进行说明. 一.设置ssh超时断连 使用root用户,编辑/etc/profile文件,在 HOSTNAME='/bin/hostname' HISTIZE=30 后增 ...
- Qt设置生成的exe文件图标
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_37354286/article/d ...
- 模拟赛小结:2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest
2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest 2019年10月11日 15:35-20:35(Solved 8,Penalty 675 ...
- /etc/sysconfig/network-scripts/ifcfg-ens33
因为最近重新搭建CDH集群: DEVICE 接口名(设备,网卡) USERCTL [yes|no](非root用户是否可以控制该设备) BOOTPROTO IP的配置方法[none|s ...
- 【NOIP2016提高组day1】换教室
题目 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的 课程. 在可以选择的课程中,有 2n 节课程安排在 n 个时间段上. 在第 i ( 1 ≤ i ≤ n )个 时间段上,两 ...