我每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;
}
[更新] 我现在已经尝试了几种不同的实现,使用异步,winusb库,并且在过去三天中,我尝试刷新端点并中止端点,并尝试了无数其他方法。简而言之,我已经尝试了所有可能的方法,并且问题仍在继续。我已决定解决此问题的唯一方法是模拟我从USB端口中删除设备并再次插入设备的行为。我能否以某种方式确定USB设备连接到的确切端口,然后在C#中将其关闭然后再打开?
答案 0 :(得分:0)
我注意到的是,响应以5-6个数据包的形式发送。每当我编写命令但未使用read命令读取所有数据包时(在收到完整响应之前我停止读取),我的PC就会发出声音。为了解决该问题,我使用了一个布尔值来表示是否收到了整个响应,并且每当必须停止程序时,我都会按如下所示延迟停止:
retry pattern