Mtf.Serial 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Mtf.Serial --version 1.0.2                
NuGet\Install-Package Mtf.Serial -Version 1.0.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="Mtf.Serial" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Mtf.Serial --version 1.0.2                
#r "nuget: Mtf.Serial, 1.0.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 Mtf.Serial as a Cake Addin
#addin nuget:?package=Mtf.Serial&version=1.0.2

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

Mtf.Serial Library Documentation

Overview

The Mtf.Serial library provides a SerialDevice class designed for serial communication, including features for connecting to and configuring serial ports, handling data reception, and managing errors. This document covers the installation, properties, methods, events, and usage examples for effective serial communication in .NET applications.

Installation

To install the Mtf.Serial package, use the following steps:

  1. Add Package: Open the terminal in your project directory and run:

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

    using Mtf.Serial;
    

Class: SerialDevice

The SerialDevice class facilitates serial communication with configurations for port name, baud rate, and data handling. This class also includes events to monitor data and errors and provides both synchronous and asynchronous methods for data reading and writing.

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)

  • Parameters:
    • portName: The serial port name (e.g., "COM1"). Defaults to the first available port if unspecified.
    • baudRate: Communication speed. Default is 9600.
    • parity, dataBits, stopBits, handshake, dataTerminalReady, and requestToSend: Optional settings to configure communication behavior.

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.

Methods

Connection Management
  • void Connect(bool subscribeToDefaultEvents)
    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.

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

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

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

  • void Write(string message)
    Writes a string message to the serial port.

  • Task WriteAsync(string message)
    Writes a message asynchronously to the serial port.

  • void Write(string message, Encoding encoding)
    Writes a string message with specified encoding.

  • Task WriteAsync(string message, Encoding encoding)
    Writes a message asynchronously with specified encoding.

Event Handling

The following events enable you to monitor serial communication:

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.

Example Usage

using Mtf.Serial;
using System;
using System.Text;

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

        // 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

  • Ensure exception handling for port access and communication errors.
  • Use the Logger property to capture and log serial events for debugging and monitoring purposes.
Product Compatible and additional computed target framework versions.
.NET net8.0-windows7.0 is compatible.  net9.0-windows7.0 is compatible. 
.NET Framework net481 is compatible. 
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.7 39 11/22/2024
1.0.6 77 11/2/2024
1.0.3 77 11/2/2024
1.0.2 80 11/1/2024
1.0.0 83 11/1/2024