Skip to content

Commit

Permalink
Added compatability with Net 6 and netstandard2.0, removed Net 5 and …
Browse files Browse the repository at this point in the history
…netcore3.1
  • Loading branch information
ChrisPulman committed Dec 13, 2021
1 parent 18730f7 commit cb6a34e
Show file tree
Hide file tree
Showing 6 changed files with 841 additions and 893 deletions.
13 changes: 7 additions & 6 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Version>1.4.4</Version>
<Version>2.1.1</Version>
<Authors>Chris Pulman</Authors>
<TargetFrameworks>net48;netcoreapp3.1;net5.0;</TargetFrameworks>
<TargetFrameworks>net461;netstandard2.0;net6.0;</TargetFrameworks>
<Description>An Observable Com port extension of System.IO.Ports.SerialPort</Description>
<Copyright>Copyright © https://github.com/ChrisPulman $([System.DateTime]::Now.ToString(yyyy))</Copyright>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand All @@ -12,7 +12,9 @@
<Company>ChrisPulman</Company>
<NoWarn>CS1591</NoWarn>
<Configurations>Debug;Release;PreRelease</Configurations>
<PackageReleaseNotes>Added compatability with Net 5</PackageReleaseNotes>
<PackageReleaseNotes>Added compatability with Net 6 and netstandard2.0, removed Net 5 and netcore3.1</PackageReleaseNotes>
<EnableNETAnalyzers>True</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand All @@ -25,9 +27,8 @@

<ItemGroup Condition="'$(Configuration)'=='Debug'">
<!--<Compile Update="**\*.cs" DependentUpon="I%(Filename).cs" />-->
<PackageReference Include="stylecop.analyzers" Version="1.*" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.*" PrivateAssets="all" />
<PackageReference Include="Roslynator.Analyzers" Version="3.*" PrivateAssets="All" />
<PackageReference Include="stylecop.analyzers" Version="1.2.0-beta.376" PrivateAssets="all" />
<PackageReference Include="Roslynator.Analyzers" Version="3.3.0" PrivateAssets="All" />
<AdditionalFiles Include="$(MSBuildThisFileDirectory)stylecop.json" Link="stylecop.json" />
</ItemGroup>
</Project>
99 changes: 52 additions & 47 deletions SerialPortRx.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,60 @@
using System;
// <copyright file="Program.cs" company="Chris Pulman">
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

using System;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;

namespace CP.IO.Ports.Test
namespace CP.IO.Ports.Test;

internal class Program
{
internal class Program
private static void Main(string[] args)
{
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.AddTo(comdis);
// Subscribe to Exceptions from port
port.ErrorReceived.Subscribe(Console.WriteLine).AddTo(comdis);
port.IsOpenObservable.Subscribe(x => Console.WriteLine($"Port {comPortName} is {(x ? "Open" : "Closed")}")).AddTo(comdis);
// Subscribe to the Data Received
port.DataReceived.BufferUntil(startChar, endChar, 100).Subscribe(data => {
Console.WriteLine(data);
}).AddTo(comdis);
// Subscribe to the Is Open @500ms intervals and write to com port
port.WhileIsOpen(TimeSpan.FromMilliseconds(500)).Subscribe(_ => {
port.Write(dataToWrite);
}).AddTo(comdis);
// Open the Com Port after subscriptions created
port.Open();
} else {
comdis.Dispose();
Console.WriteLine($"Port {comPortName} Disposed");
comdis = new CompositeDisposable();
}
}).ForEach().Subscribe(name => {
// Show available ports
Console.WriteLine(name);
}).AddTo(dis);
Console.ReadLine();
// Cleanup ports
comdis.Dispose();
dis.Dispose();
}
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.AddTo(comdis);

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

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

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

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

// Cleanup ports
comdis.Dispose();
dis.Dispose();
}
}
31 changes: 15 additions & 16 deletions SerialPortRx/IDisposableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@
using System;
using System.Collections.Generic;

namespace CP.IO.Ports
namespace CP.IO.Ports;

/// <summary>
/// IDisposable Extensions.
/// </summary>
public static class IDisposableExtensions
{
/// <summary>
/// IDisposable Extensions.
/// Add disposable(self) to CompositeDisposable(or other ICollection).
/// </summary>
public static class IDisposableExtensions
/// <typeparam name="T">The type.</typeparam>
/// <param name="disposable">The disposable.</param>
/// <param name="container">The container.</param>
/// <returns>Type of T.</returns>
public static T AddTo<T>(this T disposable, ICollection<IDisposable> container)
where T : IDisposable
{
/// <summary>
/// Add disposable(self) to CompositeDisposable(or other ICollection).
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <param name="disposable">The disposable.</param>
/// <param name="container">The container.</param>
/// <returns>Type of T.</returns>
public static T AddTo<T>(this T disposable, ICollection<IDisposable> container)
where T : IDisposable
{
container.Add(disposable);
return disposable;
}
container.Add(disposable);
return disposable;
}
}
Loading

0 comments on commit cb6a34e

Please sign in to comment.