RxSerialPort 1.2.2

dotnet add package RxSerialPort --version 1.2.2
NuGet\Install-Package RxSerialPort -Version 1.2.2
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="RxSerialPort" Version="1.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RxSerialPort --version 1.2.2
#r "nuget: RxSerialPort, 1.2.2"
#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 RxSerialPort as a Cake Addin
#addin nuget:?package=RxSerialPort&version=1.2.2

// Install RxSerialPort as a Cake Tool
#tool nuget:?package=RxSerialPort&version=1.2.2

RxSerialPort

An extension to the classic System.IO.Ports.SerialPort class, to be able to use reactive extensions.

It enables developers to create observable streams from received data of a serial port or to write data from an observable stream to a serial port in an reactive and functional way of programming.

Create observable stream from SerialPort

Internally managed SerialPort

Use one of the static connect methods from RxSerialPort. There is a connect method for all SerialPort constructors. If there is a need to do additional stuff, a port factory kann be provided.

var eventObservable = RxSerialPort.Connect("COM1");

Do NOT use the serialport from the read function anywhere else! This serialport will be created and opened by the static function upon subscribing to the stream und will also be disposed if the stream is not longer needed.

Externally managed SerialPort

If there is already a serial port available, just use on of the extension methods to connect to the port.

SerialPort serialPort = ...;

var eventObservable = serialPort.Connect()

In this case the serialPort needs to be opened, closed and disposed by the using code! If the port is not opend, no data events will be fired.

Read data from SerialPort

After connecting to the serial port a AndRead extension can be used. For every read method of SerialPort there is a extension function. Plus a specific read funtion can be provided (eg for testing).

var eventsWithData = eventObservable.AndReadLine();
var eventsWithData = eventObservable.AndReadExisting();
var eventsWithData = eventObservable.AndReadTo(string valueToReadTo);
var eventsWithData = eventObservable.AndReadByte();
var eventsWithData = eventObservable.AndReadChar();

var eventsWithData = eventObservable.AndRead<TResult>(serialPort => { /* Do the reading */ });

Write observable stream data to SerialPort

There are different extension methods to write data form an observable stream to a serialport. The SerialPort can be managed internally or externally. A completed and error Action can be provided optionally. Plus a specific write funtion can be provided (eg for testing).

Internally managed SerialPort

IObservable<bytes[]> bytesToWrite = ...
IDisposable subscription = bytesToWrite.WriteTo("COM1");

IObservable<char[]> charsToWrite = ...
IDisposable subscription = charsToWrite.WriteTo("COM1");

IObservable<string> stringsToWrite = ...
IDisposable subscription = stringsToWrite.WriteTo("COM1");
IDisposable subscription = stringsToWrite.WriteLineTo("COM1");

IDisposable subscription = stringsToWrite.WriteLineTo("COM1", (serialPort, data) => { /* Do the writing */});

Externally managed SerialPort

SerialPort serialPort = ...

IObservable<bytes[]> bytesToWrite = ...
IDisposable subscription = bytesToWrite.WriteTo(serialPort);

IObservable<char[]> charsToWrite = ...
IDisposable subscription = charsToWrite.WriteTo(serialPort);

IObservable<string> stringsToWrite = ...
IDisposable subscription = stringsToWrite.WriteTo(serialPort);
IDisposable subscription = stringsToWrite.WriteLineTo(serialPort);

IDisposable subscription = stringsToWrite.WriteLineTo(serialPort, (serialPort, data) => { /* Do the writing */});

Testing

The test project consists mainly of tests to test the correct creation of the observbales and observers. To test with real serial ports the tag TEST_WITH_REAL_PORTS needs to be set to true.

Virtual COM Ports are very easy to setup with NetBurner VirtualComPort. One port needs to send its data to the other port.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.2 72 4/24/2024
1.1.86 181 11/14/2023
1.1.28 134 5/23/2023
1.1.3 316 11/12/2022
1.0.44 362 9/6/2022
1.0.30 379 5/20/2022
1.0.23 358 5/20/2022
1.0.11 370 5/19/2022