linux/libusb获取USB设备路径 [英] linux / libusb get usb device path

查看:1347
本文介绍了linux/libusb获取USB设备路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用libusb枚举了几个USB设备.现在我喜欢获取设备路径".我认为它不称为usb设备路径,因为我在google上并不成功.

I use libusb to enumerate over a few usb-devices. Now i like to get the "device-path". I think it's not called usb device-path, because i was not successful with google.

如果我将USB设备与linux连接,则会在dmesg中收到一条消息,这是带有USB温度传感器的设备路径"的一些示例(例如

If i connect a usb-device with linux, i get a message in dmesg, here are a few examples for such a "device-path" with an usb temperature sensor (something like this):

直接连接到USB端口: [68448.099682] generic-usb 0003:0C45:7401.0056: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:12.0-1/input0 => 12.0-1

Directly to a usb port: [68448.099682] generic-usb 0003:0C45:7401.0056: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:12.0-1/input0 => 12.0-1

直接到另一个端口: [68560.853108] generic-usb 0003:0C45:7401.0058: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:13.0-1/input0 => 13.0-1

Directly to another port: [68560.853108] generic-usb 0003:0C45:7401.0058: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:13.0-1/input0 => 13.0-1

到第一个使用的端口上的USB集线器: [68600.245809] generic-usb 0003:0C45:7401.005A: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:12.2-1.4/input0 => 12.2-1.4

To a usb hub on the first used port: [68600.245809] generic-usb 0003:0C45:7401.005A: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:12.2-1.4/input0 => 12.2-1.4

到同一USB集线器上的另一个端口: [68647.925092] generic-usb 0003:0C45:7401.005C: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:12.2-1.3/input0 => 12.2-1.3

To another port on the same usb hub: [68647.925092] generic-usb 0003:0C45:7401.005C: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:12.2-1.3/input0 => 12.2-1.3

现在使用的USB集线器上的USB集线器现在: [68740.715518] generic-usb 0003:0C45:7401.005E: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:12.2-1.4.4/input0 => 12.2-1.4.4

An now to a usb hub on the usb hub used before: [68740.715518] generic-usb 0003:0C45:7401.005E: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:12.2-1.4.4/input0 => 12.2-1.4.4

长话短说: 内核消息始终包含用于物理USB设备位置的唯一路径(请参见前面的粗体文本).是否可以通过libusb在用户空间中获得此路径"?我用struct usb_busstruct usb_device尝试了很多东西,但是我总是失败了.

Long story short: The kernel message always contains a unique path for the physical usb device location (see the bold text before). Is it possible to get this "path" in the user space via libusb? I tried many things with struct usb_bus and struct usb_device, but i always was unsuccessfully.

我需要用它来识别多个USB温度计,因为它们没有唯一的序列号,有时它们只是在运行时重新连接",因此它们获得了不同的USB ID.因此,我认为识别它们的唯一方法是通过物理位置.

I need this to identify multiple of these usb thermometers, because they don't have a unique serial number and sometimes they just "reconnect" at runtime, so they get different usb id's. So i think the only way to identify them is via the physical location.

感谢您的帮助,

最好的问候 凯文M.

-编辑-

当前,我使用以下代码搜索我的USB设备:

Currently i use the following code to search my usb device:

usb_dev_handle *find_lvr_winusb() {

     struct usb_bus *bus;
        struct usb_device *dev;

        for (bus = usb_busses; bus; bus = bus->next) {
        for (dev = bus->devices; dev; dev = dev->next) {
                        if (dev->descriptor.idVendor == VENDOR_ID && 
                                dev->descriptor.idProduct == PRODUCT_ID ) {
                                usb_dev_handle *handle;
                                if(debug) {
                                  printf("lvr_winusb with Vendor Id: %x and Product Id: %x found.\n", VENDOR_ID, PRODUCT_ID);
                                    printf("INFO: %d\n", dev->bus->location);
                                    printf("INFO: %d %s\n", bus->location, bus->dirname);
                                }

                                if (!(handle = usb_open(dev))) {
                                        printf("Could not open USB device\n");
                                        return NULL;
                                }
                                return handle;
                        }
                }
        }
        return NULL;
}

但是使用此代码,我无法获得唯一的身体位置ID. bus->location返回一个整数(bus-> dirname包含一个整数,但作为字符串),该整数不是唯一的.我知道USB有一个层次结构,在dmesg中我可以看到此层次结构路径.

But with this code i cannot get a unique physical position id. The bus->location returns an integer (bus->dirname contains the same, but as string), which is not unique. I know usb has a hierarchy and in the dmesg i can see this hierarchys path.

使用libusb,我只能获得总线ID(?)和某些设备ID.但是它们对我没有帮助,因为我需要确定两个或多个这些温度传感器.当温度传感器重置连接(每5至60秒)并且总线ID不是唯一时,设备ID始终会更改.不幸的是,温度传感器没有唯一的序列号.

With libusb i only can get the bus-id (?) and some device id's. But they don't help me, because i need to identify two or more of these temperature sensors. The device-id always changes when the temperature sensor reset's the connection (every 5 to 60 seconds) and the bus id is not unique. Unfortunately the temperature sensor has no unique serial id.

所以我认为物理路径是识别设备的唯一方法.

So i think the physical path is the only way to identify the device.

最诚挚的问候 凯文M.

Best regards Kevin M.

推荐答案

从libusb 1.0.12开始,他们引入了libusb_get_port_path(),并在1.0.16中将其替换为

Since libusb 1.0.12, they have introduced libusb_get_port_path(), and in 1.0.16 replaced it with libusb_get_port_numbers() which allows you to query the bus topology.

这篇关于linux/libusb获取USB设备路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆