带有Moxa UPort 1100的C#串行端口



早上好,

我正在开发一个C#WPF应用程序,它可以从DATALOGIC扫描仪(DS4800-1000)连续读取条形码(大约每分钟一个),并将其发送到服务器,服务器会回复有关特定条形码的详细信息。该扫描仪通过MOXA(型号UPort1100)的USB到串行转换器连接到运行Windows8.1(非RT)的平板电脑。

每当读取新条形码时,就会触发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

大多数USB到串行转换器都有这个问题。它们可能会从系统中消失,然后再次出现。在这种情况下,所有打开的句柄都将无效。

请打开设备管理器并验证每个USB集线器的电源管理选项卡。系统不应关闭集线器的电源。

最新更新