查看方2113法: 打开设备5261管理器,右键USB设备--属性--详细信息--设备范例4102id。 根据USB规范的规定,1653所有的USB设备都有供应商ID(VID)和产品识别码(PID),主机通过不同的VID和PID来区别不同的设备,VID和PID都是两个字节长,其中,供应商ID(VID)由供应商向USB执行论坛申请,每个供应商的VID是唯一的,PID由供应商自行决定,理论上来说,不同的产品、相同产品的不同型号、相同型号的不同设计的产品最好采用不同的PID,以便区别相同厂家的不同设备。 VID和PID通常情况下有两种存储方式, 第一种是主控生产商的VID和PID,存储在主控的bootcode中; 第二种是设备生产商的VID和PID,该VID和PID存储在主控外部的非易失性存储设备中(EEPROM或Flash)的设备固件中,当USB设备连接主机时,如果固件中有设备生产商的VID和PID,会将该VID和PID报告给主机,而忽略主控生产商的VID和PID。 所以理论上一个USB存储设备的VID应该是设备生产商的VID,而不是主控生产商的VID,这两个VID应该是不同的(主控生产商自己生产的设备除外)。 由于VID和PID重复并不会对产品的使用带来严重影响,很多USB设备生产商(山寨厂居多)为了方便,并不会向USB执行论坛申请自己的VID,而是依然沿用主控生产商的VID或随便向产品写入VID和PID; 同时,正规厂家只需要申请VID,PID由厂家自行确定,所以存在相同型号的产品,可能采用了不同的主控(商业需要,很正常),而的PID是一样的,基于上述原因通过VID和PID就不能准确识别USB设备的主控型号,这个问题大家在使用USB设备的过程中需要注意www.zgxue.com防采集请勿采集本网。
在本文将使用libudev库来访问hidraw的设备。通过libudev库,我们可以查询设备的厂家ID(Vendor ID, VID),产品ID(Product ID, PID),序列号和设备字符串等而不需要打开设备。进一步,libudev可以告诉我们在/dev目录下设备节点的具体位置路径,为应用程序提供一种具有足够鲁棒性而又和系统厂家独立的访问设备的方式。使用libudev库,需要包含libudev.h头文件,并且在编译时加上-ludev告诉编译器去链接udev库。
由于公司的产品,导致我在Linux下写usb通信的代码出现了许多的问题,最后发现这些设备只能通过控制传输来操作设备,所以后面的问题就迎刃而解了。现在贴上当时
将列出当前连接在系统中的所有hidraw设备,并且输出它们的设备节点路径、生产商、序列号等信息。
pid和vid信息 在/sys属于自己所在总线的设备目录中,比如: yc@yc-pc:/sys/bus/usb/devices/usb1$ cat idVendor ;cat idProduct 1d6b 0002
为了获取这些信息,需要创建一个udev_enumerate对象,其中“hidraw”字符串作为过滤条件,
WMI(Windows??Management Instrumentation ) 非常强大,它可以以数据库的形式查询你的电脑的软件和硬件,在它的数据库里面,时刻保存着最新的软件信息和硬件信息,因此你
libudev将返回所有匹配这个过滤字符串的udev_device对象。
有一些软件可以获取这些信息,看看360硬件、驱动精灵、鲁大师等那款软件能获取这些值,然后监控一下软件的API调用,看是否能找到些什么。
这个列子的步骤如下:
查看方法: 打开设备管理器,右键USB设备--属性--详细信息--设备范例id。 根据USB规范的规定,所有的USB设备都有供应商ID(VID)和产品识别码(PID),主机
1、 初始化库,获取一个struct udev句柄
2、枚举设备
3、对找到的匹配设备输出它的节点名称,找到实际USB设备的起始节点,打印出USB设备的IDs和序列号等,最后解引用设备对象
4、解引用枚举对象
5、解引用udev对象
具体代码如下:
#include <libudev.h>#include <stdio.h>#include <stdlib.h>#include <locale.h>#include <unistd.h>int main (void){ struct udev *udev; struct udev_enumerate *enumerate; struct udev_list_entry *devices, *dev_list_entry; struct udev_device *dev; /* Create the udev object */ udev = udev_new(); if (!udev) { printf("Can't create udev\n"); exit(1); } /* Create a list of the devices in the 'hidraw' subsystem. */ enumerate = udev_enumerate_new(udev); udev_enumerate_add_match_subsystem(enumerate, "hidraw"); udev_enumerate_scan_devices(enumerate); devices = udev_enumerate_get_list_entry(enumerate); /* For each item enumerated, print out its information. udev_list_entry_foreach is a macro which expands to a loop. The loop will be executed for each member in devices, setting dev_list_entry to a list entry which contains the device's path in /sys. */ udev_list_entry_foreach(dev_list_entry, devices) { const char *path; /* Get the filename of the /sys entry for the device and create a udev_device object (dev) representing it */ path = udev_list_entry_get_name(dev_list_entry); dev = udev_device_new_from_syspath(udev, path); /* usb_device_get_devnode() returns the path to the device node itself in /dev. */ printf("Device Node Path: %s\n", udev_device_get_devnode(dev)); /* The device pointed to by dev contains information about the hidraw device. In order to get information about the USB device, get the parent device with the subsystem/devtype pair of "usb"/"usb_device". This will be several levels up the tree, but the function will find it.*/ dev = udev_device_get_parent_with_subsystem_devtype( dev, "usb", "usb_device"); if (!dev) { printf("Unable to find parent usb device."); exit(1); } /* From here, we can call get_sysattr_value() for each file in the device's /sys entry. The strings passed into these functions (idProduct, idVendor, serial, etc.) correspond directly to the files in the directory which represents the USB device. Note that USB strings are Unicode, UCS2 encoded, but the strings returned from udev_device_get_sysattr_value() are UTF-8 encoded. */ printf(" VID/PID: %s %s\n", udev_device_get_sysattr_value(dev,"idVendor"), udev_device_get_sysattr_value(dev, "idProduct")); printf(" %s\n %s\n", udev_device_get_sysattr_value(dev,"manufacturer"), udev_device_get_sysattr_value(dev,"product")); printf(" serial: %s\n", udev_device_get_sysattr_value(dev, "serial")); udev_device_unref(dev); } /* Free the enumerator object */ udev_enumerate_unref(enumerate); udev_unref(udev); return 0;}
编译程序:
gcc -Wall -g -o udev_example udev_example.c -ludev
pid和vid信息 在/sys属于自己所在总线的设备目录中,比如:yc@yc-pc:/sys/bus/usb/devices/usb1$ cat idVendor ;cat idProduct 1d6b0002内容来自www.zgxue.com请勿采集。