Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SerialPort.DataReceived does not fire without debugger

93 views
Skip to first unread message

Stropek.@discussions.microsoft.com Rainer Stropek.

unread,
Dec 28, 2008, 6:45:00 AM12/28/08
to
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.

Jan Kučera

unread,
Dec 28, 2008, 10:01:54 AM12/28/08
to
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

"Rainer Stropek." <Rainer Stropek.@discussions.microsoft.com> wrote in
message news:B6721622-3BFD-45FA...@microsoft.com...

Martin Welford

unread,
Dec 28, 2008, 1:48:25 PM12/28/08
to
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


"Rainer Stropek." <Rainer Stropek.@discussions.microsoft.com> wrote in
message news:B6721622-3BFD-45FA...@microsoft.com...

zmorris

unread,
May 21, 2010, 10:00:01 AM5/21/10
to
FYI Micro Framework team: This bug still exists in v4.0.

The work around still works to.

0 new messages