-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added compatability with Net 6 and netstandard2.0, removed Net 5 and …
…netcore3.1
- Loading branch information
1 parent
18730f7
commit cb6a34e
Showing
6 changed files
with
841 additions
and
893 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.