Clan x86

Technical (Development, Security, etc.) => General Programming => Topic started by: abc on May 28, 2007, 01:47:25 PM

Title: Using USB Ports w/ C#
Post by: abc on May 28, 2007, 01:47:25 PM
How would I go about, playing with the USB ports with C#? Do I have to use API calls? or does C# have a functionality built in like the Serial Port control.
Title: Re: Using USB Ports w/ C#
Post by: MyndFyre on May 29, 2007, 05:01:34 PM
What are you trying to do with it?  Interface directly with hardware?

You might have luck using #usblib (http://www.icsharpcode.net/OpenSource/SharpUSBLib/default.aspx).
Title: Re: Using USB Ports w/ C#
Post by: abc on May 29, 2007, 07:19:52 PM
Hoping to control a motor with my USB.  :-\
Title: Re: Using USB Ports w/ C#
Post by: Skywing on May 29, 2007, 07:31:16 PM
USB is much more complicated than a plain serial port and typically involves a microcontroller.  If you are doing a DIY electronics project, you would generally be better off with plain RS232.

On Windows, it depends on what you are trying to do.  There is an interface for interacting with collections exposed by HID devices (see the HidD* and HidP* APIs in the WDK documentation or on MSDN online).  However, "raw" USB communication typically involves a driver to manage communication with the USB stack.

(The library MyndFyre links to uses usblib, which is essentially an NT port of a driver that exposes lowlevel USB to user mode.  I would tend to recommend using the native USB stack instead of that for any serious work, although for a one-off it might do the job.)
Title: Re: Using USB Ports w/ C#
Post by: abc on May 30, 2007, 05:29:48 PM
Thanks
Title: Re: Using USB Ports w/ C#
Post by: Skywing on June 04, 2007, 11:48:25 AM
Although not C#, there is now a supported API for interacting with generic USB devices in user mode on modern systems: WinUSB (http://msdn2.microsoft.com/en-us/library/aa476426.aspx).  This would be the recommended approach over writing a kernel mode drivers for most situations.