Problem using LibUsnDotNet C# USB Library
Chuck Gannon <[email protected]>
| Newsgroups | gmane.comp.lib.libusb.devel.windows |
|---|---|
| Message-ID | <FE46BB7E13311448B8FEE9CC1B49E648373A497B@MXEX2010.minnetronix.com> |
If I disconnect our USB device and then quickly reconnect, then wait a few seconds, I get an exception in OverlappedTransferContext on line 97:
bSuccess = EndpointBase.mUsbApi.GetOverlappedResult(EndpointBase.Handle, Overlapped.GlobalOverlapped, out transferredCount, true);
It is always for the UsbEndPointWriter and the EndPointBaseHandle is already closed. I have attempted this with r113 and I get the same results.
So I am suspecting I am not using this interface correctly.
Our code does the following to open our device:
private bool OpenDevice()
{
int usbDeviceIndex = -1;
bool usbDeviceFound = false;
bool bRtn = false;
_mRegDevices = UsbDevice.AllDevices;
foreach (UsbRegistry regDevice in _mRegDevices)
{
usbDeviceIndex++;
if ((regDevice.Vid == _bandVid) && (regDevice.Pid == _bandPid))
{
usbDeviceFound = true;
break;
}
}
if (usbDeviceFound)
{
if (_mRegDevices[usbDeviceIndex].Open(out _mUsbDevice))
{
IUsbDevice wholeUsbDevice = _mUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
wholeUsbDevice.SetConfiguration(1);
wholeUsbDevice.ClaimInterface(0);
}
_mEpReader = _mUsbDevice.OpenEndpointReader((ReadEndpointID)(_readEndpointID));
_mEpWriter = _mUsbDevice.OpenEndpointWriter((WriteEndpointID)(_writeEndpointID));
_mEpReader.Flush();
bRtn = true;
}
}
return bRtn;
}
Then we use the Write method (on the UsbEndPointWriter) to send a request for data followed by a Read (on the UsbEndPointReader) to read back data
(there are 3 different data messages we can read back)
private ErrRtn PollForData(out ErrorCode usbErr)
{
ErrRtn error = ErrRtn.ERROK;
int bytesReadCount;
int uiTransmitted;
byte[] bytesRead = new byte[structsize];
usbErr = _mEpWriter.Write(_usbGetJamCmd, _usbWriteTimeOutMs, out uiTransmitted);
if (usbErr == ErrorCode.None)
{
usbErr = _mEpReader.Read(bytesRead, _usbReadTimeOutMs, out bytesReadCount);
if (usbErr == ErrorCode.None)
{
// process data
}
else if (usbErr == ErrorCode.IoTimedOut)
{
DoCommFailed(usbErr);
}
else if (usbErr == ErrorCode.Win32Error)
{
UsbDisconnected();
}
else
{
UsbError(usbErr);
}
}
else if (usbErr == ErrorCode.IoTimedOut)
{
DoCommFailed(usbErr);
}
else if (usbErr == ErrorCode.Win32Error)
{
UsbDisconnected();
}
else
{
UsbError(usbErr);
}
return error;
}
This code is running on timer and continues to send Write/Read requests as long as the USB device is connected.
If a disconnect is detected, the device is closed and the timer then keeps checking (~every second) to see if the device has
become available again.
So, should we be using Write followed by Read, or is there any other cleanup that should occur when we
detect that the USB device is no longer connected. Thanks.
------------------------------------------------------------------------------
Start Your Social Network Today - Download eXo Platform
Build your Enterprise Intranet with eXo Platform Software
Java Based Open Source Intranet - Social, Extensible, Cloud Ready
Get Started Now And Turn Your Intranet Into A Collaboration Platform
http://p.sf.net/sfu/ExoPlatform
_______________________________________________
Libusb-win32-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libusb-win32-devel