Balau

Working to work less

  • RSS Shared Items

    • New Web-Based Netbook From Litl — Based On Clutter, Uncluttered 2009/11/04
      cananian writes "The webbook company of Gnome's own Havoc Pennington (with a healthy dose of ex-Nokia and ex-OLPC engineers) finally shed its secrecy today, with a new web site and an article in the WSJ. Technical specs on the hardware were found by Engadget last week, and now comes a bit more information on the software behind the UI. Most of the […]
    • Man-In-the-Middle Vulnerability For SSL and TLS 2009/11/05
      imbaczek writes "The SSL 3.0+ and TLS 1.0+ protocols are vulnerable to a set of related attacks which allow a man-in-the-middle (MITM) operating at or below the TCP layer to inject a chosen plaintext prefix into the encrypted data stream, often without detection by either end of the connection. This is possible because an 'authentication gap' […]
    • Marc Deslauriers: GNOME Keyring 2009/11/04
      For the past week or so, people have been talking about a “security issue” in Seahorse. This sums up my opinion on the matter:This isn't a security issue, and there is no good way to fix it.There, I've said it. Now, here's some background:Although people are talking about Seahorse, the actual application that manages passwords is called GNOME […]
    • The Machine SID Duplication Myth 2009/11/04
      toppings writes "Microsoft Technical fellow Mark Russinovich explains why he is now retiring NewSID, which has been used by IT departments for years when deploying Windows to new systems from customized clone images. Russinovich writes: 'The reason that I began considering NewSID for retirement is that, although people generally reported success wi […]
    • Greatest Moments In EDA Innovation 2009/11/03
      Innovation is the lifeblood of the EDA industry, and it is only because of innovation from many sources – including academia and industry – that modern IC design is possible at all. Today at Cadence (Nov. 3, 2009), we are celebrating Cadence Innovation Day. As such, it seems like a good time to consider the “greatest” innovations that shaped our industry. Al […]
    • Educating Young People 2009/11/02
      Educated women have fewer children, are wealthier and are less likely to accept fundamentalist extremism. If we want a safer world, we should consider the utility of spending dollars on educating young people as an alternative to troops and weapons. —Lawrence M. Krauss, “How Women Can Save the Planet“
    • RepRap, the replicating machine: The Free and Open Source Factory on the Desktop? 2009/11/02
      RepRap (replicating Rapid-prototyper) is a 3D printer and it is impeccably free and open source under both the GPL and the Creative Commons Licence. It’s early days but the implications and the promise are potentially enormous in their own right — but the fact that it is resolutely not proprietary is what caught my attention. read more
    • An UML-driven Interface Generation Approach for SoC Design 2009/11/02
      In the latest years, the usage of UML to describe complete systems, including SoCs, has been increased. In trend with this line, this work proposes a methodology, based on UML, for SoCs designing which is focused on high-level communication modeling. The goal is to transform UML models, compliant with UML profile for SoC, into SystemC behavioral descriptions […]
    • A Low-Power, ARM-based Microcontroller from Oslo with a Winning Presentation (Steve Leibson - Steve Leibson) 2009/11/02
      Last month at the ARM Techcon 3 conference, I watched as the CEO of a Norwegian fabless semiconductor company named Energy Micro. I was impressed by the strikingly graphical way the Energy Micro marketing crew came up with to demonstrate why their microcontroller has the lowest power. I was impressed enough to go through those slides here with you. See […]
    • Toyota Develops New Flower Species To Reduce Pollution 2009/11/02
      teko_teko writes "Toyota has created two flower species that absorb nitrogen oxides and take heat out of the atmosphere. The flowers, derivatives of the cherry sage plant and the gardenia, were specially developed for the grounds of Toyota's Prius plant in Toyota City, Japan. The sage derivative's leaves have unique characteristics that absorb […]

Simplest serial port terminal in C#

Posted by Balau on 2009/04/18

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.

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

4 Responses to “Simplest serial port terminal in C#”

  1. Dan said

    I have a project that uses C# SerialPort class and com0com virtual com ports running on 64-bit Windows 2008 Server. I am running into the following problem.

    If I go:
    - C# Terminal -> com0com -> TeraTerm it works just fine

    - TeraTerm -> com0com -> T38Modem (soft voip fax modem) it also works fine, TeraTerm communicates with the modem and commands and responses go back and forth

    - C# Terminal -> com0com -> T38Modem however fails to work, and I’ve spent two days on it and can’t figure it out.

    When using TeraTerm, as soon as the connection is established, T38Modem indicates a connection has started. When using C#, it never indicates it see’s the connection, nor does it ever respond to AT modem commands.

    I’ve tried sending both \r and \r\n to the modem with no luck.

    With your experience with com0com and C# SerialPort class, have you ever seen this? Its strange that if I use a terminal app to talk to the soft modem it works, as does if I use Window’s fax server.

    I can only assume something isn’t getting set in the connection correctly, but there are very few options available.

    Any ideas? I tried replacing my code with your simple console app and get the exact same problem.

  2. Dan said

    Thanks for the help!
    I had tried DtrEnable and RtsEnable independently but not together, that fixed it.

    I knew it had to be something simple I was missing!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>