C# fire event when certain string received over serial

Hi folks,

Im trying to automate my observatory with a .Net C# app and an arduino.... Everything is going OK apart from this last bit - I need to wait until the roof is opened before moving the telescope. I already have an event setup for serial data received but what i need is another event that ius triggered when the word "SHUTTEROPEN" is received over the serial line and im struggling :cold_sweat: Can somebody help me out? So far I have this:

private void btnOpenSerial_Click(object sender, EventArgs e) { if (!Arduino.IsOpen) { Arduino.PortName = Properties.Settings.Default.ComPort; Arduino.Open(); string testtext = "Serial Port Status: " + Arduino.IsOpen.ToString() + Environment.NewLine; txtbx1.Text += testtext; Arduino.DataReceived += Arduino_DataReceived; timer1.Start(); CheckStatus(); } else if (Arduino.IsOpen) { CheckStatus(); } else { string testtext = "Serial Port ERROR: " + Arduino.IsOpen.ToString() + Environment.NewLine; txtbx1.Text += testtext; } }

and this:

void Arduino_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { string datafromarduino = Arduino.ReadLine(); Invoke((MethodInvoker)delegate { txtbx1.AppendText("Data recieved: " + datafromarduino + " @ " + DateTime.Now.ToString("HH:mm:ss") + System.Environment.NewLine); if (datafromarduino == "Shutter open\r") { lblShuter.BackColor = System.Drawing.Color.Red; } if (datafromarduino == "Shutter closed\r") { lblShuter.BackColor = System.Drawing.Color.Green; }

if (datafromarduino == "Mains On\r") { lblMains.BackColor = System.Drawing.Color.Red; } if (datafromarduino == "Mains Off\r") { lblMains.BackColor = System.Drawing.Color.Green; } }); }

But how do I go about creating a specific handler for a specific word?

what i need is another event that ius triggered when the word “SHUTTEROPEN” is received over the serial line and im struggling

How is defining an event handler, an event to be handled, and raising that event going to result in different behavior from simply calling the event handler function?

Hi, I wrote an C# implementation for CmdMessenger. A serial messaging library. One of the things it handles is performing callbacks on incoming commands. See this thread: http://forum.arduino.cc/index.php?topic=186928.0

If you have questions, please post them there


Have you been able to use the C# implementation of CmdMessenger? I'd love to hear what you think of it. Meanwhile, I did a big update on the C# code, see this post