Simplest serial port terminal in C#

Posted on 2009/04/18

11


Serial ports are not very useful for the end-user, but they are very important for administration of headless (without monitor) servers and for embedded device development.

For example, if you are designing a chip that contains a micro-processor with a serial port (UART) to communicate with an external control (such as a graphical tool running on a PC) chances are that you won’t immediately have a real device attached to the real serial port of the PC. In order to start developing while you wait for the real device (or an evaluation board) you can use virtual serial ports.

com0com is a nice Windows tool to create virtual serial ports and to connect them together, simulating a cable link. It is then possible to run two programs, connected to the two virtual serial ports, that communicate with each other. You can develop your graphical user interface on one side, and a model of the device on the other side.

  • Download and install the latest version of com0com from Sourceforge: https://sourceforge.net/project/showfiles.php?group_id=129551&package_id=141993
  • If you are a Vista user, disable UAC (more info here: http://support.flex-radio.com/Downloads.aspx?id=218)
  • Navigate into your Start menu to com0com and open Setup
  • Click “Add Pair” button to create two virtual serial ports. A pop up can appear, telling you that the driver could not be verified. Decide to continue anyway.
  • Change the names of the serial ports to COM20 and COM21 (or any couple of names that are not already taken) and click Apply. This is necessary because .NET complains if the name of the serial port does not start with COM.
com0com graphical Setup

com0com graphical Setup

  • Create a new C# Console Project named SerialTest
  • In Program.cs, write the following code
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;

namespace SerialTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] names = SerialPort.GetPortNames();
            Console.WriteLine("Serial ports:");
            foreach (string name in names)
                Console.WriteLine(name);
            Console.Write("Choose one:");
            SerialPort p = new SerialPort(Console.ReadLine());
            p.DataReceived += new SerialDataReceivedEventHandler(p_DataReceived);
            p.Open();
            string line;
            do
            {
                line = Console.ReadLine();
                p.Write(line);
            } while (line != "quit");
            p.Close();
        }

        static void p_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Console.WriteLine(
                (sender as SerialPort).ReadExisting());
        }
    }
}
  • Build, and hit ctrl-F5 to run the program without debugging.
  • Into the terminal, choose COM20 port.
  • Into Visual Studio, hit ctrl-F5 to run another instance of the program.
  • Into the terminal, choose COM21 port.
  • Write a line in a terminal and check that in the other terminal the line is indeed been received and written on the screen.
  • To exit the terminals, type “quit”.

From this starting point, you can develop your own solution. Keep in mind that the behavior of this serial port emulation could differ from the behavior of the physical device you will attach because com0com does not emulate (for example) power problems, signal attenuation, interference and so on.

As a complex example, “Any Serial Port” is a free open source full-blown serial port manager written in C# that enables the creation of custom drivers and communication protocols using a simplified programming language and a graphical interface.

add to del.icio.us :: Bookmark Post in Technorati :: Add to Blinkslist :: add to furl :: Digg it :: add to ma.gnolia :: Stumble It! :: add to simpy :: seed the vine :: :: :: TailRank :: post to facebook :: Bookmark on Google :: Add to Netscape :: Share on Yahoo :: Add this to Live

Advertisements
Posted in: Software