zoukankan      html  css  js  c++  java
  • libusb 根据设备的serialnumber来打开

    在libusb开源的源码当中,有根据VID和PID来打开设备的接口。但是这对于设备来说并不唯一。因为有很多设备的VID和PID都是一样的,比如iPhone。

    但是设备的序列号就是唯一的,所以根据参照VID和PID打开的源码,如下就是打开一个指定serialnumber的设备。

    libusb_device_handle* libusb_open_device_with_serialnumber(
    libusb_context* ctx, const char* serial_number) {
    struct libusb_device** devs;
    struct libusb_device* dev;
    struct libusb_device_handle* handle = NULL;
    char string[128];
    char serialnumber[128] = "";
    uint8_t string_index[3];
    size_t i = 0;
    int r = 0;

    if (serial_number)
    strcpy(serialnumber, serial_number);

    if (libusb_get_device_list(ctx, &devs) < 0)
    return NULL;

    while ((dev = devs[i++]) != NULL) {
    struct libusb_device_descriptor desc;
    r = libusb_get_device_descriptor(dev, &desc);

    if (r < 0)
    goto out;

    string_index[2] = desc.iSerialNumber;
    r = libusb_open(dev, &handle);

    if (r < 0) {
    handle = NULL;
    break;
    }

    if (libusb_get_string_descriptor_ascii(handle, string_index[2], (unsigned char*)string, 128) >= 0) {
    if (!strcmp(serialnumber, string))
    break;
    }

    libusb_close(handle);
    handle = NULL;
    }

    out:
    libusb_free_device_list(devs, 1);
    return handle;
    }

  • 相关阅读:
    UVA 1152 4 Values whose Sum is 0
    IOI 2006 Pyramid
    CTSC 2000 冰原探险
    IOI 2009 Mecho
    IOI 2011 Rice Hub 米仓
    NOIP 2013 火柴排队 程序
    USACO 2004 MooFest 奶牛集会
    USACO Training Section 3.1 Contact
    动态加载Ribbon功能区
    Ribbon2: 创建动态的Ribbon库
  • 原文地址:https://www.cnblogs.com/time-invariant/p/6194547.html
Copyright © 2011-2022 走看看