nanoFramework.System.IO.Ports 1.1.60

The ID prefix of this package has been reserved for one of the owners of this package by Prefix Reserved
.NET Framework
dotnet add package nanoFramework.System.IO.Ports --version 1.1.60
NuGet\Install-Package nanoFramework.System.IO.Ports -Version 1.1.60
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="nanoFramework.System.IO.Ports" Version="1.1.60" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nanoFramework.System.IO.Ports --version 1.1.60
#r "nuget: nanoFramework.System.IO.Ports, 1.1.60"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install nanoFramework.System.IO.Ports as a Cake Addin
#addin nuget:?package=nanoFramework.System.IO.Ports&version=1.1.60

// Install nanoFramework.System.IO.Ports as a Cake Tool
#tool nuget:?package=nanoFramework.System.IO.Ports&version=1.1.60

Quality Gate Status Reliability Rating License NuGet #yourfirstpr Discord

nanoFramework logo

Welcome to the .NET nanoFramework System.IO.Ports Library repository

Build status

Component Build Status NuGet Package
System.IO.Ports Build Status NuGet


You will find detailed examples in the Tests.

Creating the SerialPort

You can create the SerialPort like this:

var port = new SerialPort("COM2");

Note that the port name must be COMx where x is a number.

The GetPortNames method will give you a list of available ports:

var ports = SerialPort.GetPortNames();

You can also directly specify the baud rate and other elements in the constructor:

var port = new SerialPort("COM2", 115200);

Each property can be adjusted, including when the port is open. Be aware that this can generate hazardous behaviors. It is always recommended to change the properties while the port is closed.

Important: You should setup a timeout for the read and write operations. If you have none, read or a write periods may cause theads to be locked for indefinite periods.

port.WriteTimeout = 1000;
port.ReadTimeout = 1000;

Note: some MCU do not support Handshake or specific bit parity even if you can set them up in the constructor.

Opening and Closing the port

The SerialPort can only operate once open and will finish the operations when closed. When disposed, the SerialPort will perform the close operation regardless of any ongoing receive or transmit operations.

var port = new SerialPort("COM2");
// Do a lot of things here, write, read

Read and Write

You have multiple functions to read and write, some are byte related, others string related. Note that string functions will use UTF8 Encoding charset.

Sending and receiving bytes

Example of sending and reading byte arrays:

byte[] toSend = new byte[] { 0x42, 0xAA, 0x11, 0x00 };
byte[] toReceive = new byte[50];
// this will send the 4 bytes:
port.Write(toSend, 0, toSend.Length);
// This will only send the bytes AA and 11:
port.Write(toSend, 1, 2);
// This will check then number of available bytes to read
var numBytesToRead = port.BytesToRead;
// This will read 50 characters:
port.Read(toReceive, 0, toReceive.Length);
// this will read 10 characters and place them at the offset position 3:
port.Read(toReceive, 3, 10);
// Note: in case of time out while reading or writing, you will receive a TimeoutException
// And you can as well read a single byte:
byte oneByte = port.ReadByte();
Sending and receiving string


string toSend = "I ❤ nanoFramework";
// this will send the string encoded finishing by a new line, by default `\n`
// You can change the new line to be anything:
port.NewLine = "❤❤";
// Now it will send 2 hearts as the line ending `WriteLine` and will use 2 hearts as the terminator for `ReadLine`.
// You can change it back to the `\n` default at anytime:
port.NewLine = SerialPort.DefaultNewLine; // default is "\n"
// This will read the existing buffer:
string existingString = port.ReadExisting();
// Note that if it can't properly convert the bytes to a string, you'll get an exception
// This will read a full line, it has to be terminated by the NewLine string.
// If nothing is found ending by the NewLine in the ReadTimeout time frame, a TimeoutException will be raised.
string aFullLine = port.ReadLine();



SerialPort supports events when characters are received.

    // Subscribe to the event
    port.DataReceived += DataReceivedNormalEvent;

    // When you're done, you can as well unsubscribe
    port.DataReceived -= DataReceivedNormalEvent;

private void DataReceivedNormalEvent(object sender, SerialDataReceivedEventArgs e)
    var ser = (SerialPort)sender;
    // Now you can check how many characters are available, read a line for example
    var numBytesToRead = port.BytesToRead;
    string aFullLine = ser.ReadLine();

.NET nanoFramework has a custom API event to watch for a specific character if present during the transmission.

    port.WatchChar = '\r';
    // Subscribe to the event
    port.DataReceived += DataReceivedNormalEvent;

private void DataReceivedNormalEvent(object sender, SerialDataReceivedEventArgs e)
    if (e.EventType == SerialData.WatchChar)
        // The specified character was detected when reading from the serialport.

Feedback and documentation

For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.

Join our Discord community here.


The list of contributors to this project can be found at CONTRIBUTORS.


The nanoFramework Class Libraries are licensed under the MIT license.

Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. For more information see the .NET Foundation Code of Conduct.

.NET Foundation

This project is supported by the .NET Foundation.

Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (16)

Showing the top 5 NuGet packages that depend on nanoFramework.System.IO.Ports:

Package Downloads
nanoFramework.M5Core2 The ID prefix of this package has been reserved for one of the owners of this package by

This package includes the nanoFramework.M5Core2 assembly for .NET nanoFramework C# projects.

nanoFramework.M5Core The ID prefix of this package has been reserved for one of the owners of this package by

This package includes the nanoFramework.M5Core assembly for .NET nanoFramework C# projects.

nanoFramework.Fire The ID prefix of this package has been reserved for one of the owners of this package by

This package includes the nanoFramework.Fire assembly for .NET nanoFramework C# projects.

nanoFramework.Logging.Serial The ID prefix of this package has been reserved for one of the owners of this package by

This package includes the nanoFramework.Logging.Serial assembly (Serial Logging only) for .NET nanoFramework C# projects. There is also a package with the Stream Logging only and another with the basic Debug Logging.

nanoFramework.Tough The ID prefix of this package has been reserved for one of the owners of this package by

This package includes the nanoFramework.Tough assembly for .NET nanoFramework C# projects.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on nanoFramework.System.IO.Ports:

Repository Stars
🍬 Code samples from the nanoFramework team used in testing, proof of concepts and other explorational endeavours
📦 This repo includes .NET nanoFramework implementations for various sensors, chips, displays, hats and drivers
Version Downloads Last updated
1.1.60 18,486 12/28/2022
1.1.57 265 12/28/2022
1.1.54 269 12/27/2022
1.1.46 11,318 10/26/2022
1.1.44 2,632 10/25/2022
1.1.42 608 10/25/2022
1.1.40 825 10/24/2022
1.1.38 643 10/24/2022
1.1.36 875 10/23/2022
1.1.34 2,546 10/22/2022
1.1.31 5,688 10/9/2022
1.1.29 2,306 10/8/2022
1.1.25 11,383 9/15/2022
1.1.22 656 9/15/2022
1.1.18 370 9/15/2022
1.1.16 664 9/15/2022
1.1.6 16,676 8/4/2022
1.1.4 378 8/3/2022
1.1.2 383 8/3/2022 362 8/3/2022 31,655 6/16/2022 6,275 6/13/2022 979 6/8/2022 11,019 5/26/2022 387 5/26/2022
1.0.4 31,243 3/29/2022
1.0.4-preview.3 117 3/29/2022
1.0.3 710 3/28/2022
1.0.3-preview.46 107 3/28/2022
1.0.3-preview.44 112 3/28/2022
1.0.3-preview.42 107 3/28/2022
1.0.3-preview.39 274 3/17/2022
1.0.3-preview.37 203 3/14/2022
1.0.3-preview.35 122 3/14/2022
1.0.3-preview.33 363 2/24/2022
1.0.3-preview.29 197 2/17/2022
1.0.3-preview.25 225 2/8/2022
1.0.3-preview.23 315 2/5/2022
1.0.3-preview.21 120 2/4/2022
1.0.3-preview.18 300 1/28/2022
1.0.3-preview.16 156 1/28/2022
1.0.3-preview.14 128 1/28/2022
1.0.3-preview.12 294 1/21/2022
1.0.3-preview.10 137 1/21/2022
1.0.3-preview.8 127 1/21/2022
1.0.3-preview.6 257 1/12/2022
1.0.3-preview.4 247 12/29/2021
1.0.3-preview.3 130 12/28/2021
1.0.2 1,756 12/3/2021
1.0.2-preview.3 136 12/3/2021
1.0.1 271 12/2/2021
1.0.1-preview.31 135 12/2/2021
1.0.1-preview.29 135 12/2/2021
1.0.1-preview.27 126 12/2/2021
1.0.1-preview.25 138 12/2/2021
1.0.1-preview.21 198 11/4/2021
1.0.1-preview.19 241 11/2/2021
1.0.1-preview.17 173 10/23/2021
1.0.1-preview.15 138 10/22/2021
1.0.1-preview.13 420 10/18/2021
1.0.1-preview.11 358 9/29/2021
1.0.1-preview.9 274 9/26/2021
1.0.1-preview.3 1,113 7/17/2021
1.0.0 8,142 7/16/2021
1.0.0-preview.54 142 7/16/2021
1.0.0-preview.52 162 7/15/2021
1.0.0-preview.50 173 7/14/2021
1.0.0-preview.44 607 6/26/2021
1.0.0-preview.42 200 6/20/2021
1.0.0-preview.40 174 6/18/2021
1.0.0-preview.35 202 6/8/2021
1.0.0-preview.33 195 6/7/2021
1.0.0-preview.31 275 6/5/2021
1.0.0-preview.29 158 6/4/2021
1.0.0-preview.27 392 6/2/2021
1.0.0-preview.25 184 6/1/2021
1.0.0-preview.23 198 5/31/2021
1.0.0-preview.21 198 5/30/2021
1.0.0-preview.19 157 5/29/2021
1.0.0-preview.17 160 5/27/2021
1.0.0-preview.15 149 5/26/2021
1.0.0-preview.12 149 5/24/2021