USB programming guidance in C#

Papabravo

Joined Feb 24, 2006
21,228
I'm not aware of any connection between USB and the programming language C#. That doesn't mean there isn't one somewhere. IMHO C# would not be a very good language to use for the implementation of a USB firmware stack in a PIC microcontroller. The language C, all by itself with no extra punctuation, can be used for a USB stack and the microchip website probably has free example code for various flavors of PIC processors. You could also do it in assembly language.

Do you have a copy of the USB specification, and Jan Axelson's book USB Complete? I recommend both before attacking such a project.
 

ErnieM

Joined Apr 24, 2011
8,377
How you connect to the device depends on what the device looks like: is it a HID (keyboard, mouse) or a CDC (RS-232 or serial device) or a MSC (mass storage like an SD card or flash drive) or some other standard or custom device.

Each has it's own method to connect.
 

Thread Starter

ep.hobbyiest

Joined Aug 26, 2014
201
USB study in PIC means usb protocols and how to use with that controller.
and i mean there is any library or dll file that can easily add and start to communicate with PIC.
 

sirch2

Joined Jan 21, 2013
1,037
Do you want to communicate from a PIC to a PC over USB with a C# program running on the PC? If so, one approach is to use a virtual serial port driver on the PC and then your C# program can read the USB as a virtual serial port.
 

ErnieM

Joined Apr 24, 2011
8,377
USB study in PIC means usb protocols and how to use with that controller.
and i mean there is any library or dll file that can easily add and start to communicate with PIC.
The depends on what kind of USB device the PIC looks like. Many devices already have build in methods in Windows.

What are you trying to connect to? PLEASE don't say a PIC, what kind of USB device is the PIC programmed to be?
 

adam555

Joined Aug 17, 2013
858
I've also been looking to learn USB programming with a PIC18F4550. Found some libraries and samples with source code for the PIC (e.g. MCHPFSUSB and MLA); but couldn't find any good manual or tutorial.

Anyway, some of the MLA examples come with the Visual C# source code -as well as Visual C++ and Visual Basic-; maybe you find them useful.
 

Papabravo

Joined Feb 24, 2006
21,228
A PIC18F4550 has the hardware to implement a USB device, but without some programming inside the the chip that you create using assembly language or the the C18 compiler it will sit there as dumb as a post when you connect to it with your PC. AFAIK the is no C# compiler for the part or any PIC for that matter. In Visual Studio you can no doubt tell some USB Object you want to talk to a USB device but if that device does not respond correctly to the enumeration process you will have to manually use regedit to clean up your registry before Windows will allow you to try again. Did you get the USB specification, Jan Axelson's book, and your Beagle analyzer yet? If not, WTH are you waiting for?
 

sirch2

Joined Jan 21, 2013
1,037
AFAIK there is no "USB Object" in .NET USB devices always appear as a particular device (mass storeage, camera, printer, mouse, serial port). The OP either needs to write a device driver or use an available driver and make his PIC emulate that kind of device.
 

Papabravo

Joined Feb 24, 2006
21,228
AFAIK there is no "USB Object" in .NET USB devices always appear as a particular device (mass storeage, camera, printer, mouse, serial port). The OP either needs to write a device driver or use an available driver and make his PIC emulate that kind of device.
OK so maybe there is a wrapper for calls to system functions like "CreateFile" used with serial ports. I've never written a C# program, but my friends and associates have and there is definitely some mechanism for doing it. If a C# program can get to Win ring 0 then I'm sure it can get to a USB device
 

sirch2

Joined Jan 21, 2013
1,037
Well I write lots of C# but that's besides the point, the point I was making is that the USB needs a driver on the end of it and that driver "emulates" a device. Typically when talking to MCUs people use a driver which emulates a serial port and so the C# program would just use the standard serial port library to send and receive data.

A common USB driver is the Human Input Device (HID) driver that handles keyboards, mice,etc. and the OP may be very interested in this - http://www.codeproject.com/Tips/530836/Csharp-USB-HID-Interface which shows exactly how to connect a PIC and have it recognised as a USB-HID.
 

Papabravo

Joined Feb 24, 2006
21,228
Well I write lots of C# but that's besides the point, the point I was making is that the USB needs a driver on the end of it and that driver "emulates" a device. Typically when talking to MCUs people use a driver which emulates a serial port and so the C# program would just use the standard serial port library to send and receive data.

A common USB driver is the Human Input Device (HID) driver that handles keyboards, mice,etc. and the OP may be very interested in this - http://www.codeproject.com/Tips/530836/Csharp-USB-HID-Interface which shows exactly how to connect a PIC and have it recognised as a USB-HID.
Sure, that makes sense. I was thinking in terms of HID, serial port emulation, and mass storage devices. We used a USB "mass storage device" to do firmware updates from the PC to serial Flash Memories on the peripheral device which measured parts being fabricated in grinding machines. It used the HID device to report measurements, just like a USB mouse would do.
 

Thread Starter

ep.hobbyiest

Joined Aug 26, 2014
201
i think there is confusion
and may be i fail to explain well.
i have done usb code sample for controller so from controller side USB is OK. now my problem is other end that is C# .dll files.
 

shteii01

Joined Feb 19, 2010
4,644
i think there is confusion
and may be i fail to explain well.
i have done usb code sample for controller so from controller side USB is OK. now my problem is other end that is C# .dll files.
Generally speaking, people let Windows use generic HID driver to communicate with PIC over USB.

It seems to me that all you have to do is tell your program to use the Windows HID driver.
Here M$ example and stuff: http://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn263133.aspx
 

ErnieM

Joined Apr 24, 2011
8,377
i think there is confusion
and may be i fail to explain well.
i have done usb code sample for controller so from controller side USB is OK. now my problem is other end that is C# .dll files.
The only confusion is your continued refusal to identify what your device is doing.

Without such all anyone can do is guess what needs be done on the PC side.
 
Top