crozone.SerialPorts.LinuxSerialPort 2.0.3

A managed Linux Serial Port implementation targeting netstandard 2.0. Uses /bin/stty to avoid native code compilation and interop.

Install-Package crozone.SerialPorts.LinuxSerialPort -Version 2.0.3
dotnet add package crozone.SerialPorts.LinuxSerialPort --version 2.0.3
<PackageReference Include="crozone.SerialPorts.LinuxSerialPort" Version="2.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add crozone.SerialPorts.LinuxSerialPort --version 2.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: crozone.SerialPorts.LinuxSerialPort, 2.0.3"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install crozone.SerialPorts.LinuxSerialPort as a Cake Addin
#addin nuget:?package=crozone.SerialPorts.LinuxSerialPort&version=2.0.3

// Install crozone.SerialPorts.LinuxSerialPort as a Cake Tool
#tool nuget:?package=crozone.SerialPorts.LinuxSerialPort&version=2.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

crozone.SerialPorts.LinuxSerialPort

NuGet
license

A managed Linux Serial Port implementation targeting netstandard2.0.
This implementation performs all serial port setup by calling the /bin/stty binary, rather than relying on any native interop directly. This means it should work on any system that has a POSIX compatible /bin/stty binary.

About

This SerialPort class is intended to offer similar functionality to the System.IO.Ports.SerialPort class provided by Microsoft. However, although much of the interface is the same or similar, it is not intended to be a direct drop in replacement.

Most basic functionality is covered, including the configuration of the BaudRate, DataBits, StopBits, Handshake, and Parity.

The class implements the shared ISerialPort interface from crozone.SerialPorts.Abstractions. This allows it to be compatible with the crozone.SerialPorts.WindowsSerialPort wrapper for System.IO.Ports.SerialPort, which makes cross-platform development easier.

Properties

The SerialPort.BaseStream property provides the underlying Stream used to read and write from the serial port. It is a direct filestream opened on the serial port file. The SerialPort.Read() and SerialPort.Write() methods are absent from the SerialPort class itself (as an aside, these are problematic in the Microsoft implementation anyway). The SerialPort.DataReceived event is also absent (and it is also unreliable in the Microsoft implementation, and difficult to implement correctly).

SerialPort.EnableRawMode must be set to true in order to disable Linux TTY behaviour, and since v1.1.0 is set to true by default. This will be the desired behaviour for most people and allow the reading/writing of raw bytes to the serial port without the interference of the kernel TTY layer.

SerialPort.MinimumBytesToRead and SerialPort.ReadTimeout allow the blocking behaviour of BaseStream.Read() to be modified.

SerialPort.MinimumBytesToRead corresponds to the stty min parameter. MinimumBytesToRead specifies the minimum number of bytes to be read before BaseStream.Read() will return. BaseStream.Read() will only return after MinimumBytesToRead bytes have been read, or the timeout has been reached. Setting MinimumBytesToRead to 0 will cause BaseStream.Read() to never block, and instantly return whatever bytes are available in the buffer, even if the buffer is empty.

SerialPort.ReadTimeout corresponds to the stty time parameter. ReadTimeout specifies the number of milliseconds a BaseStream.Read() will block for, before it times out. After it times out, it will return whatever data has been read, which may be zero bytes. Due to stty constraints, the time-span will be rounded to the nearest tenth of a second (100ms resolution). A ReadTimeout of 0 specifies an infinite timeout. When an infinite timeout is set, the BaseStream.Read() will only return after MinimumBytesToRead bytes have been read.

SerialPort.EnableDrain controls the use of the stty [-]drain setting. If drain is enabled (true), stty will attempt to flush the serial port write buffer before applying any configuration to the serial port.
This is problematic if the serial port has data in the write buffer and flow control is enabled, since the buffer may never flush, causing stty to hang indefinitely.

If your code is hanging during LinuxSerialPort.Open(), this is likely the bug you are seeing.

To fix this, set SerialPort.EnableDrain = false. This is only possible if [-]drain is available in your version of stty. [-]drain is not a POSIX compatible command, and older/different versions of stty may not have it available, so be sure to check before setting this. All versions of stty use drain enabled behaviour by default, including the stty versions that to not have the option available.

crozone.SerialPorts.LinuxSerialPort

NuGet
license

A managed Linux Serial Port implementation targeting netstandard2.0.
This implementation performs all serial port setup by calling the /bin/stty binary, rather than relying on any native interop directly. This means it should work on any system that has a POSIX compatible /bin/stty binary.

About

This SerialPort class is intended to offer similar functionality to the System.IO.Ports.SerialPort class provided by Microsoft. However, although much of the interface is the same or similar, it is not intended to be a direct drop in replacement.

Most basic functionality is covered, including the configuration of the BaudRate, DataBits, StopBits, Handshake, and Parity.

The class implements the shared ISerialPort interface from crozone.SerialPorts.Abstractions. This allows it to be compatible with the crozone.SerialPorts.WindowsSerialPort wrapper for System.IO.Ports.SerialPort, which makes cross-platform development easier.

Properties

The SerialPort.BaseStream property provides the underlying Stream used to read and write from the serial port. It is a direct filestream opened on the serial port file. The SerialPort.Read() and SerialPort.Write() methods are absent from the SerialPort class itself (as an aside, these are problematic in the Microsoft implementation anyway). The SerialPort.DataReceived event is also absent (and it is also unreliable in the Microsoft implementation, and difficult to implement correctly).

SerialPort.EnableRawMode must be set to true in order to disable Linux TTY behaviour, and since v1.1.0 is set to true by default. This will be the desired behaviour for most people and allow the reading/writing of raw bytes to the serial port without the interference of the kernel TTY layer.

SerialPort.MinimumBytesToRead and SerialPort.ReadTimeout allow the blocking behaviour of BaseStream.Read() to be modified.

SerialPort.MinimumBytesToRead corresponds to the stty min parameter. MinimumBytesToRead specifies the minimum number of bytes to be read before BaseStream.Read() will return. BaseStream.Read() will only return after MinimumBytesToRead bytes have been read, or the timeout has been reached. Setting MinimumBytesToRead to 0 will cause BaseStream.Read() to never block, and instantly return whatever bytes are available in the buffer, even if the buffer is empty.

SerialPort.ReadTimeout corresponds to the stty time parameter. ReadTimeout specifies the number of milliseconds a BaseStream.Read() will block for, before it times out. After it times out, it will return whatever data has been read, which may be zero bytes. Due to stty constraints, the time-span will be rounded to the nearest tenth of a second (100ms resolution). A ReadTimeout of 0 specifies an infinite timeout. When an infinite timeout is set, the BaseStream.Read() will only return after MinimumBytesToRead bytes have been read.

SerialPort.EnableDrain controls the use of the stty [-]drain setting. If drain is enabled (true), stty will attempt to flush the serial port write buffer before applying any configuration to the serial port.
This is problematic if the serial port has data in the write buffer and flow control is enabled, since the buffer may never flush, causing stty to hang indefinitely.

If your code is hanging during LinuxSerialPort.Open(), this is likely the bug you are seeing.

To fix this, set SerialPort.EnableDrain = false. This is only possible if [-]drain is available in your version of stty. [-]drain is not a POSIX compatible command, and older/different versions of stty may not have it available, so be sure to check before setting this. All versions of stty use drain enabled behaviour by default, including the stty versions that to not have the option available.

Release Notes

Use updated abstractions version

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.0.3 435 9/2/2019
2.0.2 269 8/28/2019