Usb.Net is a cross platform USB library for .NET, UWP, and Android. Adapters can be added for platforms such as Linux, or OSX. It puts a wrapper over the OS specific APIs and allows you to write USB code that runs on any of the platforms. Device.Net is a framework for communicating with connected devices such as USB, and Hid devices. Version 3.0 Beta of these libraries have been released, and I’d like to hear your feedback.

GitHub Repo

Major Changes

This is the Github release notes page that lists the issues the were fixed. The main problems with previous versions of Usb.Net were that it was trying to use interrupt pipes for transfer, and the buffer sizes could not be specified. The library has been overhauled so that any if the existing USB interfaces or pipes can be selected, and the buffer sizes can be specified in constructors. This should cover any scenario for connecting to a device.

Using the Library

The best place to get started is with the GitHub wiki. This page is a little out of date and is missing some information about how to set the USB interface, and Endpoint. This documentation is being worked on, and will be added to the wiki soon. Meantime, if you have trouble modifying the sample for your device, please get in contact through the issues page, and I will be happy to help.

using Device.Net;
using System;
using System.Threading.Tasks;

namespace Usb.Net.Windows
{
    public class UsbExample : IDisposable
    {
        private WindowsUsbDevice _WindowsUsbDevice;

        public UsbExample(string deviceId)
        {
            _WindowsUsbDevice = new WindowsUsbDevice(deviceId, new DebugLogger(), new DebugTracer(), 64, 64);
        }

        public async Task InitializeAsync()
        {
            await _WindowsUsbDevice.InitializeAsync();

            //This is not necessary for most cases, but allows you to select an interface and endpoint
           _WindowsUsbDevice.UsbDeviceHandler.ReadUsbInterface = _WindowsUsbDevice.UsbDeviceHandler.UsbInterfaces[1];
           _WindowsUsbDevice.UsbDeviceHandler.ReadUsbInterface.ReadEndpoint = _WindowsUsbDevice.UsbDeviceHandler.ReadUsbInterface.UsbInterfaceEndpoints[1];
           _WindowsUsbDevice.UsbDeviceHandler.WriteUsbInterface = _WindowsUsbDevice.UsbDeviceHandler.UsbInterfaces[2];
          _WindowsUsbDevice.UsbDeviceHandler.WriteUsbInterface.ReadEndpoint = _WindowsUsbDevice.UsbDeviceHandler.WriteUsbInterface.UsbInterfaceEndpoints[1];
       }

       public void Dispose()
       {
           _WindowsUsbDevice.Dispose();
       }
    }
}

The above is a windows sample but the same code will work on any of the platforms.

Grab the NuGet. You will need to add Device.Net manually.

Usb.Net

Hid.Net

Device.Net

Make sure you include pre-release.

Why use Device.Net and Usb.Net?

There are plenty of C# libraries around for USB. The problem is that they are all different across platforms. Most of the code cannot be reused. Usb.Net maintains uniform code across platforms, but also coupled with Hid.Net allows you to communicate with Hid devices in exactly the same way. Bluetooth support is also on its way. The Device.Net framework is much more than a simple library. So, please check it out and help me iron out the kinks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s