Serial Port DataReceived Event in .Net 2.0

Hi there,
This is driving me crazy, I cannot seem to get the DataRecieved event to work on the Pocket PC using the new serial port class in Visual Studio 2005 with C#.
The DataRecieved event just never seems to be called.
I can write data to the port just fine.
Here is some very simple sample code that reproduces the error:.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace SerialTest
{

public partial class Form1 : Form
{
// Setup the port
private SerialPort port = new SerialPort("COM8", 38400, Parity.None, 8, StopBits.One);

public Form1()
{
InitializeComponent();
// Enable Event handler
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{// Event for receiving data
// Read the buffer to text box.
statusBar1.Text = "Getting Data";
statusBar1.Refresh();
txtData.Text = txtData.Text + port.ReadExisting();
}

private void btn_Send_Click(object sender, EventArgs e)
{// Button to send test data
try
{
port.WriteLine("This is a test Line");
}
catch (Exception wex)
{
MessageBox.Show(wex.ToString());
}
}

private void btn_OpenPort_Click(object sender, EventArgs e)
{// Button to open the port
// Begin communications
if (port.IsOpen == false)
try
{
port.Open();
}
catch (Exception oex)
{
MessageBox.Show(oex.ToString());
}
}

private void btn_ClosePort_Click(object sender, EventArgs e)
{ // Button to close the port
port.Close();
}
private void btn_Close_Click(object sender, EventArgs e)
{// Button to exit the app.
this.Close();
}
I think I have the same problem described in this thread :http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=6727
But InvokeRequired does not exist in the CF, and I really don't grasp the solution described anyway.
I appreciate any suggestions.
Jason

[2365 byte] By [jebrown] at [2008-1-31]
# 1
You can always use control.invoke without checking if it is required (InvokeRequired). You can simulate InvokeRequired by caching the GUI thread instance and checking if it is the same as the thread running in a method but as I said this is unnecessary as you can always Invoke.

However, if you had the same problem you would be getting the same exception. I suspect you don't have any data to read and that is why your method doesn't run. Tweak the port properties to make sure your connection is good. If you get data through the port then your method should run and you'll get the exception and then you'll have to use Control.Invoke

To test this theory, add a button and in its click handler call port_DataReceived(). Do you get anything in the textbox?

Cheers
Daniel

DanielMoth at 2007-9-8 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 2
I am having the same issue. Wanted to know if you found a solution.Tongue Tied
euton_l at 2007-9-8 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 3
jebrown, euton_l:
Same here! I've tried everything and I'm simply not receiving any incoming data. Hyperterminal works fine with my modem. I even tried doing a loopback test and i send and receive data fine. The SerialDataReceivedEventHandler just never seems to fire. Any of you reach any solutions? I'll be sure to post if i find one.
Nathan
NathanIe at 2007-9-8 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 4
Hi Jason,

You would need to use Control.Invoke() to update the GUI controls because unlike Windows Forms events like Button.Click which are processed in the GUI thread, SerialPort events are processed in a non-GUI thread (more precisely a ThreadPool thread). The easiest way for you to update your controls would be to change the following code:

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{// Event for receiving data
// Read the buffer to text box.
statusBar1.Text = "Getting Data";
statusBar1.Refresh();
txtData.Text = txtData.Text + port.ReadExisting();
}

into this:

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)

{// Event for receiving data

// Read the buffer to text box.

this.Invoke(new EventHandler(DoUpdate));

}

private void DoUpdate(object s, EventArgs e)

{

statusBar1.Text = "Getting Data";

statusBar1.Refresh();

txtData.Text = txtData.Text + port.ReadExisting();

}

This should resolve your issue.

Cheers,
Anthony

AnthonyWong6 at 2007-9-8 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 5
Hi Jason (& the rest)
Did any of you ever get this stuff to work? I can't. I know my PDA is good cos other GPS apps work, and I know the serial data is good cos I can retrieve it a byte at a time. But the DataReceived won't fire (even using the Invoke), and I get a stupid number for BytesToRead (about 1.7 million). Has anyone *actually* made it work? Or is it really buggy on CF? Help!
Martin
docmartin at 2007-9-8 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 6
May be a stupid suggestion, but if you are NOT getting a thread exception but no data is received, have you tried setting DtrEnable=True

Ed Tenholder

jebrown wrote:
Hi there,

This is driving me crazy, I cannot seem to get the DataRecieved event to work on the Pocket PC using the new serial port class in Visual Studio 2005 with C#.

The DataRecieved event just never seems to be called.
I can write data to the port just fine.
Here is some very simple sample code that reproduces the error:.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace SerialTest
{
public partial class Form1 : Form
{
// Setup the port
private SerialPort port = new SerialPort("COM8", 38400, Parity.None, 8, StopBits.One);

public Form1()
{
InitializeComponent();
// Enable Event handler
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{// Event for receiving data
// Read the buffer to text box.
statusBar1.Text = "Getting Data";
statusBar1.Refresh();
txtData.Text = txtData.Text + port.ReadExisting();
}

private void btn_Send_Click(object sender, EventArgs e)
{// Button to send test data
try
{
port.WriteLine("This is a test Line");

}
catch (Exception wex)
{
MessageBox.Show(wex.ToString());
}
}

private void btn_OpenPort_Click(object sender, EventArgs e)
{// Button to open the port
// Begin communications
if (port.IsOpen == false)
try
{
port.Open();
}

catch (Exception oex)
{
MessageBox.Show(oex.ToString());
}
}

private void btn_ClosePort_Click(object sender, EventArgs e)
{ // Button to close the port
port.Close();
}

private void btn_Close_Click(object sender, EventArgs e)
{// Button to exit the app.
this.Close();
}

I think I have the same problem described in this thread : http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=6727

But InvokeRequired does not exist in the CF, and I really don't grasp the solution described anyway.

I appreciate any suggestions.

Jason

EdTenholder at 2007-9-8 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 7

It might be something much simpler...

I have noticed that if you set the SerialPort.ReadBufferSize to higher than the actual buffer size, then I never got a bytes received event. The actual buffer size on the kit I use is a suprisingly small 2047 bytes!

The SerialPort.ReceivedBytesThreshold MUST be lower than this, or you never recieve the event

See http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=93783&SiteID=1

Anarchy at 2007-9-8 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 8

Can I send any AT command to serialPort and read the result(OK) ?

DishanFernando at 2007-9-8 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 9

Thanks,

worked beautifully once your posting turned on the light bulb above my head that the serial receive data event is not in a GUI thread

Grant

GrantHaugen at 2007-9-8 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 10
Thanks for this code. I don't know much C# but I was able to convert this to VB enough to solve my issue.
thewizster at 2007-9-8 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 11

Greetings Ed,

I am curious as to whether you managed to get this to work. I am having a similar issue. I added the little patch ( DoUpdate ) and that does not work either.

My target system is am Axim x51, WM5, NET CF 2.0 developed in C# in VS 2005 Pro.

I realy need to get this to function soon. I have been looking everywhere and had no luck ( tried Franson.com, 32feet.net ).

Any assistance would greatly be appreciated.

Thanks.

David

DGretlein at 2007-9-8 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 12

Greetings Anthony,

I am curious as to whether you managed to get this to work. I am having a similar issue. I added the little patch ( DoUpdate ) and that does not work either.

My target system is am Axim x51, WM5, NET CF 2.0 developed in C# in VS 2005 Pro.

I realy need to get this to function soon. I have been looking everywhere and had no luck ( tried Franson.com, 32feet.net ).

Any assistance would greatly be appreciated.

Thanks.

David

DGretlein at 2007-9-8 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 13

Hi,

I was having this issue with an I-mate, .NET CF 2.0 C# and used the above mentioned fix and it worked beautifully!

Thanks

Grant

GrantHaugen at 2007-9-8 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 14

this works beautifully with setting DtrEnable = true

SimranJindal at 2007-9-8 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...