views:

616

answers:

2

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

A: 

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?

Michael Burr
Unfortunately a bad assumption - the serial driver is provided by the OEM - sure CE ships with a sample, but many, many OEMs just bring along their old drivers from previous versions.
ctacke
+2  A: 

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.

ctacke