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]
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
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
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
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