Hello...
I am using C#.NET 2.0 and #usblib(on WinXP SP2) and I want to read some
buffers from USB.
After I found the device and open it :
public Device OpenDevice()
{
return new Device(this);
}
//.....
internal Device(Descriptor descriptor)
{
this.descriptor = descriptor;
this.deviceHandle = NativeMethods.usb_open(descriptor.NativeDevice);
if (this.deviceHandle == IntPtr.Zero) {
throw new UsbException("Can't open device.");
}
int rc = NativeMethods.usb_set_configuration(deviceHandle, 1);
if (rc < 0) {
throw new MethodCallUsbException("usb_set_configuration", rc);
}
rc = NativeMethods.usb_claim_interface(deviceHandle, 0);
if (rc < 0) {
throw new MethodCallUsbException("usb_claim_interface", rc);
}
rc = NativeMethods.usb_set_altinterface(deviceHandle, 0);
if (rc < 0)
{
throw new MethodCallUsbException("usb_set_altinterface", rc);
}
}
I start to read from it :
byte read = new byte[1];
int noBytes = this.device.BulkRead(0x82, read);
where :
public int BulkRead(int endpoint, byte[] bytes)
{
// byte i = this.descriptor.NativeDevice.GetConfig(1).Interface.Altsetting.Endpoint.bEndpointAddress;
// when I use the line above, I get: Attempted to read or write protected
memory.
// This is often an indication that other memory is corrupt.
CheckDeviceOpen();
return NativeMethods.usb_bulk_read(deviceHandle, endpoint, bytes, timeout);
}
//in NativeMethods class
const CallingConvention CALLING_CONVENTION = CallingConvention.Cdecl;
const string LIBUSB_NATIVE_LIBRARY = "libusb0.dll";
[DllImport(LIBUSB_NATIVE_LIBRARY, CallingConvention = CALLING_CONVENTION,
ExactSpelling = true), SuppressUnmanagedCodeSecurity]
public static extern int usb_bulk_read(IntPtr dev, int ep, byte[] bytes, int
size, int timeout);
public static int usb_bulk_read(IntPtr dev, int ep, byte[] bytes, int
timeout)
{
return usb_bulk_read(dev, ep, bytes, bytes == null ? 0 : bytes.Length,
timeout);
}
I get -116 error if I use 0x82 as endpoint and if I use 0 or 1, I get -22.
Can you help me with that?
Thanks.
|