我每 30 毫秒向 QR 码阅读器发送一个命令,然后我接受响应,并在标签上显示其大小。在我按下停止按钮之前,一切似乎都运行良好,之后我开始连续听到 Windows USB 连接/断开连接的声音。如果我拔下 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;
}
[更新] 我现在尝试了几种不同的实现,使用 async、winusb 库,并且在过去 3 天里我尝试过刷新端点并中止它们,以及无数其他方法。简而言之,我已经尝试了所有可能的方法,但问题仍在继续。我决定解决问题的唯一方法是模仿我从 USB 端口移除设备并重新插入的行为。我是否有可能以某种方式识别 USB 设备连接到的确切端口,然后在 C# 中将其关闭并再次打开?