I use the LibUSBDotNet library in a WPF application that communicates with a simple USB device. I send only 3 bytes to the device as a command, no response is expected from it, and my code works like a charm:
MyUsbFinder = new UsbDeviceFinder(vid, pid);
MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
wholeUsbDevice = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
wholeUsbDevice.SetConfiguration(1);
wholeUsbDevice.ClaimInterface(0);
}
writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
And then the process of writing to the device:
byte[] update = { 0x21, some_code_generated_byte, some_code_generated_byte };
int bytesWritten;
if (MyUsbDevice != null)
{
ec = writer.Write(update, 2000, out bytesWritten);
}
I'm relatively new to working with USB, but so far I have worked, and everything works. However, after some time, from time to time, when I try to record the device again, I would get one of the following two errors:
Win32Error:GetOverlappedResult Ep 0x01
31:The parameter is incorrect.
Win32Error:PipeTransferSubmit Ep 0x01
87:The parameter is incorrect.
Often I need to restart the device / application several times before it starts working again, and then works like a charm for a few more hours before it happens again ... I have not been able to repeat the crash so far in the test environment.
, /, , . , , , - , , , 12 , , , - ...
, , , , .
, !
EDIT:
, , , OverlappedResult, , reset . , , , - .
:
UsbDeviceFinder MyUsbFinder;
UsbDevice MyUsbDevice;
IUsbDevice wholeUsbDevice;
MyUsbFinder = new UsbDeviceFinder(vid, pid);
MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
wholeUsbDevice = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
wholeUsbDevice.SetConfiguration(1);
wholeUsbDevice.ClaimInterface(0);
}
writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
. , try/catch:
ErrorCode ec = ErrorCode.None;
if (MyUsbDevice != null)
{
ec = writer.Write(update, 2000, out bytesWritten);
if (ec != ErrorCode.None)
{
writer.Dispose();
if (this.reconnect())
{
ec = writer.Write(update, 2000, out bytesWritten);
if (ec != ErrorCode.None)
{
}
}
}
}
else
{
if (this.reconnect())
updateRelayController();
}
"" - , , updateRelayController - , USB-.
, reconnect(), 99% / USB- ..:
public bool reconnect()
{
if (MyUsbDevice != null)
{
writer.Dispose();
wholeUsbDevice.ReleaseInterface(0);
wholeUsbDevice.Close();
MyUsbDevice.Close();
UsbDevice.Exit();
}
MyUsbFinder = new UsbDeviceFinder(vid, pid);
MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
if (MyUsbDevice == null)
{
return false;
}
else
{
wholeUsbDevice = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
wholeUsbDevice.SetConfiguration(1);
wholeUsbDevice.ClaimInterface(0);
}
writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
return true;
}
}
. SetConfiguration/ClaimInterface/Dispose , OverlappedResult, , , , . , , , , , , , . , - . , , !