Giter Club home page Giter Club logo

serialportrx's Introduction

SerialPortRx

A Reactive Serial Port Library This serial port is configured to provide a stream of data Read and accept a stream of Write requests

SerialPortRx CI-Build Nuget Nuget

An Example of the usage of SerialPortRx

using System;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using ReactiveMarbles.Extensions;

namespace CP.IO.Ports.Test;

internal static class Program
{
    private static void Main(string[] args)
    {
        const string comPortName = "COM1";

        // configure the data to write, this can be a string, a byte array, or a char array
        const string dataToWrite = "DataToWrite";
        var dis = new CompositeDisposable();

        // Setup the start of message and end of message
        var startChar = 0x21.AsObservable();
        var endChar = 0x0a.AsObservable();

        // Create a disposable for each COM port to allow automatic disposal upon loss of COM port
        var comdis = new CompositeDisposable();

        // Subscribe to com ports available
        SerialPortRx.PortNames().Do(x =>
        {
            if (comdis?.Count == 0 && x.Contains(comPortName))
            {
                // Create a port
                var port = new SerialPortRx(comPortName, 9600);
                port.DisposeWith(comdis);

                // Subscribe to Exceptions from port
                port.ErrorReceived.Subscribe(Console.WriteLine).DisposeWith(comdis);
                port.IsOpenObservable.Subscribe(x => Console.WriteLine($"Port {comPortName} is {(x ? "Open" : "Closed")}")).DisposeWith(comdis);

                // Subscribe to the Data Received
                port.DataReceived.BufferUntil(startChar, endChar, 100).Subscribe(data => Console.WriteLine(data)).DisposeWith(comdis);

                // Subscribe to the Is Open @500ms intervals and write to com port
                port.WhileIsOpen(TimeSpan.FromMilliseconds(500)).Subscribe(_ => port.Write(dataToWrite)).DisposeWith(comdis);

                // Open the Com Port after subscriptions created
                port.Open();
            }
            else
            {
                comdis?.Dispose();
                Console.WriteLine($"Port {comPortName} Disposed");
                comdis = [];
            }
        }).ForEach().Subscribe(name =>
        {
            // Show available ports
            Console.WriteLine(name);
        }).DisposeWith(dis);
        Console.ReadLine();

        // Cleanup ports
        comdis.Dispose();
        dis.Dispose();
    }
}

serialportrx's People

Contributors

chrispulman avatar dependabot-preview[bot] avatar dependabot[bot] avatar professordave avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

serialportrx's Issues

Does not support encoding

This is a great Rx serial library, but the default encoding is set to ASCII. In order to read raw (non-encoded) bytes from the device the encoding needs to be set to extended ASCII to avoid data corruption. I have made the necessary code change and would like to push if you would like.

Does error handling work? Or am I missing how it is supposed to work

What is the intent with error handling? And the infinite retry logic. Am I missing something with the retry logic? If the port is bad retrying forever is not the correct thing to do.

The task-> run is wrapped by a calling function open(), which means the caller cannot await any errors? Or can it? I am somewhat new to the latest c# features.

Exceptions thrown from the underlying open are not trapped for at all, in contrast to the write which does trap for errors.

However, even the errors from write are trapped and thrown to the "connect" But the connect is done with a task => run so that those errors are lost, I think.

I am wondering if it is as simple as for real physical ports, open and write never fail. And therefore there was no need for error handling as the library writer here only used with real physical com ports??

I am using this with USB Virtual com ports and the open and the writes can fail

Readme.md AddTo(dis) does not work

In your example code, the AddTo(dis) does not work. It will lead to an error:

CS1061 'IDisposable' does not contain a definition for 'AddTo' and no extension method 'AddTo' accepting a first argument of type 'IDisposable' could be found (are you missing a using directive or an assembly reference?)

Add EventLoopScheduler in timeout handling

When using your code https://github.com/ChrisPulman/SerialPortRx/blob/master/SerialPortRx/SerialPortRxMixins.cs#L125, you will see that (for example 100ms) the delay is not working as expected:

            Observable.Interval(TimeSpan.FromMilliseconds(1)).Subscribe(_ => {
                elapsedTime++;
                if (elapsedTime > timeOut)
                {
                    Trace.WriteLine(DateTime.Now.ToString("HH:mm:ss:FFF"));

If you additional add an EventLoopScheduler, everything works as expected

            Observable.Interval(TimeSpan.FromMilliseconds(1), new EventLoopScheduler()).Subscribe(_ => {
                elapsedTime++;
                if (elapsedTime > timeOut)
                {
                    Trace.WriteLine(DateTime.Now.ToString("HH:mm:ss:FFF"));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.