UsbDevice.FromIdAsync在UWP应用中返回null

时间:2019-05-16 08:32:02

标签: windows uwp usb iot

我目前正在开发UWP应用,其中包括与自定义USB设备的USB通信。我能够在Raspberry Pi 3b和设备之间建立通信通道,并来回发送一些字节。如果我在本地计算机(Windows 10 build 1809)上运行此应用程序,它将无法正常工作。在这种情况下,对UsbDevice.FromIdAsync的调用始终返回NULL。

我看到了一些相关的问题hereherehere,它们具有相似甚至相同的问题。但是,提到的解决方案都没有对我有用。

我使用以下inf文件安装了Win USB驱动程序:

;
; Installs WinUsb
;
[Version]
Signature = "$Windows NT$"
Class     = USBDevice
ClassGUID = {88BAE032-5A81-49f0-BC3D-A4FF138216D6}
Provider  = %ManufacturerName%
CatalogFile = WinUSBInstallation.cat
DriverVer = 12/12/2016,13.54.20.543

[Generic.Section.NTamd64]
%USB\MS_COMP_WINUSB.DeviceDesc% = WINUSB,USB\MS_COMP_WINUSB 

; ========== Manufacturer/Models sections ===========

[Manufacturer]
%ManufacturerName% = Standard,NTamd64

[Standard.NTamd64]
%DeviceName% = USB_Install, USB\VID_0483&PID_0001

; ========== Class definition ===========

[ClassInstall32]
AddReg = ClassInstall_AddReg

[ClassInstall_AddReg]
HKR,,,,%ClassName%
HKR,,NoInstallClass,,1
HKR,,SilentInstall,,1 
HKR,,IconPath,%REG_MULTI_SZ%,"%systemroot%\system32\setupapi.dll,-20"
HKR,,LowerLogoVersion,,5.2

; =================== Installation ===================

[USB_Install]
Include = winusb.inf
Needs   = WINUSB.NT

[USB_Install.Services]
Include = winusb.inf
Needs   = WINUSB.NT.Services

[USB_Install.HW]
AddReg = Dev_AddReg

[Dev_AddReg]
HKR,,DeviceInterfaceGUIDs,0x10000,"{9f543223-cede-4fa3-b376-a25ce9a30e74}"

; =================== Strings ===================

[Strings]
ManufacturerName = "Manufacturer"
ClassName = "Universal Serial Bus devices"
DeviceName = "Device"
REG_MULTI_SZ = 0x00010000

设备在设备管理器中正确显示,没有任何错误。 我将设备功能添加到Package.appxmanifest中,如下所示:

<Capabilities>
  <DeviceCapability Name="usb">
    <Device Id="vidpid:0483 0001">
      <Function Type="classId:ff 00 00"/>
      <Function Type="name:vendorSpecific"/>
    </Device>
  </DeviceCapability>
</Capabilities>

在我的代码中,我尝试如下打开设备:

byte deviceClass = 0xff;
byte deviceSubclass = 0x00;

var myDevices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(
    Windows.Devices.Usb.UsbDevice.GetDeviceClassSelector(
        new Windows.Devices.Usb.UsbDeviceClass() {
            ClassCode = deviceClass,
            SubclassCode = deviceSubclass
        }
    )
);

UsbDevice device = null;
var numDevices = myDevices.Count;

foreach (var d in myDevices) {
    device = await UsbDevice.FromIdAsync(d.Id);
    if (device == null) {
        // Device could not be found.
    }
    else {
        // Found device.
    }
}

对FindAllAsync的调用的确找到了具有正确ID的单个设备。但是调用FromIdAsync总是返回NULL。如前所述,这些都可以在Raspberry Pi 3b上运行,但不能在台式机或我尝试过的任何其他计算机上本地运行。 我搜索了几天,但我不知道我仍然忽略或遗漏了什么。还有什么建议我还能做什么?

最诚挚的问候

0 个答案:

没有答案