如果先前接收到数据,SerialPort.Open() 会冻结系统(紧凑框架)

发布于 2024-07-16 02:18:30 字数 1239 浏览 7 评论 0 原文

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

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

我制作了代码的简化版本,看看复杂性是否与它有关,但是带有打开端口的按钮的简单表单将以同样的方式冻结。 这是简化的代码:

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 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.

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);
        }
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┼── 2024-07-23 02:18:30

MS 提供的 SerialPort 类还有很多不足之处。 您真正需要知道的是本机调用中到底在哪里卡住了。 我的猜测是内部缓冲区中有数据,或者甚至可能设置了溢出,并且托管包装器正在呕吐。

我的建议是获取 OpenNETCF 串行库,它是免费并带有完整源代码。 它与 MS 提供的类接口兼容,并且允许您将代码一直执行到驱动程序调用以查看发生了什么。

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.

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.

明天过后 2024-07-23 02:18:30

早在我做串行驱动程序时,我就在 16550 UART(至少其中不少)中遇到了一个错误,即当 FIFO 启用时,如果接收数据寄存器中已有数据,芯片就会锁定。

我通过让驱动程序在启用 FIFO 之前清除芯片来修复该错误。 然而,如果像 WinCE 6 这样最近的版本没有解决这个错误,我会感到惊讶,但我认为这是可能的(也许当前最新的 16550 实现不再有这个错误)。 我对 WinCE 不是特别熟悉 - 某些 DDK 中是否提供了串行驱动程序的源代码? 您正在使用的串行驱动程序有支持/更新选项吗?

Way back when I was doing serial drivers I ran into a bug in the 16550 UARTs (at least quite a few of them) that the chip would lock up if there was data alreay in the Receive Data Register when the FIFOs were enabled.

I fixed that bug by having the driver clear the chip before enabling the FIFOs. However, I'd be surprised if something as recent as WinCE 6 didn't take care of this bug, but I suppose it's possible (maybe current most current 16550 implemetations don't have this bug anymore). I'm not particularly familiar with WinCE - is the source for the serial driver available in some DDK? Is there a support/update option for the serial driver you're using?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文