The following code demonstrates the use of the SerialPort class to allow communication between a pc and wiring. In this example, the serialPort.portName is 'hard coded' although it would be easy to either prompt the user to provide it or to attempt to find it automatically. The main difference between this an earlier examples is it's use of an additional thread to 'read' any incoming data as opposed to using the SerialPort.dataRecieved event.



/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* WiringSimpleChat - Send and recive data with Wiring
* Version 1.0
* Uses a seprate thread to read incomming data
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

//Standard console namespace
using System;
//Namespaces for this example
using System.IO.Ports;
using System.Threading;

namespace WiringSimpleChat
{
  class WiringSimpleChat
  {
    static bool _continue; // For the read thread
    static SerialPort _serialPort; // Represents a serial port reasource.
    static string _COM = "COM3"; // set this to the COM port created by FTDI

    static void Main(string[] args)
    {
      Console.Write("WiringSimpleChat - Send/Receive data To/From Wiring" + Environment.NewLine);

      string message; //Data to be sent to Wiring
      StringComparer stringComparer = StringComparer.Ordinal; //To check for EXIT (CaSe SeNsItIvE)

      // Create the 'readThread' thread to handle Wirings responce
      Thread readThread = new Thread(Read);

      // Create a new SerialPort object with default settings.
      _serialPort = new SerialPort(_COM, 9600, Parity.None, 8, StopBits.One);

      // Attempt to open the port
      try
      {
        _serialPort.Open();
      }
      catch (Exception e)
      {
        Console.WriteLine("Error: " + e.Message);
      }

      if (_serialPort.IsOpen)
      {
        Console.WriteLine("** " + _serialPort.PortName + " connected **" + Environment.NewLine);
        Console.WriteLine("Type any text and wiring should echo it back to the console" + Environment.NewLine);
        Console.WriteLine("Enter EXIT to quit the application" + Environment.NewLine);
        readThread.Start(); // continue to read messages until user sends "EXIT"
        _continue = true;

      }
      else
      {
        Console.ReadLine();
        _continue = false;
      }

      // continue to send messages until user sends "EXIT"
      while (_continue)
      {
        message = Console.ReadLine();

        if (stringComparer.Equals("EXIT", message))
        {
          _continue = false;
          Environment.Exit(0);
        }
        else
        {
          _serialPort.WriteLine(message);
        }
      }

      // Block the read thread
      // Close the port connection
      try
      {
        readThread.Join();
        _serialPort.Close();
      }
      catch (Exception) { }
    }

    ///


    /// Reads a line of characters from the current stream
    /// then outputs the data in the console
    ///

    public static void Read()
    {
      while (_continue)
      {
        try
        {
          string message = _serialPort.ReadLine();
          Console.WriteLine("Wiring recived: "" + message + """);
        }
        catch (TimeoutException) { }
      }
    }

  }
}