I am sending a command to a QR Code reader every 30 milliseconds and I take the response, and display its size on a label. Everything seems to be working fine until I press the stop button, after which I start hearing the windows usb connect/disconnect sound continuously. The sound goes away if I unplug the USB device and plug it in again. What can possibly be going wrong ?
public ReaderForm()
{
InitializeComponent();
}
private void ReaderForm_Load(object sender, EventArgs e)
{
_timer = new System.Windows.Forms.Timer();
_timer.Interval = 270;
_timer.Tick += new EventHandler(_timer_Tick);
}
private void ConnectButton_Click(object sender, EventArgs e)
{
_errorCode = ErrorCode.None;
_myUsbFinder = new UsbDeviceFinder(0x0c2e, 0x0b6a);
// Find and open the usb device.
_myUsbDevice = UsbDevice.OpenUsbDevice(_myUsbFinder);
// If the device is open and ready
if (_myUsbDevice == null) throw new Exception("Device Not Found.");
IUsbDevice wholeUsbDevice = _myUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
// Select config #1
wholeUsbDevice.SetConfiguration(1);
// Claim interface #0.
wholeUsbDevice.ClaimInterface(2);
}
_writer = _myUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep07);
_reader = _myUsbDevice.OpenEndpointReader(ReadEndpointID.Ep02);
}
private void StartButton_Click(object sender, EventArgs e)
{
_timer.Interval = Convert.ToInt32(speedTextBox.Text);
_timer.Start();
}
private void StopButton_Click(object sender, EventArgs e)
{
if (_myUsbDevice != null)
{
if (_myUsbDevice.IsOpen)
{
IUsbDevice wholeUsbDevice = _myUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
// Release interface #2.
wholeUsbDevice.ReleaseInterface(2);
}
_myUsbDevice.Close();
}
}
_myUsbDevice = null;
// Free usb resources
UsbDevice.Exit();
_timer.Stop();
}
private void _timer_Tick(object sender, EventArgs e)
{
try
{
if (!String.IsNullOrEmpty(cmdLine))
{
int bytesWritten;
_errorCode = _writer.Write(Encoding.Default.GetBytes(cmdLine), 0, cmdLine.Length, 40, out bytesWritten);
Console.WriteLine("Bytes Written: {0} ", bytesWritten, _errorCode);
if (_errorCode != ErrorCode.None) throw new Exception(UsbDevice.LastErrorString);
_reader.DataReceived += (_onRxEndPointData);
_reader.DataReceivedEnabled = true;
label1.Text = _findImageSize().ToString();
Response = "";
// Always disable and unhook event when done.
_reader.DataReceivedEnabled = false;
_reader.DataReceived -= (_onRxEndPointData);
Console.WriteLine("\r\nDone!\r\n");
}
else
throw new Exception("Nothing to do.");
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine((_errorCode != ErrorCode.None ? _errorCode + ":" : String.Empty) + ex.StackTrace);
}
finally
{
}
}
private static void _onRxEndPointData(object sender, EndpointDataEventArgs e)
{
Response += Encoding.Default.GetString(e.Buffer, 0, e.Count);
}
public static int _findImageSize()
{
int imageSizeInBytes = 0;
byte[] responseBytes = Encoding.Default.GetBytes(Response);
for (int i = 0; i < responseBytes.Length; i++)
{
if (responseBytes[i] == 0x1d)//Find the start of the image data
{
i++;
imageSizeInBytes = responseBytes.Length - i;
break;
}
}
return imageSizeInBytes;
}
[UPDATES] I have tried several different implementations now, using async, the winusb library and I've tried flushing the end points and aborting them, and countless other approaches for the past 3 days. In short, I've tried everything possible and the problem is continuing. I have decided the only way to solve the problem is to emulate the behavior of me removing the device from the USB port and plugging it in again. Is it possible for me to somehow identify the exact port the usb device is connected to, and turn it off and on again in C#?
What I noticed was that the response was being sent in 5-6 packets. Whenever I wrote a command and didn't read all the packets using the read command (I stopped reading before the complete response was received), my PC started making that sound. In order to fix it, I used a boolean to indicate whether the entire response was received, and whenever I had to stop the program, I would delay the stopping as follows:
while(!responseComplete){
};
_timer.Stop();