在我停止我的 libusbdotnet 应用程序后,USB 设备保持连接和断开连接

USB device keeps connecting and disconnecting after I stop my libusbdotnet application

我每 30 毫秒向二维码 reader 发送一个命令,然后接收响应,并将其大小显示在标签上。一切似乎都正常,直到我按下停止按钮,之后我开始连续听到 windows usb connect/disconnect 声音。如果我拔下 USB 设备并重新插入,声音就会消失。可能出了什么问题?

    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;
    }

[更新] 我现在已经尝试了几种不同的实现,使用异步、winusb 库,并且在过去 3 天里我尝试了刷新端点并中止它们以及无数其他方法。简而言之,我已经尝试了所有可能的方法,但问题仍在继续。我决定解决问题的唯一方法是模拟我从 USB 端口移除设备并再次插入的行为。我是否有可能以某种方式识别 USB 设备连接到的确切端口,并在 C# 中将其关闭并再次打开?

我注意到响应以 5-6 个数据包的形式发送。每当我写了一个命令但没有使用读取命令读取所有数据包时(我在收到完整响应之前停止读取),我的 PC 就开始发出那种声音。为了修复它,我使用了一个布尔值来指示是否收到了整个响应,并且每当我不得不停止程序时,我都会延迟停止,如下所示:

while(!responseComplete){
};

_timer.Stop();