USB programming guidance in C#

Discussion in 'Embedded Systems and Microcontrollers' started by ep.hobbyiest, Oct 2, 2014.

  1. ep.hobbyiest

    Thread Starter Member

    Aug 26, 2014
    91
    0
    I have done study of USB in pic controller but i don't know about C# that much. so where i can learn USB program in C#.
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,387
    497
    You are not making sense.
     
  3. josip

    Member

    Mar 6, 2014
    63
    12
    USB inside micro is not related to any programming language, even USB stack on micro side can be written in any.
     
  4. DNA Robotics

    Member

    Jun 13, 2014
    123
    26
  5. Papabravo

    Expert

    Feb 24, 2006
    10,142
    1,790
    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.
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    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.
     
  7. ep.hobbyiest

    Thread Starter Member

    Aug 26, 2014
    91
    0
    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.
     
  8. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    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.
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    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?
     
  10. ep.hobbyiest

    Thread Starter Member

    Aug 26, 2014
    91
    0
    i m using 18f4550 controller in which there is built in USB now i want code in c# to communicate from pc to pic.
     
  11. adam555

    Active Member

    Aug 17, 2013
    858
    39
    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.
     
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    I give up.
     
  13. Papabravo

    Expert

    Feb 24, 2006
    10,142
    1,790
    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?
     
  14. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    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.
     
  15. Papabravo

    Expert

    Feb 24, 2006
    10,142
    1,790
    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
     
  16. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    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.
     
  17. Papabravo

    Expert

    Feb 24, 2006
    10,142
    1,790
    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.
     
  18. ep.hobbyiest

    Thread Starter Member

    Aug 26, 2014
    91
    0
    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.
     
  19. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,387
    497
    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
     
  20. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    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.
     
Loading...