Mtf.Serial 1.0.6

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

// Install Mtf.Serial as a Cake Tool
#tool nuget:?package=Mtf.Serial&version=1.0.6                

Mtf.Serial Library Documentation

Overview

The Mtf.Serial library offers a SerialDevice class designed for robust serial communication with configuration options for port parameters, enhanced data reception handling, and logging integration using ILogger. The library provides both synchronous and asynchronous methods for data handling, connection management, and error tracking, making it well-suited for .NET applications requiring reliable serial communications.

Installation

  1. Install the Package: Run the following command in your project directory:

    dotnet add package Mtf.Serial
    
  2. Add the Namespace: Include the Mtf.Serial namespace in your code file:

    using Mtf.Serial;
    

Class: SerialDevice

The SerialDevice class simplifies serial port communication and configuration, with customizable settings for port name, baud rate, encoding, and connection settings. Logging actions are included to assist with error and debug tracking.

Constructor

SerialDevice(string portName = "", int baudRate = 9600, Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One, Handshake handshake = Handshake.None, bool dataTerminalReady = false, bool requestToSend = false, bool discardNull = false)

  • Parameters:
    • portName: The serial port name (e.g., "COM1"). Defaults to the first available port if unspecified.
    • baudRate: Communication speed. Default is 9600.
    • Additional parameters configure parity, data bits, stop bits, handshake, DTR/RTS, and null discarding options.

Properties

Property Type Description
AppendCarriageReturn bool Appends a carriage return (\r) to outgoing messages if set to true.
AppendLineFeed bool Appends a line feed (\n) to outgoing messages if set to true.
Encoding Encoding Defines the character encoding for messages (default is UTF-8).
Logger ILogger An ILogger instance for logging serial events and errors.
PortName string The name of the connected serial port.
BytesToRead int Number of bytes available to read from the buffer.

Methods

Connection Management
  • void Connect(bool subscribeToDefaultEvents = true)
    Connects to the serial port. If subscribeToDefaultEvents is true, it subscribes to default data and error event handlers.

  • void Disconnect()
    Disconnects from the serial port and unsubscribes from event handlers.

  • Dispose()
    Releases all resources used by SerialDevice and disconnects if connected. The destructor also invokes cleanup to ensure proper disposal.

Data Transmission
  • string Read()
    Reads available data from the port as a string, using the set encoding.

  • string Read(Encoding encoding)
    Reads data with the specified encoding.

  • int Read(byte[] buffer)
    Reads data into a byte array and returns the number of bytes read.

  • void Write(byte[] buffer)
    Writes a byte array to the serial port without any modifications.

  • void WriteAsync(byte[] buffer)
    Asynchronously writes a byte array to the serial port without any modifications.

  • void Write(byte[] buffer, int offset, int count)
    Writes a specified number of bytes from the byte array, starting at the given offset, to the serial port without any modifications.

  • void WriteAsync(byte[] buffer, int offset, int count)
    Asynchronously writes a specified number of bytes from the byte array, starting at the given offset, to the serial port without any modifications.

  • void Write(string message)
    Writes a string message to the serial port using the specified encoding. If AppendCarriageReturn or AppendLineFeed is set, it will modify the sent bytes accordingly."

  • Task WriteAsync(string message)
    Writes a message asynchronously to the serial port. If AppendCarriageReturn or AppendLineFeed is set, it will modify the sent bytes accordingly."

  • void Write(string message, Encoding encoding)
    Writes a string message with specified encoding. If AppendCarriageReturn or AppendLineFeed is set, it will modify the sent bytes accordingly."

  • Task WriteAsync(string message, Encoding encoding)
    Writes a message asynchronously with specified encoding. If AppendCarriageReturn or AppendLineFeed is set, it will modify the sent bytes accordingly."

Event Handling
Event Description
RawDataReceived Triggered when raw binary data is received.
DataReceived Triggered when processed data is available.
ErrorReceived Triggered when an error occurs during serial communication.

Logging

The SerialDevice class supports logging through an ILogger instance:

  • Error Logging: Logs errors during communication with logErrorAction.
  • Debug Logging: Logs debug messages for connection and data events using logDebugAction.

Example Usage

using Mtf.Serial;
using Microsoft.Extensions.Logging;
using System.Text;

public class SerialCommunicationExample
{
    public void Example()
    {
        // Initialize SerialDevice with default settings
        var serialDevice = new SerialDevice("COM3", 9600)
        {
            Logger = new ConsoleLogger<SerialDevice>()
        };

        // Subscribe to events
        serialDevice.RawDataReceived += (sender, args) =>
        {
            Console.WriteLine($"Raw data received: {BitConverter.ToString(args.Data)}");
        };
        serialDevice.DataReceived += (sender, args) =>
        {
            Console.WriteLine($"Data received: {args.Data}");
        };
        serialDevice.ErrorReceived += (sender, args) =>
        {
            Console.WriteLine($"Error received: {args}");
        };

        // Connect and write data
        serialDevice.Connect(true);
        serialDevice.Write("Hello, Serial Port!", Encoding.UTF8);

        // Read response
        var response = serialDevice.Read();
        Console.WriteLine($"Response: {response}");

        // Disconnect from the device
        serialDevice.Disconnect();
    }
}

Notes

  • Logging: Use the Logger property to capture and log serial events for monitoring.
  • Thread Safety: The SerialDevice class ensures thread safety with a connection lock.
  • Error Handling: Ensure exception handling for port access errors and communication issues.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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.0.6 69 11/2/2024
1.0.3 70 11/2/2024
1.0.2 73 11/1/2024
1.0.0 75 11/1/2024