如果以前收到数据,则SerialPort.Open()将冻结系统(Compact Framework) [英] SerialPort.Open() freezes the system if data was received previously (Compact Framework)

查看:115
本文介绍了如果以前收到数据,则SerialPort.Open()将冻结系统(Compact Framework)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个很奇怪的问题,也许你们可以帮帮我。我有一个Windows CE 6设备,该设备使用通过串行端口连接的条形码读卡器。我正在使用Compact Framework的(v2.0) SerialPort 类来处理此问题,并且一切都工作正常。

I have a very weird problem here, maybe you guys can help me out. I have a Windows CE 6 device that uses a barcode card reader connected through a serial port. I'm using the Compact Framework's (v2.0) SerialPort class to handle this, and everything has been working fine.

但是,有一个问题。如果在打开串行端口之前的任何时间刷卡,则整个系统将在 Open()调用时冻结。没有例外,没有警告,只是无缘无故地冻结了整个系统。我尝试在打开端口之前清除缓冲区,但显然该方法只能在打开端口后使用,因为我收到了 InvalidOperationException

There's one problem, however. If a card is swiped at any point before the serial port is opened, the entire system freezes at the Open() call. No exceptions, no warnings, just a complete system freeze for no reason. I tried clearing the buffers before opening the port but apparently that method can only be used after the port has been opened because I got an InvalidOperationException.

我制作了代码的简化版,以查看复杂性是否与之相关,但是带有打开端口的按钮的简单形式也会以相同的方式冻结。这是简化的代码:

I made a simplified version of the code to see if the complexity had anything to do with it, but a simple form with a button that opens the port will freeze in the same way. Here's the simplified code:

private void btConnect_Click(object sender, EventArgs e)
{
        try
        {
            this.serialPort = new SerialPort(this.txName.Text, Convert.ToInt32(this.txBaud.Text));
            this.serialPort.RtsEnable = this.chRTS.Checked;

            this.serialPort.Open(); //it freezes here
            this.btConnect.Text = "Disconnect";

            this.txName.Enabled = false;
            this.txBaud.Enabled = false;
            this.chRTS.Enabled = false;
        }
        catch
        {
            MessageBox.Show("Failed to open port. Please check your settings and try again.", "Operation failed", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
        }
}

我看不到我在做什么,我开始认为这是紧凑型框架中的错误。读卡器发送诸如 F03030DKD03003\r\n 之类的数据包。有任何想法吗?

I can't see what I'm doing wrong, I'm starting to think that it's a bug in the compact framework. The card reader sends packets such as F03030DKD03003\r\n. Any ideas? Thanks in advance.

推荐答案

MS提供的SerialPort类有很多不足之处。您真正需要知道的是事物在本地调用中的确切位置。我的猜测是内部缓冲区中有数据,或者甚至设置了溢出,并且托管包装程序正在对此进行呕吐。

The MS-provided SerialPort classes leave a lot to be desired. What you really need to know is exactly where in the native calls the thing is stuck. My guess is that the internal buffers have data in them, or maybe even an overrun is set, and the managed wrapper is puking on that.

我的建议是获取 OpenNETCF串行库,它是免费的,并提供了完整的源代码。它与MS提供的类兼容,并且使您可以将代码一直浏览到驱动程序调用中,以了解发生了什么。

My suggestion is to get the OpenNETCF serial library, which is free and comes with full source. It's interface-compatible with the MS-supplied classes, and it will allow you to walk the code all the way to the driver calls to see what's going on.

这篇关于如果以前收到数据,则SerialPort.Open()将冻结系统(Compact Framework)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆