簡體   English   中英

UWP/C# - AQS 和 USB 設備問題

[英]UWP/C# - Issue with AQS and USB Devices

我正在嘗試編寫的 UWP 應用程序存在問題。 我正在連接到我已編程的自定義嵌入式 USB 批量設備(它實際上是賽普拉斯半導體的開箱即用示例)。 我正在使用 WinUSB.sys 驅動程序,在設備中使用嵌入式 MS OS 字符串,以便無需編寫自定義 INF 文件即可使用該設備來調用 WinUSB.sys 驅動程序。

在我的代碼中,我使用 UsbDevice.GetDeviceSelector 方法返回一個 AQS,然后可以將其傳遞到 DeviceInformation.FindAllAsync 以開始與我的應用程序中的設備進行通信。 我已經確認設備在設備管理器中顯示沒有任何問題,並且我已經檢查了注冊表以確保它具有接口 GUID。 我有一個來自 USBViewer 的屏幕截圖來顯示設備的配置。 這種用於查找和連接 USB 設備的方法來自此處的MSDN 示例。

當我使用 UsbDevice.GetDeviceSelector 方法時,它返回一個與此設備無關的 GUID。 它返回的 GUID 實際上與 Lumia Phones (DEE824EF-729B-4A0E-9C14-B7117D33A817) 相關聯。 因此,它找不到我的設備連接到系統。

為了進行故障排除,我都調用了 DeviceInformation.FindAllAsync 而不帶任何參數來查看我的設備是否被列出,並且它確實找到了該設備(在曾經連接到我的機器的 1000 多個其他設備中)。 然后,我在沒有 GetDeviceSelector 方法幫助的情況下編寫了一個自定義 AQS 字符串,僅從 GUID 開始。 這樣做會返回 27 個設備,但是當我嘗試將 VID 和 PID 添加到此 AQS 字符串時,沒有返回任何內容。

我還確保我要使用的設備按其適當的 VID 和 PID 列在應用程序清單中,因為這是自定義類為 0xFF 的設備所必需的。 我使用了自定義 USB UWP 設備示例,它可以找到該設備,盡管它使用了一種完全不同的方法和設備選擇器,如果需要,我會去,但這不是我的願望,因為它使該部分成為應用程序沒有那么干凈的解決方案。

我已經發布了在MSDN論壇這個問題, 這里有更多的信息,但我還沒有得到很多參與那里。 任何幫助將不勝感激。 我知道我一定錯過了一些簡單的東西。

亞當

 private async void button_Click(object sender, RoutedEventArgs e) { //UInt32 vid = 0x04B4; //UInt32 pid = 0x00F0; UInt32 vid = uint.Parse(textBox1.Text, System.Globalization.NumberStyles.HexNumber); UInt32 pid = UInt32.Parse(textBox2.Text, System.Globalization.NumberStyles.HexNumber); Guid winusbInterfaceGuid = new Guid("a5dcbf10-6530-11d2-901f-00c04fb951ed"); //string aqs = UsbDevice.GetDeviceSelector(vid, pid); string aqs = UsbDevice.GetDeviceSelector(winusbInterfaceGuid); var myDevices = await DeviceInformation.FindAllAsync(aqs, null); //var myDevices = await DeviceInformation.FindAllAsync(); var myDevicesCount = myDevices.Count; if (myDevicesCount >= 1) { textBlock2.Text = "Device Found"; } else { textBlock2.Text = "Searching"; await Task.Delay(1000); textBlock2.Text = "looking for device"; } }

剛剛給您發了一封詢問進度的郵件(我認為,必須猜測您的郵件地址),但現在看來我自己找到了解決方案。 請參閱我對UWP 應用無法找到/連接到 USB 設備的回答

簡而言之,您必須創建一個用於安裝 winusb 驅動程序的 inf。 我不知道為什么,但這對我有用(和其他人,請參閱無法從 DeviceInformation.Id 創建 UsbDevice

Guid DEE824EF-729B-4A0E-9C14-B7117D33A817 實際上是標准的 WinUSB Guid。 我認為這與 Lumia Phones 沒有任何關系。 我不知道為什么它沒有在任何地方記錄。 我認為您指定的 Guid a5dcbf10-6530-11d2-901f-00c04fb951ed 實際上是一條紅鯡魚。 我也錯誤地使用了它,但它只是把我帶到了花園小徑上。 它顯示USB接口,但我無法連接到它們。

你可能想試試這個類https://github.com/MelbourneDeveloper/Device.Net/blob/master/src/Usb.Net.UWP/UWPUsbDevice.cs

這是它獲取設備的方式:

    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;
    }

此示例連接到設備,我認為您將能夠以相同的方式連接到設備:

        private static async Task InitializeTrezor()
        {
            //Register the factory for creating Usb devices. This only needs to be done once.
            UWPUsbDeviceFactory.Register();

            //Register the factory for creating Usb devices. This only needs to be done once.
            UWPHidDeviceFactory.Register();

            //Note: other custom device types could be added here

            //Define the types of devices to search for. This particular device can be connected to via USB, or Hid
            var deviceDefinitions = new List<DeviceDefinition>
            {
                new DeviceDefinition{ DeviceType= DeviceType.Hid, VendorId= 0x534C, ProductId=0x0001, Label="Trezor One Firmware 1.6.x" },
                new DeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x1209, ProductId=0x53C1, ReadBufferSize=64, WriteBufferSize=64, Label="Trezor One Firmware 1.7.x" },
                new DeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x1209, ProductId=0x53C0, ReadBufferSize=64, WriteBufferSize=64, Label="Model T" }
            };

            //Get the first available device and connect to it
            var devices = await DeviceManager.Current.GetDevices(deviceDefinitions);
            var trezorDevice = devices.FirstOrDefault();
            await trezorDevice.InitializeAsync();

            //Create a buffer with 3 bytes (initialize)
            var buffer = new byte[64];
            buffer[0] = 0x3f;
            buffer[1] = 0x23;
            buffer[2] = 0x23;

            //Write the data to the device
            await trezorDevice.WriteAsync(buffer);

            //Read the response
            var readBuffer = await trezorDevice.ReadAsync();
      }

如果您以這種方式連接到設備,您將通過 Device.Net ( https://github.com/MelbourneDeveloper/Device.Net ) 免費獲得 Windows 經典版和 Android 支持

使用 Device.net 的 DeviceManager.Current.GetDevices(deviceDefinitions) 使用 .NET 5 我找不到任何連接到我的 win10 的設備,ManagementObjectSearcher 可以輕松選擇這些設備:

public List<ManagementBaseObject> GetLogicalDevices()
{
   List<ManagementBaseObject> devices = new List<ManagementBaseObject>();
   ManagementObjectCollection collection;
   ManagementObjectSearcher seacher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM CIM_LogicalDevice");
   collection = seacher.Get();

   foreach (var device in collection)
   {
       devices.Add(device);
   }

   return devices;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM