How to Use Datareceived Event of the Serialport Port Object in C#

How do I use dataReceived event of the SerialPort Port Object in C#?

I think your issue is the line:**

sp.DataReceived += port_OnReceiveDatazz;

Shouldn't it be:

sp.DataReceived += new SerialDataReceivedEventHandler (port_OnReceiveDatazz);

**Nevermind, the syntax is fine (didn't realize the shortcut at the time I originally answered this question).

I've also seen suggestions that you should turn the following options on for your serial port:

sp.DtrEnable = true;    // Data-terminal-ready
sp.RtsEnable = true; // Request-to-send

You may also have to set the handshake to RequestToSend (via the handshake enumeration).


UPDATE:

Found a suggestion that says you should open your port first, then assign the event handler. Maybe it's a bug?

So instead of this:

sp.DataReceived += new SerialDataReceivedEventHandler (port_OnReceiveDatazz);
sp.Open();

Do this:

sp.Open();
sp.DataReceived += new SerialDataReceivedEventHandler (port_OnReceiveDatazz);

Let me know how that goes.

how can I get, which SerialPort has triggered the given DataReceived event?

Sender object is the SerialPort.

public static void Main()
{
SerialPort mySerialPort = new SerialPort("COM1");

mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;

mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

mySerialPort.Open();

Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}

private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
Console.Write(indata);
}

How to display the data read in DataReceived event handler of serialport

The MSDN contains a good article with examples about using control methods and properties from other threads.

In short, what you need is a delegate method that sets the Text property of your text box with a given string. You then call that delegate from within your mySerialPort_DataReceived handler via the TextBox.Invoke() method. Something like this:

public delegate void AddDataDelegate(String myString);
public AddDataDelegate myDelegate;

private void Form1_Load(object sender, EventArgs e)
{
//...
this.myDelegate = new AddDataDelegate(AddDataMethod);
}

public void AddDataMethod(String myString)
{
textbox1.AppendText(myString);
}

private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string s= sp.ReadExisting();

textbox1.Invoke(this.myDelegate, new Object[] {s});
}

How to call a method using the serial port DataReceived event

One of your problems is the using block:

using (arduino = new SerialPort(port))
{
arduino.Open();
arduino.DtrEnable = true;
arduino.RtsEnable = true;
arduino.BaudRate = 9600;

arduino.DataReceived += OnDataReceived; //when data is received, call method below.

//System.Windows.Forms.Application.Exit(); //this works, which means the above line has been run too.
}

When the code is executed arduino.Dispose is called and your SerialPort vanishes into oblivion and so does the event registration. Therefore you event is never fired.

To Check whether your event works. Use a breakpoint in the Debugger.

EDIT:

The null value means that it has been unregistered. Since the SerialPort has been disposed.

Basically I just want to run the method OnDataReceived upon receiving data through my serial port.

The thing is that you don't run this method. It is designed (like any other event) to be fired when needed. So it will help you to remain passive and your GUI can remain responsive (since you don't have to run it actively).

A SerialPort can be written to and read from when it is open.
So if you want to monitor it, just open it up, register to the event and wait to see what happens.

public void checkBox1_CheckedChanged(object sender, EventArgs e)
{

if (checkBox1.Checked)
{

arduino = new SerialPort(port);

// you should specify these before opening the port
arduino.DtrEnable = true;
arduino.RtsEnable = true;
arduino.BaudRate = 9600;

// register to the event
arduino.DataReceived += OnDataReceived;
//open the port
arduino.Open();

// tell the user
checkBox1.Text = "Listening...";
}
}

In the Event you should specify what you want to do when the data arrives:

private void OnDataReceived(object sender, SerialDataReceivedEventArgs e) //this method does not get called from the above method.
{
// first of all read out what you have received!
received = arduino.ReadLine();
// do something with the information

// if you have a condition and really want to close the application or shut down the computer
// first close the port!!!
arduino.Close();

}

Your old version:

    System.Windows.Forms.Application.Exit();
received = arduino.ReadLine();

will probably not work. It is like closing the door behind you and then trying to pick up the phone that is still in your closed flat...



Related Topics



Leave a reply



Submit