从USB设备描述符获取数据

时间:2018-09-07 07:12:04

标签: c# .net wpf usb-descriptor

在我的项目(C#,WPF应用程序)中,我有显示为VCP的设备。我需要连接到它。我正在使用WMI检测串行端口,并通过VID和PID进行过滤。它使工作完成了90%。设备制造商对所有设备使用相同的VID / PID对。准确的模型位于USB描述符中(Device Decsriptor部件,属性iProduct)。我在探索WMI的任何地方都找不到。

如何通过.NET转到USB decriptor?在C# read USB Descriptor中,建议使用WMI。在WMI中,设备描述不是USB描述符。我不需要列出连接的USB设备,而是从USB设备描述符中读取特定数据。

1 个答案:

答案 0 :(得分:0)

非常有用的文章https://lihashgnis.blogspot.com/2018/07/getting-descriptors-from-usb-device.html 我刚刚添加了一些代码来获取字符串描述符:

    USB_STRING_DESCRIPTOR* stringDescriptor = nullptr;
    int sBufferSize = sizeof(USB_DESCRIPTOR_REQUEST) + MAXIMUM_USB_STRING_LENGTH;
    BYTE *sBuffer = new BYTE[sBufferSize];
    memset(sBuffer, 0, sBufferSize);

    requestPacket = (USB_DESCRIPTOR_REQUEST*)sBuffer;
    stringDescriptor = (USB_STRING_DESCRIPTOR*)((BYTE*)sBuffer + sizeof(USB_DESCRIPTOR_REQUEST));

    requestPacket->SetupPacket.bmRequest = 0x80;
    requestPacket->SetupPacket.bRequest = USB_REQUEST_GET_DESCRIPTOR;
    requestPacket->ConnectionIndex = usbPortNumber;
    requestPacket->SetupPacket.wValue = (USB_STRING_DESCRIPTOR_TYPE << 8); // String Descriptior 0
    requestPacket->SetupPacket.wLength = MAXIMUM_USB_STRING_LENGTH;

    err = DeviceIoControl(hUsbHub, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, sBuffer, sBufferSize, sBuffer, sBufferSize, &bytesReturned, nullptr);

    // Now get iProduct string in language at zero index
    requestPacket->SetupPacket.wValue = (USB_STRING_DESCRIPTOR_TYPE << 8) | deviceDescriptor->iProduct;
    requestPacket->SetupPacket.wIndex = (USHORT)stringDescriptor->bString[0];

    err = DeviceIoControl(hUsbHub, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, sBuffer, sBufferSize, sBuffer, sBufferSize, &bytesReturned, nullptr);

    std::wcout << stringDescriptor->bString