Here is a rudimentary example of using an event handler to capture and display data from an Arduino (I used a Uno and code similar to what sterretje posted above). The C# project is simply windows form with a richtext box control. Do not copy and paste the event handlers instead access them from the properties window in design view. This is a good basis to start creating your own custom Arduino serial port monitor.
namespace Serial_Port_Reader
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SerialPort SP = new SerialPort();
public delegate void spDelegate(string sData);
private void Form1_Load(object sender, EventArgs e)
{
SP.DataReceived += SP_DataReceived;
SP.PortName = "COM7";
SP.BaudRate = 9600;
SP.DtrEnable = true; //reset Arduino
SP.Open();
SP.ReadExisting(); //clear any junk that might be in the serial buffer
}
private void SP_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SP.ReadTimeout = 20;
try
{
string str = SP.ReadLine();
this.BeginInvoke((new spDelegate(stringDisplay)), str);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void stringDisplay(string sData)
{
DateTime localDate = DateTime.Now; //snippet copied from docs.Microsoft.com
richTextBox1.AppendText(localDate + " ");
richTextBox1.AppendText(sData + "\n");
}
}
}