Skip to content

USB Initialization: Interfaces And Endpoints

Christian Findlay edited this page Aug 3, 2019 · 2 revisions

USB devices have one or more "Interfaces". Each interface can have one or more "Endpoints" and are sometimes called "Pipes". When some devices are initialized, these will have reasonable defaults. However, for some devices, you will need to select the interface and/or endpoint. To do this, first call InitializeAsync. This will populate the interfaces and endpoints. Then set the interface and endpoints as necessary.

This is a Windows code example, but the principle is the same for any platform:

using Device.Net;
using System;
using System.Linq;
using System.Threading.Tasks;
using Usb.Net.Windows;

namespace Usb.Net.WindowsSample
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Go().Wait();
        }

        private static async Task Go()
        {
            var logger = new DebugLogger();
            var tracer = new DebugTracer();

            //This is the only platform specific part. Each platform has a UsbInterfaceManager
            var usbInterfaceManager = new WindowsUsbInterfaceManager
            (
                @"\\?\usb#vid_1209&pid_53c1&mi_00#6&1b4d0e06&0&0000#{dee824ef-729b-4a0e-9c14-b7117d33a817}",
                logger,
                tracer,
                null,
                null
            );

            var usbDevice = new UsbDevice(usbInterfaceManager, logger, tracer);

            await usbDevice.InitializeAsync();

            foreach (var usbInterface in usbDevice.UsbInterfaceManager.UsbInterfaces)
            {
                Console.WriteLine($"Interface Number: {usbInterface.InterfaceNumber}");

                foreach (var usbEndpoint in usbInterface.UsbInterfaceEndpoints)
                {
                    Console.WriteLine($"Endpoint PipeId: {usbEndpoint.PipeId} Is Interrupt: {usbEndpoint.IsInterrupt} IsRead: {usbEndpoint.IsRead} IsWrite: {usbEndpoint.IsWrite}");
                }
            }

            var firstUsbInterface = usbDevice.UsbInterfaceManager.UsbInterfaces.First();

            //Set the read interface to the first one in the list
            usbInterfaceManager.ReadUsbInterface = firstUsbInterface;

            //Set the write interface to the first one in the list
            usbInterfaceManager.WriteUsbInterface = firstUsbInterface;

            //Set the read endpoint to the first endpoint with read capability
            firstUsbInterface.ReadEndpoint = firstUsbInterface.UsbInterfaceEndpoints.First(e => e.IsRead);

            //Set the write endpoint to the first endpoint with write capability
            firstUsbInterface.WriteEndpoint = firstUsbInterface.UsbInterfaceEndpoints.First(e => e.IsWrite);

            //Create the buffer
            var writeBuffer = new byte[64];
            writeBuffer[0] = 0x3f;
            writeBuffer[1] = 0x23;
            writeBuffer[2] = 0x23;

            //Write the data to the device
            var response = await usbDevice.WriteAndReadAsync(writeBuffer);

            Console.WriteLine($"Result: {string.Join(" ", response.Data)}");

            Console.ReadLine();
        }
    }
}

Output:

Interface Number: 0 Endpoint PipeId: 129 Is Interrupt: True IsRead: True IsWrite: False Endpoint PipeId: 1 Is Interrupt: True IsRead: False IsWrite: True Result: 63 35 35 0 17 0 0 0 131 10 9 116 114 101 122 111 114 46 105 111 16 1 24 8 32 0 50 24 51 66 69 65 55 66 50 55 50 55 66 49 55 57 50 52 67 56 67 70 68 56 53 48 56 1 64 0 82 5 66 108 97 99 107 96

Note: the above output is a little odd. The endpoints for this device are Interrupt endpoints, and there are no bulk endpoints. See below.

Bulk Vs. Interrupt Endpoints

Bulk endpoints are generally used for normal data transfers - especially when there is large amounts of data to be transferred. Interrupt endpoints are supposed to be used for small transfers and are not supposed to be triggered by a write. However, some devices use the interrupt endpoint for read/write transfer.

Here is a more technical explanation of USB data transfer types.