Click Here to Install Silverlight*
United StatesChange|All Microsoft Sites|Sign in
Microsoft
|Communities Home|Communities Worldwide
  SerialPort.DataReceived does not fire without debugger in .NET Micro Framework  
 |  Edit my Profile  |  Help
 
     
  
 
 
 
Rainer Stropek. 12/28/2008 3:44 AM PST
  Question
  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.
 
  Was this post helpful to you?  
 
 
  Reply | Print post   TopTop  
 
 
 
 
Jan Kučera 12/28/2008 7:03 AM PST
   
  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-BCC5-1ECB48A2EF8A@microsoft.com...
> 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.
>
 
  Was this post helpful to you?  
 
 
  Reply | Print post   TopTop  
 
 
 
 
Martin Welford 12/28/2008 10:51 AM PST
  Answer
  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-BCC5-1ECB48A2EF8A@microsoft.com...
> 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.
>

 
  Was this post helpful to you?  
 
 
  Reply | Print post   TopTop  
 
 
  Return to Microsoft Communities  Notify me of replies  
 
Windows Live ID

© 2009 Microsoft Corporation. All rights reserved. Contact Us |Terms of Use |Trademarks |Privacy Statement