UsbDevice UsbDevice UsbDevice UsbDevice UsbDevice Class

Definition

Represents a USB device. The object provides methods and properties that an app can use to find the device (in the system) with which the app wants to communicate, and sends IN and OUT control transfers to the device.

public : sealed class UsbDevice : IUsbDevice, IClosable
struct winrt::Windows::Devices::Usb::UsbDevice : IUsbDevice, IClosable
public sealed class UsbDevice : IUsbDevice, IDisposable
Public NotInheritable Class UsbDevice Implements IUsbDevice, IDisposable
// This class does not provide a public constructor.
Attributes

Windows 10 requirements

Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Examples

This example code shows how to get the UsbDevice object by specifying the vendor/product Id or the device interface GUID.

protected override async void OnLaunched1(LaunchActivatedEventArgs args)
{
    UInt32 vid = 0x045E;
    UInt32 pid = 0x078F;

    string aqs = UsbDevice.GetDeviceSelector(vid, pid);

    var myDevices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(aqs, null);

    if (myDevices.Count == 0)
    {
        ShowError("Device not found!");
        return;
    }

    UsbDevice device = await UsbDevice.FromIdAsync(myDevices[0].Id);

   // Send a control transfer. 

   UsbSetupPacket initSetupPacket = new UsbSetupPacket() 
   { 
       Request = initRequest,
       RequestType = new UsbControlRequestType() { 

           Recipient = UsbControlRecipient.DefaultInterface,

           ControlTransferType = UsbControlTransferType.Vendor 
       } 
   };

   await device.SendOutControlTransferAsync(initSetupPacket);

}

Remarks

Before getting a reference to the UsbDevice object, you must have one of these identifiers:

  • The vendor and product identifiers for the physical device. Those identifiers are part of the hardware ID string. You can obtain that string from the Models section in the INF file. Alternatively, if WinUSB was loaded based on the compatible ID specified in the device descriptor, you can derive the identifiers from the Hardware Ids property in Device Manager. For example, if Hardware Id is "USB\VID_045E&PID_078E", vendor ID is "0x045E" and product Id is "0x078E".
  • The device interface GUID. You can obtain that GUID from the DeviceInterfaceGuids registry entry under this key:HKEY_LOCAL_MACHINE*System**CurrentControlSet**Enum**USB**<Device Identifier>**<Instance Identifier>**Device Parameters*
  • The device's class, subclass, and protocol codes. You can obtain that information from the CompatibleIds registry entry, found under the Device Parameters key.

To get the UsbDevice object:

  1. Get the Advanced Query Syntax (AQS) string that contains search criteria for finding the device in the enumerated device collection. If you want to search by the vendor ID/product ID or the device interface GUID, call GetDeviceSelector. If you want to search by the device class, call GetDeviceClassSelector. Both calls retrieve formatted AQS strings.
  2. Pass the retrieved string to FindAllAsync. The call retrieves a DeviceInformationCollection object.
  3. Loop through the collection. Each iteration gets a DeviceInformation object.
  4. Get the DeviceInformation.Id property value. The string value is the device instance path. For example, "\\?\USB#VID_045E&PID_078F#6&1b8ff026&0&5#{dee824ef-729b-4a0e-9c14-b7117d33a817}".
  5. Call FromIdAsync by passing the device instance string and get the UsbDevice object. You can then use the UsbDevice object to perform other operations, such as sending a control transfer. When the app has finished using the UsbDevice object, the app must release it by calling Close.

Properties

Configuration Configuration Configuration Configuration Configuration

Gets an object that represents a USB configuration including all interfaces and their endpoints.

DefaultInterface DefaultInterface DefaultInterface DefaultInterface DefaultInterface

Gets the object that represents the default or the first interface in a USB configuration.

DeviceDescriptor DeviceDescriptor DeviceDescriptor DeviceDescriptor DeviceDescriptor

Gets the object that represents the USB device descriptor.

Methods

Close() Close() Close() Close() Close()

Releases the reference to the UsbDevice object that was previously obtained by calling FromIdAsync.

Dispose() Dispose() Dispose() Dispose() Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

FromIdAsync(String) FromIdAsync(String) FromIdAsync(String) FromIdAsync(String) FromIdAsync(String)

Starts an asynchronous operation that creates a UsbDevice object.

GetDeviceClassSelector(UsbDeviceClass) GetDeviceClassSelector(UsbDeviceClass) GetDeviceClassSelector(UsbDeviceClass) GetDeviceClassSelector(UsbDeviceClass) GetDeviceClassSelector(UsbDeviceClass)

Gets an Advanced Query Syntax (AQS) string that the app can pass to DeviceInformation.FindAllAsync in order to find a specific type of USB device.

GetDeviceSelector(Guid) GetDeviceSelector(Guid) GetDeviceSelector(Guid) GetDeviceSelector(Guid) GetDeviceSelector(Guid)

Gets an Advanced Query Syntax (AQS) string, based on the device interface GUID identifier, specified by the app. The app passes the string to DeviceInformation.FindAllAsync in order to find a specific type of USB device.

GetDeviceSelector(UInt32, UInt32) GetDeviceSelector(UInt32, UInt32) GetDeviceSelector(UInt32, UInt32) GetDeviceSelector(UInt32, UInt32) GetDeviceSelector(UInt32, UInt32)

Gets an Advanced Query Syntax (AQS) string, based on vendor and product identifiers, specified by the app. The app passes the string to DeviceInformation.FindAllAsync in order to find a specific type of USB device.

GetDeviceSelector(UInt32, UInt32, Guid) GetDeviceSelector(UInt32, UInt32, Guid) GetDeviceSelector(UInt32, UInt32, Guid) GetDeviceSelector(UInt32, UInt32, Guid) GetDeviceSelector(UInt32, UInt32, Guid)

Gets an Advanced Query Syntax (AQS) string, based on vendor, product, and device interface GUID identifiers, specified by the app. The app passes the string to DeviceInformation.FindAllAsync in order to find a specific type of USB device.

SendControlInTransferAsync(UsbSetupPacket) SendControlInTransferAsync(UsbSetupPacket) SendControlInTransferAsync(UsbSetupPacket) SendControlInTransferAsync(UsbSetupPacket) SendControlInTransferAsync(UsbSetupPacket)

Starts a zero-length USB control transfer that reads from the default control endpoint of the device.

SendControlInTransferAsync(UsbSetupPacket, IBuffer) SendControlInTransferAsync(UsbSetupPacket, IBuffer) SendControlInTransferAsync(UsbSetupPacket, IBuffer) SendControlInTransferAsync(UsbSetupPacket, IBuffer) SendControlInTransferAsync(UsbSetupPacket, IBuffer)

Starts a USB control transfer to receive data from the default control endpoint of the device.

SendControlOutTransferAsync(UsbSetupPacket) SendControlOutTransferAsync(UsbSetupPacket) SendControlOutTransferAsync(UsbSetupPacket) SendControlOutTransferAsync(UsbSetupPacket) SendControlOutTransferAsync(UsbSetupPacket)

Starts a zero-length USB control transfer that writes to the default control endpoint of the device.

SendControlOutTransferAsync(UsbSetupPacket, IBuffer) SendControlOutTransferAsync(UsbSetupPacket, IBuffer) SendControlOutTransferAsync(UsbSetupPacket, IBuffer) SendControlOutTransferAsync(UsbSetupPacket, IBuffer) SendControlOutTransferAsync(UsbSetupPacket, IBuffer)

Starts a USB control transfer to send data to the default control endpoint of the device.

See Also