nanoFramework.System.IO.Ports 1.0.4

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package nanoFramework.System.IO.Ports --version 1.0.4                
NuGet\Install-Package nanoFramework.System.IO.Ports -Version 1.0.4                
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.0.4" />                
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.0.4                
#r "nuget: nanoFramework.System.IO.Ports, 1.0.4"                
#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.0.4

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

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
System.IO.Ports (preview) 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 gi you a list of available ports:

var ports = SerialPort.GetPortNames();

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

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

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

Important: you should setup a timeout for the read and write operations. If you have none, while operating a read or a write, you will wait indefinitely to read or write that everything is received or sent.

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

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

Opening and Closing the port

The port can only be in operation once open and will finish his operations when closed. If you dispose the SerialPort, it will close it before.

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 the string one will use the Enconding charset that you will define. By default, this is UTF8.

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

You can as well write and read strings:

string toSend = "I ❤ nanoFramework";
port.WriteLine(toSend);
// this will send the string encoded finishing by a new line, by default \r\n
// You can change the new line by anything:
port.NewLine = "❤❤";
// Now it will send the 2 hearts as the end of line while operating a ReadLine or WriteLine
// You can ad anytime change it back:
port.NewLine = SerialPort.DefaultNewLine; // default is "\r\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

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

.NET nanoFramework has a specific API to watch for a specific character if present at the end of the transmission.

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

private void DataReceivedNormalEvent(object sender, SerialDataReceivedEventArgs e)
{
    if (e.EventType == SerialData.WatchChar)
    {
        // We have our special character at the end of the transmission
    }
}

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 (3)

Showing the top 3 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
nanoframework/nanoFramework.M5Stack
:package: Board support package for M5Stack, M5StickC and M5StickCPlus for .NET nanoFramework
Version Downloads Last updated
1.1.86 9,308 5/13/2024
1.1.84 1,727 4/8/2024
1.1.82 503 4/3/2024
1.1.78 5,634 11/9/2023
1.1.75 168 11/9/2023
1.1.60 28,917 12/28/2022
1.1.57 337 12/28/2022
1.1.54 331 12/27/2022
1.1.46 13,033 10/26/2022
1.1.44 3,209 10/25/2022
1.1.42 719 10/25/2022
1.1.40 968 10/24/2022
1.1.38 772 10/24/2022
1.1.36 1,029 10/23/2022
1.1.34 3,098 10/22/2022
1.1.31 6,475 10/9/2022
1.1.29 2,888 10/8/2022
1.1.25 12,553 9/15/2022
1.1.22 791 9/15/2022
1.1.18 454 9/15/2022
1.1.16 784 9/15/2022
1.1.6 18,028 8/4/2022
1.1.4 426 8/3/2022
1.1.2 443 8/3/2022
1.0.7.12 411 8/3/2022
1.0.7.10 34,347 6/16/2022
1.0.7.8 6,814 6/13/2022
1.0.7.6 1,114 6/8/2022
1.0.7.1 11,753 5/26/2022
1.0.6.1 435 5/26/2022
1.0.4 33,359 3/29/2022
1.0.4-preview.3 149 3/29/2022
1.0.3 825 3/28/2022
1.0.3-preview.46 133 3/28/2022
1.0.3-preview.44 143 3/28/2022
1.0.3-preview.42 138 3/28/2022
1.0.3-preview.39 305 3/17/2022
1.0.3-preview.37 234 3/14/2022
1.0.3-preview.35 150 3/14/2022
1.0.3-preview.33 394 2/24/2022
1.0.3-preview.29 226 2/17/2022
1.0.3-preview.25 257 2/8/2022
1.0.3-preview.23 345 2/5/2022
1.0.3-preview.21 151 2/4/2022
1.0.3-preview.18 331 1/28/2022
1.0.3-preview.16 191 1/28/2022
1.0.3-preview.14 158 1/28/2022
1.0.3-preview.12 325 1/21/2022
1.0.3-preview.10 169 1/21/2022
1.0.3-preview.8 157 1/21/2022
1.0.3-preview.6 288 1/12/2022
1.0.3-preview.4 274 12/29/2021
1.0.3-preview.3 154 12/28/2021
1.0.2 1,981 12/3/2021
1.0.2-preview.3 162 12/3/2021
1.0.1 325 12/2/2021
1.0.1-preview.31 163 12/2/2021
1.0.1-preview.29 159 12/2/2021
1.0.1-preview.27 153 12/2/2021
1.0.1-preview.25 172 12/2/2021
1.0.1-preview.21 229 11/4/2021
1.0.1-preview.19 267 11/2/2021
1.0.1-preview.17 204 10/23/2021
1.0.1-preview.15 169 10/22/2021
1.0.1-preview.13 454 10/18/2021
1.0.1-preview.11 393 9/29/2021
1.0.1-preview.9 311 9/26/2021
1.0.1-preview.3 1,139 7/17/2021
1.0.0 9,289 7/16/2021
1.0.0-preview.54 177 7/16/2021
1.0.0-preview.52 185 7/15/2021
1.0.0-preview.50 205 7/14/2021
1.0.0-preview.44 632 6/26/2021
1.0.0-preview.42 225 6/20/2021
1.0.0-preview.40 200 6/18/2021
1.0.0-preview.35 228 6/8/2021
1.0.0-preview.33 221 6/7/2021
1.0.0-preview.31 300 6/5/2021
1.0.0-preview.29 186 6/4/2021
1.0.0-preview.27 423 6/2/2021
1.0.0-preview.25 209 6/1/2021
1.0.0-preview.23 225 5/31/2021
1.0.0-preview.21 223 5/30/2021
1.0.0-preview.19 183 5/29/2021
1.0.0-preview.17 183 5/27/2021
1.0.0-preview.15 176 5/26/2021
1.0.0-preview.12 181 5/24/2021