【问题标题】:UWP app cannot find/connect to USB deviceUWP 应用无法找到/连接到 USB 设备
【发布时间】:2017-04-27 02:00:50
【问题描述】:

我正在尝试制作一个连接到 USB 设备然后执行一系列命令的 UWP 应用,例如从内部传感器检索数据(想想加速度计)。我开始遵循以下准则:

https://msdn.microsoft.com/en-us/library/windows/hardware/dn303343(v=vs.85).aspx

所以,我也尝试制作一个空白应用,并相应地调整清单:

<Capabilities>
  <DeviceCapability Name="usb">
    <Device Id="vidpid:1CBE 0003">
      <Function Type="classId:ff 00 00" />
    </Device>
  </DeviceCapability>
</Capabilities>

可以肯定的是,这就是设备在设备管理器中标识自己的方式:

然后用了

string aqs = UsbDevice.GetDeviceSelector(vid, pid);
var finder = await DeviceInformation.FindAllAsync(aqs);

但是,没有成功。问题很简单,应用找不到任何设备。然后我继续修改这个示例应用程序(它使用 DeviceWatcher 而不是上述查找连接设备 USB 的方式):

https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/CustomUsbDeviceAccess

其中也没有找到 USB 设备。当然我尝试了另一台PC,看看是否与我的配置有关,但正如您所料,没有成功。这让我认为它可能与 USB 设备有关,但有什么问题呢?还是我真的在这五行中犯了一些错误?还有其他方法可以尝试连接到 USB 设备吗?任何提示表示赞赏!

相关问题:

UWP/C# - Issue with AQS and USB Devices

https://social.msdn.microsoft.com/Forums/en-US/e9b85e2a-27c0-489e-9bf5-d990f0e83a61/uwpissue-with-usbdevicegetdeviceselector-not-finding-attached-bulk-usb-device?forum=wpdevelop

【问题讨论】:

    标签: usb uwp


    【解决方案1】:

    看来我找到了解决这个问题的方法,这有点愚蠢,但是嘿,反正谁问我......

    我在 stackoverflow 上遇到了这个: Cannot create UsbDevice from DeviceInformation.Id

    当我使用 .inf 将 winusb 称为驱动程序时,我的问题似乎确实得到了解决。我不知道为什么,所以如果你们有任何解释,请告诉我。

    正如上面的答案是指一个确实存在的博客文章(我使用回程机器来获取它),我在这里发布了 inf,以防它帮助任何人(但它是一个普通的 inf)

    ;
    ;
    ; 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
    
    ; ========== Manufacturer/Models sections ===========
    
    [Manufacturer]
    %ManufacturerName% = Standard,NTamd64
    
    [Standard.NTamd64]
    %DeviceName% =USB_Install, USB\VID_1267&PID_0000
    
    ; ========== Class definition ===========
    
    [ClassInstall32]
    AddReg = ClassInstall_AddReg
    
    [ClassInstall_AddReg]
    HKR,,,,%ClassName%
    HKR,,NoInstallClass,,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}"
    
    ; [DestinationDirs]
    ; If your INF needs to copy files, you must not use the DefaultDestDir         directive here.  
    ; You must explicitly reference all file-list-section names in this     section.
    
    ; =================== Strings ===================
    
    [Strings]
    ManufacturerName=""
    ClassName="Universal Serial Bus devices"
    DeviceName="OWI-535 Robotic Arm"
    REG_MULTI_SZ = 0x00010000
    

    请注意,我在驱动程序中留下了任意 VID 和 PID,但我仍然必须连接设备告诉我的 VID 和 PID。

    【讨论】:

    【解决方案2】:

    这是在 UWP 上连接 WinUSB 设备的方法。

    public async Task<IEnumerable<DeviceDefinition>> GetConnectedDeviceDefinitions(uint? vendorId, uint? productId)
    {
        var aqsFilter = "System.Devices.InterfaceClassGuid:=\"{DEE824EF-729B-4A0E-9C14-B7117D33A817}\" AND System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True AND " + $" System.DeviceInterface.WinUsb.UsbVendorId:={vendorId.Value} AND System.DeviceInterface.WinUsb.UsbProductId:={productId.Value}";
    
        var deviceInformationCollection = await wde.DeviceInformation.FindAllAsync(aqsFilter).AsTask();
    
        //TODO: return the vid/pid if we can get it from the properties. Also read/write buffer size
    
        var deviceIds = deviceInformationCollection.Select(d => new DeviceDefinition { DeviceId = d.Id, DeviceType = DeviceType.Usb }).ToList();
        return deviceIds;
    }
    

    这里有一个更完整的答案:https://stackoverflow.com/a/53954352/1878141

    还有,这里有一个来自 repo 的类:https://github.com/MelbourneDeveloper/Device.Net/blob/master/src/Usb.Net.UWP/UWPUsbDeviceFactory.cs

    【讨论】:

      【解决方案3】:

      在我的情况下,设备的DeviceInterfaceGUID 注册表项丢失,这似乎是必需的,以便可以找到并实例化 WinUSB 设备。

      我添加了带有随机 GUID 的条目 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_16D0&amp;PID_0BD7\None\Device Parameters\DeviceInterfaceGUID,例如 {86529001-c433-4530-a578-9a67adf1ffa9}(在我的例子中,16D0 是供应商 ID,0BD7 是产品 ID,None 是实例)。这可以通过命令行来完成,例如调用reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_16D0&PID_0BD7\None\Device Paramet ers" /v "DeviceInterfaceGUID" /t REG_SZ /d "{86529001-c433-4530-a578-9a67adf1ffa9}" /f

      我绝对不是 USB 方面的专家,也不知道为什么需要它或为什么它丢失了;但至少在我的情况下,添加 GUID 对我的 Windows 10 Professional 和 Windows 10 IoT Core (Raspberry PI 3B) 都有帮助。或许可以查看https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/usb-device-specific-registry-settings 了解更多详情。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-21
        • 2013-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多