View on GitHub

Devendra's Log

Communicate with USB device in C# using LibUsbDotNet

LibUsbDotNet is a cross-platform library. The source code below has been tested on Mac OS X (version 10.6.8 to be precise) using the Mono .NET run-time. On Linux and Mac LibUsbDotNet requires libusb.

On Mac OS X, you’ll need to obtain and build libusb from source. Ensure you have downloaded and installed the developer SDK for Mac. To build and install it, execute the following commands from a terminal, in the folder that contains the source of libusb.

./configure CC="gcc -m32" --prefix=/usr
sudo make install

The following source code snippet is an example of how to send data to a device. The code itself is a heavily modified version of the Read.Write example that ships with LibUsbDotNet.

Here’s a sample output produced by the code.

IoTimedOut:No more bytes!

The same code should run on Windows and Linux. For more details see the documentation page of LibUSBDotNet. They have a really nice wizard that can create an INF and installer package for Windows.

If Mono raises a System.DllNotFoundException, you might want to take a look at this page.