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();
        }
    }
}
Clone this wiki locally