public partial class Form1 : Form
{
string rxdata;
public Form1()
{
InitializeComponent();
///////////////////////Serial port Setting//////////////////////////////////
SerialPort SerialPort1 = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
SerialPort1.Handshake = Handshake.None;
SerialPort1.ReadTimeout = 500;
SerialPort1.WriteTimeout = 500;
///////////////////////////-Receiving event handler-/////////////////////////////
SerialPort1.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
//////////////////////////////////////////////////////////////////////////////
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (SerialPort1.IsOpen)
{
SerialPort1.Close();
}
}
private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
MessageBox.Show("Data Received");
SerialPort1 = (SerialPort)sender;
rxdata = SerialPort1.ReadExisting();
this.Invoke(new EventHandler(displaytext));
}
private void displaytext(object sender, EventArgs e)
{
// textBox1.AppendText(rxdata);
richTextBox1.AppendText(rxdata);
}