.NET Framework - SerialPort.DataReceived does not fire without debugger

Asked By Rainer Stropek on 28-Dec-08 06:45 AM
Hi!

I have connected a U-BLOX GPS receiver with a Tahoe development board. I
communicate via the SerialPort class ("COM2", 9600 baud, Parity.None, 8 data
bits, StopBits.One). Everything works fine as long as I run the program using
the VS 2008 debugger. To show my point I extracted the problem into a very
simple test application. Here is the code:

public static void Main()
{
Program myApplication = new Program();
Window mainWindow = myApplication.CreateWindow();
myApplication.Startup += new
Microsoft.SPOT.EventHandler(myApplication_Startup);
myApplication.Run(mainWindow);
}

private static SerialPort port;

static void myApplication_Startup(object sender, Microsoft.SPOT.EventArgs e)
{
port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
}

static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Debug.Print("port_DataReceived IN");

int bytesToRead = port.BytesToRead;
while (bytesToRead > 0)
{
Debug.Print(string.Concat("  ", bytesToRead.ToString(), " bytes to read."));
byte[] buffer = new byte[bytesToRead];
port.Read(buffer, 0, bytesToRead);
bytesToRead = port.BytesToRead;
}

Debug.Print("port_DataReceived OUT");
}

private Window mainWindow;

public Window CreateWindow() { ... }

If I run the program inside the debugger it runs fine. I get the following
output:

port_DataReceived IN
20 bytes to read.
2 bytes to read.
1 bytes to read.
3 bytes to read.
...
1 bytes to read.
port_DataReceived OUT
port_DataReceived IN
port_DataReceived OUT
...
port_DataReceived IN
2 bytes to read.
2 bytes to read.
...
1 bytes to read.
port_DataReceived OUT
...

However, if I run the program without the debugger port_DataReceived does
not get called! Therefore I receive a BUFFER OVERFLOW error after a few
moments. Here is the output I see if I attach MFDeploy:

Ready.
Buffer OVFLW
Buffer OVFLW
Buffer OVFLW
Buffer OVFLW
Buffer OVFLW
...

To get around this problem I created a separate thread on my own that reads
data from the serial port in a while loop with a short sleep interval.

Does anyone know why SerialPort.DataReceived does not fire without debugger?
Is it a timing problem (i.e. debugger slows down speed of execution)? Is
there a better way to get around the problem than the one I suggested?

BTW, does anyone know if there is a C# Micro Framework implementation of a
NMEA parser?

Kind regards,
Rainer.




kucer replied on 28-Dec-08 10:01 AM
Hi!
Is there any hint that the handler is not called other than the buffer
overflow error?

As for the GPS decoder, a starting point could be the code for the Rob's
book, available at
http://www.microsoft.com/mspress/companion/9780735623651/

There is a GPSDecoder in the examples for fifth chapter.

Jan
Martin Welford replied on 28-Dec-08 01:48 PM
Rainer,

There is a work around for this issue.
The initialization of the port during the "Open" call messes up the event
handler...

Instead of
port.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived);
port.Open();

do this:
port.Open();
port.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived);


--
Martin Welford
Device Solutions
www.devicesolutions.net
blog.devicesolutions.net
zmorris replied to Martin Welford on 21-May-10 10:00 AM
FYI Micro Framework team:  This bug still exists in v4.0.

The work around still works to.