Π·Π΅Ρ€ΠΊΠ°Π»ΠΎ ΠΈΠ·
1
0
Π€ΠΎΡ€ΠΊΠ½ΡƒΡ‚ΡŒ 0
πŸ“¦ System.IO.Ports library for .NET nanoFramework.
ΠŸΠ΅Ρ€Π΅ΠΉΡ‚ΠΈ ΠΊ Ρ„Π°ΠΉΠ»Ρƒ
github-actions[bot] 5987f66cff
Update 1 NuGet dependencies
2022-03-28 12:41:24 +01:00
.github/workflows Work CI-CD 2022-01-10 23:49:00 +00:00
System.IO.Ports Update 1 NuGet dependencies 2022-03-28 12:41:24 +01:00
Tests/UnitTestsSerialPort Update 1 NuGet dependencies 2022-03-28 12:41:24 +01:00
assets Work CI-CD 2021-11-25 11:11:35 +00:00
config Work CI-CD 2021-05-25 00:21:36 +01:00
.github_changelog_generator Work CI-CD 2021-05-24 23:45:14 +01:00
.gitignore Adding core elements, basic tests 2021-05-17 15:47:37 +02:00
CHANGELOG.md Update CHANGELOG for v1.0.3 2022-03-28 11:06:09 +00:00
CODE_OF_CONDUCT.md Work CI-CD 2021-06-18 17:27:40 +01:00
CONTRIBUTING.md Work CI-CD 2021-05-24 23:45:14 +01:00
LICENSE.md Work CI-CD 2021-05-25 00:21:36 +01:00
NuGet.Config Work CI-CD 2022-02-17 17:27:27 +00:00
README.md Work CI-CD 2021-11-25 11:11:35 +00:00
azure-pipelines.yml Work CI-CD 2022-02-04 07:53:50 +00:00
nanoFramework.System.IO.Ports.nuspec Update 1 NuGet dependencies 2022-03-28 12:41:24 +01:00
nanoFramework.System.IO.Ports.sln Work CI-CD 2022-02-04 07:53:50 +00:00
template.vssettings Adding core elements, basic tests 2021-05-17 15:47:37 +02:00
version.json Set version to '1.0.3-preview.{height}' 2021-12-03 00:30:50 +00:00

README.md

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.