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

Serial Port DataReceived Event in .Net 2.0
Touraj
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
Chenxia
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
Jason Dolinger
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
Matt W.
Ed Tenholder
Chartsiam
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
thargy
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
Jan Pukovec
Sinem
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
tp_hi
Can I send any AT command to serialPort and read the result(OK)
digimonk
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
dpuza
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
yoyop
this works beautifully with setting DtrEnable = true
Miah Helpmann
Mark Arteaga_MVP
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