nanoFramework.System.IO.Ports 1.1.86

Prefix Reserved
dotnet add package nanoFramework.System.IO.Ports --version 1.1.86                
NuGet\Install-Package nanoFramework.System.IO.Ports -Version 1.1.86                
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.86" />                
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.86                
#r "nuget: nanoFramework.System.IO.Ports, 1.1.86"                
#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.86

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

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

Usage

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");
port.Open();
// Do a lot of things here, write, read
port.Close();

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

Example:

string toSend = "I ❤ nanoFramework";
port.WriteLine(toSend);
// 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();

Events

Character

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();
}
WatchChar

.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.

Credits

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

License

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)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (21)

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

Package Downloads
nanoFramework.M5Core2

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

nanoFramework.M5Core

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

nanoFramework.Fire

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

nanoFramework.Logging.Serial

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

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
nanoframework/Samples
🍬 Code samples from the nanoFramework team used in testing, proof of concepts and other explorational endeavours
nanoframework/nanoFramework.IoT.Device
📦 This repo includes .NET nanoFramework implementations for various sensors, chips, displays, hats and drivers
Version Downloads Last updated
1.1.86 8,281 5/13/2024
1.1.84 1,701 4/8/2024
1.1.82 492 4/3/2024
1.1.78 5,587 11/9/2023
1.1.75 158 11/9/2023
1.1.60 28,750 12/28/2022
1.1.57 331 12/28/2022
1.1.54 325 12/27/2022
1.1.46 13,006 10/26/2022
1.1.44 3,203 10/25/2022
1.1.42 713 10/25/2022
1.1.40 963 10/24/2022
1.1.38 767 10/24/2022
1.1.36 1,024 10/23/2022
1.1.34 3,093 10/22/2022
1.1.31 6,470 10/9/2022
1.1.29 2,883 10/8/2022
1.1.25 12,547 9/15/2022
1.1.22 786 9/15/2022
1.1.18 449 9/15/2022
1.1.16 779 9/15/2022
1.1.6 18,006 8/4/2022
1.1.4 421 8/3/2022
1.1.2 438 8/3/2022
1.0.7.12 406 8/3/2022
1.0.7.10 34,342 6/16/2022
1.0.7.8 6,809 6/13/2022
1.0.7.6 1,109 6/8/2022
1.0.7.1 11,748 5/26/2022
1.0.6.1 430 5/26/2022
1.0.4 33,354 3/29/2022
1.0.4-preview.3 146 3/29/2022
1.0.3 820 3/28/2022
1.0.3-preview.46 130 3/28/2022
1.0.3-preview.44 140 3/28/2022
1.0.3-preview.42 135 3/28/2022
1.0.3-preview.39 300 3/17/2022
1.0.3-preview.37 231 3/14/2022
1.0.3-preview.35 147 3/14/2022
1.0.3-preview.33 389 2/24/2022
1.0.3-preview.29 223 2/17/2022
1.0.3-preview.25 254 2/8/2022
1.0.3-preview.23 342 2/5/2022
1.0.3-preview.21 148 2/4/2022
1.0.3-preview.18 328 1/28/2022
1.0.3-preview.16 188 1/28/2022
1.0.3-preview.14 155 1/28/2022
1.0.3-preview.12 320 1/21/2022
1.0.3-preview.10 166 1/21/2022
1.0.3-preview.8 154 1/21/2022
1.0.3-preview.6 285 1/12/2022
1.0.3-preview.4 271 12/29/2021
1.0.3-preview.3 151 12/28/2021
1.0.2 1,973 12/3/2021
1.0.2-preview.3 159 12/3/2021
1.0.1 320 12/2/2021
1.0.1-preview.31 160 12/2/2021
1.0.1-preview.29 156 12/2/2021
1.0.1-preview.27 150 12/2/2021
1.0.1-preview.25 169 12/2/2021
1.0.1-preview.21 226 11/4/2021
1.0.1-preview.19 264 11/2/2021
1.0.1-preview.17 201 10/23/2021
1.0.1-preview.15 166 10/22/2021
1.0.1-preview.13 451 10/18/2021
1.0.1-preview.11 390 9/29/2021
1.0.1-preview.9 308 9/26/2021
1.0.1-preview.3 1,136 7/17/2021
1.0.0 9,242 7/16/2021
1.0.0-preview.54 174 7/16/2021
1.0.0-preview.52 182 7/15/2021
1.0.0-preview.50 202 7/14/2021
1.0.0-preview.44 629 6/26/2021
1.0.0-preview.42 222 6/20/2021
1.0.0-preview.40 197 6/18/2021
1.0.0-preview.35 225 6/8/2021
1.0.0-preview.33 218 6/7/2021
1.0.0-preview.31 297 6/5/2021
1.0.0-preview.29 183 6/4/2021
1.0.0-preview.27 420 6/2/2021
1.0.0-preview.25 206 6/1/2021
1.0.0-preview.23 222 5/31/2021
1.0.0-preview.21 220 5/30/2021
1.0.0-preview.19 180 5/29/2021
1.0.0-preview.17 180 5/27/2021
1.0.0-preview.15 173 5/26/2021
1.0.0-preview.12 178 5/24/2021