Skip to content

nanoframework/System.Device.UsbClient

Repository files navigation

Quality Gate Status Reliability Rating NuGet #yourfirstpr Discord

nanoFramework logo


Welcome to the .NET nanoFramework USB Client repository

Build status

Component Build Status NuGet Package
System.Device.UsbStream Build Status NuGet

Usage

USB Stream

UsbStream class provides a seamless interface to a stream that can read from and write to an USB Device that's enumerated as a WinUSB device. This allows shipping your .NET nanoFramework device as an USB device without the need for any INF file or specific driver instalation.

Creating an UsbStream

Creating an UsbStream requries 2 parameters: a Guid that will be used as the Device Interface ID and a string which will be used as the device description for the USB device.

private static Guid deviceInterfaceId = new Guid("9e48651c-fa68-4b39-8731-1ee84659aac5");
private static string deviceDescription = "nanoDevice";

// create USB Stream
var usbStream = UsbClient.CreateUsbStream(deviceInterfaceId, deviceDescription);

Writing to the UsbStream

To write to the UsbStream just call the Write() method just like any other .NET stream. Like this:

// buffer with dummy data 
var bufer = new byte[] { 1, 2, 3 };

usbStream.Write(bufer, 0, bufer.Length);

Debug hints

USB can be hard. Be prepared for that! There are a number of issues that are prone to cause frustration. Follows some (hopefully valuable) advice.

  • If you need to debug device enumeration issues and check what's being passed from the devices, install a tool such as USB Device Tree Viewer from Uwe Sieber. With it you can peruse into every bit of detail about USB device, their interfaces, end points, strings, etc.

  • Another great tool is USBDeview from NirSoft. This tool lists all USB devices that currently connected to your computer, as well as all USB devices that you previously used. Extended information is displayed for each USB device. It's possible to uninstall/disable and enable USB devices from the tool.

  • Windows caches enumeration of USB devices. What's the problem with that? During development, if the enumeration fails at some point, the device most likely will be marked as being enumerated and on the next connection Windows won't do it again. This can cause the enumeration data to be wrong or incomplete. To fix this and truly force the enumeration to happen from scratch, make sure that:

    1. Delete the device from the Device Manager.

    2. Delete the entry from the enumeration cache in Registry. This lives at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\usbflags\vvvvpppprrrr

      Where vvvv is the vendor id, pppp is the PID and rrrr is the device release number.

Feedback and documentation

For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.

Join our Discord community here.

Credits

The list of contributors to this project can be found at CONTRIBUTORS.

License

The nanoFramework Class Libraries are licensed under the MIT license.

Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. For more information see the .NET Foundation Code of Conduct.

.NET Foundation

This project is supported by the .NET Foundation.