# Sending data C# to PIC by using USB

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;
usbHidPort1.SpecifiedDevice.SendData(my_buffer);
}


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?

• 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? – PeterJ Sep 4 '14 at 11:27
• 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 – ceng Sep 4 '14 at 11:31
• 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. – ceng Sep 4 '14 at 11:34
• Firstly I should solve usbHidPort1.SpecifiedDevice value must not be null. – ceng Sep 4 '14 at 11:35
• 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. – 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.

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.

• 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) – Ben Voigt Jun 17 '15 at 16:33