【问题标题】:UWP USB DeviceWatcher Doesn't WorkUWP USB DeviceWatcher 不起作用
【发布时间】:2018-03-12 07:12:15
【问题描述】:

首先,我应该指出,我已将需要与之通信的设备添加到我的应用清单中,并且我能够成功地与该设备通信。我可以通过此行获取设备的此设备信息:

var trezorDeviceInformation = await UWPHidDevice.GetDeviceByIdSlowAsync(TrezorManager.HidId);

但是,由于某种原因,如果我尝试使用产品 ID 和供应商 ID 监视设备,则监视程序的事件永远不会触发。我试过这个:

    public UWPHidDevice(uint vendorId, uint productId)
    {
        var deviceWatcher = DeviceInformation.CreateWatcher($"System.DeviceInterface.WinUsb.UsbVendorId:={vendorId} AND System.DeviceInterface.WinUsb.UsbProductId:={productId}");
        deviceWatcher.Added += DeviceWatcher_Added;
        deviceWatcher.Removed += DeviceWatcher_Removed;
        deviceWatcher.Start();
    }

和

    public UWPHidDevice(uint vendorId, uint productId)
    {
        string aqs = UsbDevice.GetDeviceSelector(vendorId, productId);
        var deviceWatcher = DeviceInformation.CreateWatcher(aqs);
        deviceWatcher.Added += DeviceWatcher_Added;
        deviceWatcher.Removed += DeviceWatcher_Removed;
        deviceWatcher.Start();
    }

当我插入设备或使用设备启动应用程序时,添加的事件永远不会触发。我必须强调我可以在我的应用程序中使用这个设备,并且我已经三次检查了 VendorId 和 ProductId。只是观察者不起作用。我被卡住了。

有人有提示或示例应用吗?

编辑:我已经尝试过官方的 UWP 示例应用。我将 VendorId 和 ProductId 替换为我需要在代码和应用清单中使用的设备值。样本也有同样的问题。

【问题讨论】:

  • 最简单的方法是使用 Official sample 将 vendorId 和 productId 替换为您自己来测试您的设备。但是你说官方app不会编译,很奇怪,官方sample在我这边运行的很好,所以第一步你应该尝试让官方app运行。
  • 错误:严重性代码描述项目文件行抑制状态错误您的项目未引用“UAP,Version=v10.0.16299”框架。在 project.json 的“frameworks”部分添加对“UAP,Version=v10.0.16299”的引用,然后重新运行 NuGet 还原。 CustomHidDeviceAccess
  • 我已经尝试升级 UWP NuGet 包,但它什么也没做。我正在使用 VS 15.5.7
  • 好的。刮那个。我做了一个 Git 清理,现在应用程序正在运行。我仍然没有看到该设备,但如果我无法让示例应用正常工作,我会尝试一些方法并编辑原始帖子。
  • 我已经替换了清单中的 Vid 和 Pid,以及 SampleConfiguration.cs 文件,但什么也没出现。

标签: c# uwp usb trezor


【解决方案1】:

您的设备是否使用 WinUSB 驱动程序显示在设备管理器中?只有在使用 WinUSB 驱动程序时,您的代码才适用于我的供应商 ID 和 pid。

我使用这个代码:

// Create a device watcher to look for instances of the Xerox device
watcher = DeviceInformation.CreateWatcher(UsbDevice.GetDeviceSelector(vendorId, productId));

watcher.Added += new TypedEventHandler<DeviceWatcher, DeviceInformation>(this.OnDeviceAdded);
watcher.Removed += new TypedEventHandler<DeviceWatcher, DeviceInformationUpdate>(this.OnDeviceRemoved);
watcher.EnumerationCompleted += new TypedEventHandler<DeviceWatcher, Object>(this.OnDeviceEnumerationComplete);
watcher.Start();

【讨论】:

  • 什么是 WinUSB 驱动程序?
  • 这是与您的 USB 设备关联的 Windows 驱动程序。在大多数情况下,您只需要一个 INF 文件,它会告诉 Windows 您的设备的 VID 和 PID,以便在连接设备时。 WinUSB.SYS 用作设备管理器中的驱动程序。
猜你喜欢
  • 2017-05-20
  • 2016-03-12
  • 2020-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-15
相关资源
最近更新 更多