Accessing USB Devices from C# Using LibUSBDotNet

1. Download and install SharpDevelop.

2. Download and install the libusbdotnet package.

3. Plug at least one USB device into the computer wait for Windows to install its driver.

4. Start the libusbdotnet Filter Wizard by selecting the item “All Programs ~ libusbdotnet ~ libusb-win32 ~ Filter Wizard” from the Start Menu.

5. In the Filter Wizard, follow the prompts to create a filter for the test USB device added in a previous step.

6. Start SharpDevelop.

7. In SharpDevelop, create a new solution, specify that it will be a “console application”, and name it “USBTest”.

8. On the newly created USBTest project, right-click the References node and add a reference to the file “LibUsbDotNet.dll”, which by default is located in the “/Program Files/LibUsbDotNet” directory.

9. Replace the text in the automatically generated file Program.cs with the following text:

using System;
using LibUsbDotNet;
using LibUsbDotNet.Info;
using LibUsbDotNet.Main;

namespace USBTest
{
	class Program
	{
		public static void Main(string[] args)
		{
			// This code is adapted from a sample found at the URL 
			// http://libusbdotnet.sourceforge.net/V2/html/6ab39c2b-8611-46bf-8066-bfe79ae9f1f9.htm

			// This only finds USB Devices explicitly enabled for LibUSB-Win32
			// via the "Filter Wizard" app installed as part of LibUSBDotNet.

			string devicesAsStringXML = "";
			string newline = "\n";
			string indentSingle = "    ";
			string indentSoFar = "";

			devicesAsStringXML += indentSoFar + "<Devices>" + newline;
			indentSoFar += indentSingle;

			foreach (UsbRegistry registry in UsbDevice.AllDevices)
			{
				UsbDevice device;
				bool wasDeviceOpenedSuccessfully = registry.Open(out device);

				if (wasDeviceOpenedSuccessfully == true)
				{					
					devicesAsStringXML +=
						indentSoFar						
						+ "<Device "
						+ "ProductString='" + device.Info.ProductString + "' "
						+ ">"
						+ newline;

					indentSoFar += indentSingle;
					devicesAsStringXML += indentSoFar + "<Configs>" + newline;
					indentSoFar += indentSingle;
					foreach (UsbConfigInfo config in device.Configs)
					{
						devicesAsStringXML += 
							indentSoFar
							+ "<Config "
							+ "ConfigString='" + config.ConfigString + "' "
							+ ">"
							+ newline;

						indentSoFar += indentSingle;						
						devicesAsStringXML += indentSoFar + "<Interfaces>" + newline;
						indentSoFar += indentSingle;
						foreach(UsbInterfaceInfo interfaceInfo in config.InterfaceInfoList)
						{
							devicesAsStringXML += 
								indentSoFar 
								+ "<Interface "
								+ "EndpointCount='" + interfaceInfo.EndpointInfoList.Count + "' "
								+ ">"
								+ newline;
						}
						indentSoFar = indentSoFar.Substring(0, indentSoFar.Length - indentSingle.Length);
						devicesAsStringXML += indentSoFar + "</Interfaces>" + newline;
						indentSoFar = indentSoFar.Substring(0, indentSoFar.Length - indentSingle.Length);
					}
					indentSoFar = indentSoFar.Substring(0, indentSoFar.Length - indentSingle.Length);
					devicesAsStringXML += indentSoFar + "</Configs>" + newline;
					indentSoFar = indentSoFar.Substring(0, indentSoFar.Length - indentSingle.Length);

					devicesAsStringXML += indentSoFar + "</Device>" + newline;				
				}
				indentSoFar = indentSoFar.Substring(0, indentSoFar.Length - indentSingle.Length);
			}
			devicesAsStringXML += indentSoFar + "</Devices>" + newline;

			Console.WriteLine(devicesAsStringXML);

			Console.ReadKey();
		}
	}
}

10. Press F5 to run the solution. A list of libusb-win32 enabled USB Devices will be created and displayed in a console window.

Advertisements
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

2 Responses to Accessing USB Devices from C# Using LibUSBDotNet

  1. Nguyễn Quý Nhật says:

    Hi!
    I am researching about USB.
    I am using LibUSBDotnet 2.2.8. But My Application only finds USB Devices explicitly enabled for LibUSB-Win32 via the “Filter Wizard” app installed as part of LibUSBDotNet. So, How My Application can find USB devices without use “Filter Wizard”.

  2. Rainer Gerlach says:

    I have the same problem and would be very interested in a solution.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s