As I couldn't find any decent sample code for reading and writing through the serial port in Mono/.NET, I decided to write my own. In the end it's fairly simple, and should work on both Mono and .NET platforms (the code is fairly self-explanatory).
// 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());
}
}
}
// 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");
}
}
}