UWP app cannot find/connect to USB device

前端 未结 3 1350
一向
一向 2020-12-19 08:02

I am trying to make a UWP app which connects to a USB device and then executes a series of commands, like retrieving data from the internal sensor (think of an accelerometer

相关标签:
3条回答
  • 2020-12-19 08:23

    It seems that I found one way to resolve this, which is a bit stupid, but hey, who asks me anyway...

    I came across this one here on stackoverflow: Cannot create UsbDevice from DeviceInformation.Id

    And it seems that my issue is indeed resolved when I use a .inf to refer to winusb as the driver. I have no idea why, so if any of you have an explanation, please let me know.

    As above answer is referring to a blogpost that does exist anymore (I used the wayback machine to get to it), I'm posting the inf here, in case it helps anyone (but it's an ordinary 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
    

    Note that I left an arbitrary VID and PID in the driver, but I still have to connect with the VID and PID that the device tells me.

    0 讨论(0)
  • 2020-12-19 08:27

    This is how to connect to a WinUSB device on UWP.

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

    Here is a more complete answer: https://stackoverflow.com/a/53954352/1878141

    And, here is a class from the repo: https://github.com/MelbourneDeveloper/Device.Net/blob/master/src/Usb.Net.UWP/UWPUsbDeviceFactory.cs

    0 讨论(0)
  • 2020-12-19 08:36

    In my case, the DeviceInterfaceGUID registry entry for the device was missing, which seems to be required so that the WinUSB device can be found and instantiated.

    I added the entry HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_16D0&PID_0BD7\None\Device Parameters\DeviceInterfaceGUID with a random GUID like {86529001-c433-4530-a578-9a67adf1ffa9} (in my case 16D0 is the vendor ID, 0BD7 the product ID and None the instance). This can be done from the command line for example, by calling 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.

    I am absolutely no expert in USB and have no idea why this is needed or why it was missing; but at least in my case adding a GUID helped, both on my Windows 10 Professional and my Windows 10 IoT Core (Raspberry PI 3B). Maybe see https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/usb-device-specific-registry-settings for more details.

    0 讨论(0)
提交回复
热议问题