发表于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_MAXstruct 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 indexstruct libusb_device_descriptor usb_info;//usb devicestruct libusb_device *usb_d=NULL;//the handle of the opened usb devicestruct libusb_device_handle *usb_p=NULL;//bufferchar buf[CHAR_MAX_LEN]={0};usb_p=libusb_open_device_with_vid_pid(NULL,pid,vid);if(usb_p!=0){//find information indexusb_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 SerialNumberlibusb_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 listusb_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 listfor(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 stlibusb_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 listreturn 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,里面的讲解十分全

转载于:https://www.cnblogs.com/lvdongjie/p/8024699.html

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

  1. linux 查看libusb版本,linux / libusb获取usb设备路径

    我使用libusb来枚举一些usb设备.现在我想获得"设备路径".我认为这不是usb device-path,因为我没有成功使用谷歌. 如果我用linux连接usb设备,我会在dm ...

  2. 基于libUSB的USB设备固件更新程序(下载数据)(转)

    源:基于libUSB的USB设备固件更新程序(下载数据) 本文紧接上一篇日志:基于libUSB-Win32的USB设备固件更新程序(前言),相关背景以及起因等,此处不再赘述,如感兴趣请移步. libU ...

  3. C# 获取USB设备信息

    WMI方式 using System; using System.Management; using System.Text.RegularExpressions; using System.Coll ...

  4. 基于WMI获取USB设备信息(即获取插即用设备信息)System.Management.ManagementObjectSearcher--ManagementObjectCollection

    基于WMI获取USB设备信息(即获取插即用设备信息)System.Management.ManagementObjectSearcher--ManagementObjectCollection 获取P ...

  5. java获取usb设备的相关信息

    我从上上个礼拜开始接触,公司需要,所以开始在网上搜索相关资料,但是都没有找到合适的范例,但万幸终于测试出合适的代码. import java.io.UnsupportedEncodingExcepti ...

  6. 使用libusb检测USB设备插拔状态

    libusb是一个提供USB设备访问的跨平台用户模式程序库.该项目最新网址:http://www.libusb.info, 支持主流的操作系统:Linux.Mac OS X. Windows.Open ...

  7. 用python获取usb设备端口号,用Python查询连接的USB设备信息的简单方法?

    如何在Python中查询连接的USB设备信息? 我想得到UID设备名(例如:SonyEricsson W660),设备路径(例如:dev/ttyACM0) 此外,上述信息中的最佳参数是什么,以便在设备 ...

  8. 通过libusb操作usb设备扫描二维码

    libusb是开源的操作usb设备的类库,可以在Windows, linux, android使用. 以下代码是参照libusb官方网站提供的api文档和example开发的操作二维码扫描头设备的例子 ...

  9. Android获取USB设备信息

    一.通过路径查询 cat /proc/bus/input/devices 二.使用UsbManager获取插入手机的USB设备名字 private void getDevice() {UsbManag ...

最新文章

  1. 戚薇在冰箱放香水,是贫穷限制了想象力!
  2. spring中的quartz调度问题
  3. 12:MYSQL 使用函数创建自增序列管理表(批量使用自增表,设置初始值,自增幅度)
  4. ubuntu mysql 迁移_(最新)ubuntu20.04LTS版迁移mysql8.0数据库的方法
  5. file 选择的图片作为背景图片_酷炫!用Python把桌面变成实时更新的地球图片
  6. 华为手机的分类有何区别_“鸿蒙”系统能不能玩安卓游戏?如果能,它跟安卓系统有何区别?...
  7. MUI 图标显示不出来 - 分析篇
  8. 全文来了!任正非:全球经济长期衰退,华为要把活下来作为主要纲领
  9. 数据结构-带头双向循环链表
  10. 毕业之后, 这些年薪50万+的90后程序员都经历了什么?
  11. 联合国可持续发展目标,中国大数据服务支持
  12. 微信小程序使用三元运算符
  13. Java-Swing内嵌网页判断网址类型
  14. arch Linux 作妖记录(U盘启动)
  15. janusgraph编程指南之-Schema设计
  16. 选择什么样的思想,就会有什么样的结局
  17. 最好用的格式转换器!
  18. 在家做视频二次剪辑,一个短视频播放量40多万,收益有800多
  19. mle matlab,MLE的Matlab程序
  20. 如何使用Chrome DevTools花式打断点

热门文章

  1. 【嵌入式】Linux开发工具arm-linux-gcc安装及使用
  2. 页面显示拒绝访问请求
  3. 渗透测试攻击(二)——wireshark过滤数据包语法详解
  4. vnpy学习11_增加测试评估指标
  5. 安装docker和jupyter采坑历程
  6. wepe 装linux ubuntu,Ubuntu16.04下安装Wechat的实现方法
  7. 干货!一文讲清楚电商商品生产和库存的数据分析
  8. Vue父子组件传递数据
  9. python标准库模块教程_Python标准库之Sys模块使用详解
  10. windows安装ffmpeg_免费实用的录屏工具!支持全屏、特定窗口、选定区域录制,支持添加水印、嵌入摄像头(附ffmpeg安装)...