Datareceived event in Winforms application [Resolved]

Posted by Sudhakar_A under C# on 9/20/2013 | Points: 10 | Views : 2216 | Status : [Member] | Replies : 7
I send packet to ma device......it response back. Below function returns response data.
but i wan to write the data or display the data on richtextbox immediately.



public void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
{

string data = port.ReadExisting();
}


Pls help me to sort out




Responses

Posted by: Allemahesh on: 9/20/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Dear Sudhakar,

You can use the textbox1.Clear(); method to clear the text before load.

Code:
public void AddDataMethod(String myString) 

{
textbox1.Clear();
textbox1.AppendText(myStrin g);
}


Happy Coding.
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link
http://stackoverflow.com/questions/11590945/how-to-display-the-data-read-in-datareceived-event-handler-of-serialport

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/20/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Sudhakar,

You can use the below code:-

public void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e) 

{
RichTextBoxID.Text = Convert.ToString(port.ReadExisting()) ;
}


Happy Coding
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Sample
This example adds a SerialDataReceivedEventHandler to DataReceived to read all the available data received on the COM1 port.
using System;

using System.IO.Ports;

class PortDataReceived
{
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.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);
}
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link for writing to RichTextBox
http://stackoverflow.com/questions/6521475/how-can-i-redirect-process-output-console-to-richtextbox
http://stackoverflow.com/questions/8907490/serialport-datareceived-event-does-not-fire
http://stackoverflow.com/questions/17324886/read-back-at-command-response-in-a-richtextbox

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudhakar_A on: 9/20/2013 [Member] Starter | Points: 25

Up
0
Down
@Bandi.......thnk's first of all,but i wan to display the mystring in textbox,but your above function return textbox.append
i wan only to display the upcoming output as textbox.text(mystring);


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(myStrin g);
}

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

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

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudhakar_A on: 9/20/2013 [Member] Starter | Points: 25

Up
0
Down
Thnk's @mahesh.........it worked.

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response