Using CmdMessenger

As of version 3, the CmdMessenger messaging library has an Arduino and .NET+Mono compatible C# implementation, and is packaged with examples of the these two sides communicating with each other.

The library implements

  1. Commands that can be sent or received.
  2. Multiple arguments can be appended to commands.
  3. Callback functions can be triggered on received commands.
  4. All basic data-types (char arrays, floats, ints, bytes), both for sending and receiving.
  5. Optional waiting for a acknowlegde commands.
  6. Escaping data. The special characters that would be interpreted to be field separators or command separators, can be escaped and used in arguments.
  7. Sending and receiving both plain text and binary data.

You can find the CmdMessenger Playground page here:
CmdMessenger

Download the library here:
http://thijs.elenbaas.net/downloads/?did=9

And find more background and and example of the functionality here:
http://thijs.elenbaas.net/2013/09/arduino-to-pc-messaging

Direct serial writing

If you are not looking for a messaging protocol, but just want to send over raw data, Have a look at these examples

They are fairly simple, and should work on both Mono and .NET platforms (the code is fairly self-explanatory).

Reading from serial

// SerialTest.cs
//
// The MIT License
//
// Copyright (c) 2008 Matias Korhonen
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using System;
using System.IO.Ports;

class MainClass
{	
	public static void Main(string[] args)
	{
		SerialPort sport = new SerialPort("/dev/ttyUSB0", 9600);

		if (sport != null)
		{
			if (sport.IsOpen)
				{
					sport.Close();
				}
		}

		Console.WriteLine("Arduino Serial Test");

		sport.Open();

		while(true)
		{		
			Console.WriteLine(sport.ReadLine());
		}
	}

}

Writing to serial

// SerialWriteTest.cs
//
// The MIT License
// 
// Copyright (c) 2008 Matias Korhonen
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using System;
using System.IO.Ports;

public class SerialWriteTest
{

	public static void Main(string[] args)
	{
		SerialPort sport = new SerialPort("/dev/ttyUSB0", 9600);

		if (sport != null)
		{
		if (sport.IsOpen)
			{
				sport.Close();
			}
		}

		Console.WriteLine("Arduino Serial Write Test");

		sport.Open();

		String inputString = "";
		Console.WriteLine("Toggle LED (write 'e' to exit)");

		while(!inputString.Equals("e", StringComparison.CurrentCultureIgnoreCase))
		{		
			inputString = Console.ReadLine();
			sport.WriteLine("a");
		}
	}
}

Share