This page is likely outdated (last edited on 16 Sep 2011). Visit the new documentation for updated content.


Table of contents


This is to show people how to use System.IO.Ports in older versions of Mono, such as 1.2.x. It *is* possible to read and write to a serial port (RS-232) with Mono.


At the time of this writing, there are a a few limitations that one must take note:

1) There is no event notification for received serial data. If you want to receive data, one must set a timeout and watch for received data by polling ReadByte() when you think there might be data.

2) One must Read data in byte[] format only – there is no char[] support. You must do your own reading of bytes and translate that into your encoding.

3) DiscardNull, ParityReplace, ReceivedBytesThreshold are not implemented.

Example Code

This example code can be used to test a connection with a serial modem connected to /dev/ttyS0

using System;
using System.IO.Ports;
public class SerialPortTest
    public static void Main(string[] args)
        SerialPortTest myTest = new SerialPortTest();
    private SerialPort mySerial;
    // Constructor
    public SerialPortTest()
    public void Test()
        if (mySerial != null)
            if (mySerial.IsOpen)
        mySerial = new SerialPort("/dev/ttyS0", 38400);
        mySerial.ReadTimeout = 400;
                // Should output some information about your modem firmware
    public string ReadData()
        byte tmpByte;
        string rxString = "";
        tmpByte = (byte) mySerial.ReadByte();
        while (tmpByte != 255) {
            rxString += ((char) tmpByte);
            tmpByte = (byte) mySerial.ReadByte();
        return rxString;
    public bool SendData(string Data)
        return true;

Save this code to a file (such as SerialExample.cs).

Then, you can type:

gmcs SerialExample.cs

and then

mono SerialExample.exe

Provided you are properly connected to an active serial modem, you should see output similar to the following:

$ mono SerialExample.exe
Sportster 14,400/FAX RS Rev. 1.5

Congratulations! You have now accessed your first serial device on Linux with Mono!

Here are some links that I have found helpful, and you might as well.

.NET Serial Port Library This project aims to implement the complete System.IO.Ports namespace for .NET 1.1. Note: currently it doesn’t play very nicely with Mono, but it may be worth checking out. I think Yurocheck has a great thing going with this.

Further Questions

I spent a fair bit of time learning how to do this and collecting little bits of information. I’m by no means an expert, but if you need further help, you can sometimes find me (HanClinto) hanging out on the chatroom #mono on Cheers!