Netduino SerialPortHelper to WinForm and XNA
A few weeks ago, I was contacted via YouTube in reference to the Netduino Serial Communication example I posted a while back. In my reply I promised that I would make a posting detailing how I have since updated the system. Before we start, check out the featured video to see the current system in action.
A while back I decided to adopt the methods as described by Hari over at the netduino forums to facilitate ReadLine functionality. The rationale for this was that by utilizing Hari’s code we no longer have to detail the length data being sent, thus simplifying the code greatly.
The resulting amalgamation of code enables simple filtering of data via separating the “inBound” data to a string array via a “,” delimiter. The first entry of the array then acts as an identifier for functionality, achieved via use of a switch statement. The following code block details the heart of this method:
/// <summary>
/// Filter and do something with read data
/// </summary>
/// <param name="inBound"></param>
private static void DoSomethingWithData(string inBound)
{
string[] data = inBound.Split(',');
if (data.Length == 0) { return; }
switch (data[0])
{
case "Open": netduinoSPH.PrintLine("Open");
break;
case "A": netduinoSPH.PrintLine("Automatic");
break;
case "T": DoTriggerUpdate(data);
break;
case "M": DoMotorUpdate(data);
break;
default: netduinoSPH.PrintLine(inBound); break;
}
Debug.Print(inBound);
}
In order to get things working via a windows form and even xna all that was needed was to clone Hari’s class and swap it over to the windows System.IO.Ports namespace, simple 🙂
The full source for both the forms and xna examples can be downloaded here.