I want to send data, C# to PIC by using USB (UsbLibrary.dll). I am using below codes for sending data;

private void button1_Click(object sender, EventArgs e)
            byte[] my_buffer = new byte[64];
            my_buffer[1] = (byte)0x01;

But I got error as usbHidPort1.SpecifiedDevice value is null (which means that Connected PIC Device is not identified by C#) although vendor and product id coded same in C# and CCS C (PIC). How can send data C# to PIC by using UsbLibrary?

  • 4
    \$\begingroup\$ It's probably worth adding a link to where UsbLibrary.dll comes from as I'm pretty sure it's not a standard .NET library. Also are you sure it enumerates OK - can you find the VID / PID under device manager? \$\endgroup\$ – PeterJ Sep 4 '14 at 11:27
  • \$\begingroup\$ I added dll file as a reference in C#. Then I defined product and vendor id as 1111. I could send data PIC to C#, although changed vendor and product id. but i could not send data C# to PIC by using UsbLibrary.dll \$\endgroup\$ – ceng Sep 4 '14 at 11:31
  • \$\begingroup\$ I also check vendor and product id in c#, although vendor and product id coded same in c# and ccs c, textbox says to me connected device is not your device. Interestingly, coming data to c# which are coming from my PIC. \$\endgroup\$ – ceng Sep 4 '14 at 11:34
  • \$\begingroup\$ Firstly I should solve usbHidPort1.SpecifiedDevice value must not be null. \$\endgroup\$ – ceng Sep 4 '14 at 11:35
  • \$\begingroup\$ C# is not a communication profile. It is the OS that manages device communication and resolution. The scenario you describe simply shows a problem with the device, most likely. Ìt can be in te USB stack, the USB descriptor, or the firmware you wrote. It can be rough getting this stuff working. \$\endgroup\$ – Scott Seidman Jun 17 '15 at 10:25

Easiest way to send data from PC to your PIC device is using serial communication over USB.

  • Make your PIC act as USB CDC device.
  • To be recognized by OS and to provice a virtual COM port,
    • use a VID/PID combination that is used by some device that uses such serial over USB communcation and thus the standard serial driver
    • or add a custom driver (.inf file in Windows) for your device that then uses the standard serial driver (usbser.sys in Windows). Examples of such .inf file can be found in Microchip Application Libraries.
  • 'Talk' to the device via the virtual COM port.
    • Use a serial console application (for example PuTTY) for manual testing.
    • Echo strings from console like echo hello pic >COM6.
    • Echo strings from an application/plugin/script/whatever to the virtual COM port.
| improve this answer | |

The suggestion by Mape is fair, but has a clear disadvantage over the HID approach that you're already taking, namely the need for a device driver.

You have provided way too little information to tell precisely why your setup is not working, but the easiest method to get to a working end solution is to start from a working base setup and iteratively expand/customize it to your needs. This can be a tricky and tedious progress. You could consider following an approach as below.

  1. Start with a valid HID firmware. The Microchip MLA [1] has numerous examples for a lot of device families. The example in \apps\usb\device\hid_custom likely either supports your chip, or is easily adapted to support it. CCS may offer a similar example. Once you get it to compile, make sure the device enumerates properly (shows up in device manager, without any fault codes!).

  2. Make sure you can communicate with the device. The example comes with a C++/CLI project. Get this to work before moving on.

  3. Communicate with the HID device using your library of choice, i.e. your UsbLibrary.dll. Reading from HID devices is not hard, but libraries can ease the pain of device enumeration. Personally I've successfully used 'HidLibrary' by Mike O'Brien in the past.

  4. Now you can begin to customize the firmware. Keep your report descriptors as simple as possible and test your C# counterpart after every change to minimize your headaches. If the device no longer works, dump your device descriptor with a tool like [2] and carefully go over your changes.

[1] http://www.microchip.com/pagehandler/en-us/devtools/mla/home.html

[2] http://www.thesycon.de/eng/usb_descriptordumper.shtml

| improve this answer | |
  • 1
    \$\begingroup\$ Both HID and CDC are standard profiles, with drivers provided by the OS. (Not all USB-serial converters use CDC profile, but Mape's recommendation does) \$\endgroup\$ – Ben Voigt Jun 17 '15 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.