早上好,
我正在开发一个C# WPF应用程序,它不断地从数据扫描器(DS4800-1000)读取条形码(大约每分钟一次),并将它们发送到服务器,服务器会回复该条形码的详细信息。这个扫描仪是连接到运行Windows8.1(非RT)的平板电脑通过USB到串行转换器从MOXA (型号UPort 1100)。
每当读取新的条形码时,就会触发DataReceived事件,并使用以下方法进行处理:
private void port1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Log.log(Log.LogLevel.Info, "MainScreen.port1_DataReceived");
Thread.Sleep(100);
String data = "";
// If the com port has been closed, do nothing
if (!comport1.IsOpen)
{
Log.log(Log.LogLevel.Info, "MainScreen.port1_DataReceived - COM CLOSED");
data = "COM CLOSED"; // Must be < 16 chars
}
else
{
// Obtain the number of bytes waiting in the port's buffer
int bytes = comport1.BytesToRead;
// Create a byte array buffer to hold the incoming data
byte[] buffer = new byte[bytes];
// Read the data from the port and store it in our buffer
comport1.Read(buffer, 0, bytes);
data = Encoding.Default.GetString(buffer);
Log.log(Log.LogLevel.Info, "Data received from barcode scanner number 1: " + data);
}
// COM port is handled by a different thread; this.Dispatcher calls the original thread
this.Dispatcher.Invoke((Action)(() =>
{
ExtractBarcodeData(data);
} ));
}我观察到了一种奇怪的行为:在随机的时候,我在应用程序上没有任何反应,尽管扫描仪实际上读取一个新的条形码,而我希望一个新的DataReceived事件作为以前的条形码。日志上说端口实际上是打开的,我也可以使用一个特定的按钮关闭并重新打开它。下面是一个异常(在Open()调用上):一个附加到系统上的设备不能工作。
我绝不能复制这个错误,它是完全不可预测和随机的!有人知道为什么DataReceived事件没有触发吗?
谢谢,FZ
发布于 2015-10-30 10:32:59
大多数USB到串行转换器都存在这个问题。它们可能从系统中消失并再次出现。在这种情况下打开的所有句柄都无效。
请打开设备管理器并验证每个USB集线器的电源管理选项卡。系统不应该关闭集线器。
https://stackoverflow.com/questions/33432740
复制相似问题