Hey everyone.
I'm trying to communicate with my hid device using C# library UsbLibDotNet which is recommended here.
But I couldn't make the examples for. it always fire "Device not Found". However, I run the very first examples that show all hid devices information and it shows my device too.
I couldn't understand that. The example code is below...
using System;
using System.Text;
using LibUsbDotNet;
using LibUsbDotNet.Main;
namespace Examples
{
internal class ReadPolling
{
public static UsbDevice MyUsbDevice;
#region SET YOUR USB Vendor and Product ID!
public static UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(1234, 1234);
#endregion
public static void Main(string[] args)
{
ErrorCode ec = ErrorCode.None;
try
{
MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
if (MyUsbDevice == null) throw new Exception("Device Not Found.");
IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
wholeUsbDevice.SetConfiguration(1);
wholeUsbDevice.ClaimInterface(0);
}
UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
byte[] readBuffer = new byte[1024];
while (ec == ErrorCode.None)
{
int bytesRead;
ec = reader.Read(readBuffer, 5000, out bytesRead);
if (bytesRead == 0) throw new Exception(string.Format("{0}:No more bytes!", ec));
Console.WriteLine("{0} bytes read", bytesRead);
Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead));
}
Console.WriteLine("\r\nDone!\r\n");
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
}
finally
{
if (MyUsbDevice != null)
{
if (MyUsbDevice.IsOpen)
{
IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
wholeUsbDevice.ReleaseInterface(0);
}
MyUsbDevice.Close();
}
MyUsbDevice = null;
UsbDevice.Exit();
}
Console.ReadKey();
}
}
}
}
And my descriptor file for my device...
<pre>
const unsigned int USB_VENDOR_ID = 0x1234;
const unsigned int USB_PRODUCT_ID = 0x1234;
const char USB_SELF_POWER = 0x80;
const char USB_MAX_POWER = 50;
const char HID_INPUT_REPORT_BYTES = 64;
const char HID_OUTPUT_REPORT_BYTES = 64;
const char USB_TRANSFER_TYPE = 0x03;
const char EP_IN_INTERVAL = 1;
const char EP_OUT_INTERVAL = 1;
const char USB_INTERRUPT = 1;
const char USB_HID_EP = 1;
const char USB_HID_RPT_SIZE = 33;
const struct {
char bLength;
char bDescriptorType;
unsigned int bcdUSB;
char bDeviceClass;
char bDeviceSubClass;
char bDeviceProtocol;
char bMaxPacketSize0;
unsigned int idVendor;
unsigned int idProduct;
unsigned int bcdDevice;
char iManufacturer;
char iProduct;
char iSerialNumber;
char bNumConfigurations;
} device_dsc = {
0x12,
0x01,
0x0200,
0x00,
0x00,
0x00,
8,
USB_VENDOR_ID,
USB_PRODUCT_ID,
0x0001,
0x01,
0x02,
0x00,
0x01
};
const char configDescriptor1[]= {
0x09,
0x02,
0x29,0x00,
1,
1,
0,
USB_SELF_POWER,
USB_MAX_POWER,
0x09,
0x04,
0,
0,
2,
0x03,
0,
}
Anyone that works with this library before could help maybe?
My Best Regards...