From d2e7ccd850e788817dd13c92c71e8f9f913ec923 Mon Sep 17 00:00:00 2001 From: Laurent Ellerbach Date: Fri, 28 May 2021 19:04:25 +0300 Subject: [PATCH] migrating to System.IO.Ports --- README.md | 4 +-- Tests/UnitTestDebugLogging/SerialTest.cs | 2 +- .../UnitTestDebugLogging.nfproj | 28 +++++++++++-------- Tests/UnitTestDebugLogging/packages.config | 6 ++-- nanoFramework.Logging.Serial/SerialLogger.cs | 21 ++++++++------ .../SerialLoggerFactory.cs | 22 +++++++-------- .../nanoFramework.Logging.Serial.nfproj | 16 ++++++----- nanoFramework.Logging.Serial/packages.config | 4 +-- 8 files changed, 56 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index f33c1f2..6d10d36 100644 --- a/README.md +++ b/README.md @@ -109,8 +109,8 @@ Note that you can adjust the baud speed and all other elements. Or directly using a SerialLogger: ```csharp -SerialDevice _serial; -_serial = SerialDevice.FromId("COM6", 115200); +SerialPort _serial; +_serial = new SerialPort("COM6", 115200); SerialLogger _logger = new SerialLogger(ref _serial); _logger.MinLogLevel = LogLevel.Trace; _logger.LogTrace("This is a trace"); diff --git a/Tests/UnitTestDebugLogging/SerialTest.cs b/Tests/UnitTestDebugLogging/SerialTest.cs index a97e994..5a2cd13 100644 --- a/Tests/UnitTestDebugLogging/SerialTest.cs +++ b/Tests/UnitTestDebugLogging/SerialTest.cs @@ -7,7 +7,7 @@ using nanoFramework.Logging.Serial; using nanoFramework.TestFramework; using System; -using Windows.Devices.SerialCommunication; +using System.IO.Ports; #if BUIID_FOR_ESP32 using nanoFramework.Hardware.Esp32; #endif diff --git a/Tests/UnitTestDebugLogging/UnitTestDebugLogging.nfproj b/Tests/UnitTestDebugLogging/UnitTestDebugLogging.nfproj index 9383b2a..32667a1 100644 --- a/Tests/UnitTestDebugLogging/UnitTestDebugLogging.nfproj +++ b/Tests/UnitTestDebugLogging/UnitTestDebugLogging.nfproj @@ -51,29 +51,33 @@ ..\..\packages\nanoFramework.Runtime.Events.1.9.0-preview.26\lib\nanoFramework.Runtime.Events.dll True + + ..\..\packages\nanoFramework.System.Collections.1.2.0-preview.55\lib\nanoFramework.System.Collections.dll + True + True + ..\..\packages\nanoFramework.System.Text.1.1.1-preview.51\lib\nanoFramework.System.Text.dll True - - ..\..\packages\nanoFramework.TestFramework.1.0.121\lib\nanoFramework.TestFramework.dll + + ..\..\packages\nanoFramework.TestFramework.1.0.122\lib\nanoFramework.TestFramework.dll True + True - ..\..\packages\nanoFramework.TestFramework.1.0.121\lib\nanoFramework.UnitTestLauncher.exe + ..\..\packages\nanoFramework.TestFramework.1.0.122\lib\nanoFramework.UnitTestLauncher.exe True + True ..\..\packages\nanoFramework.System.IO.FileSystem.1.0.1-preview.16\lib\System.IO.FileSystem.dll True - - ..\..\packages\nanoFramework.Windows.Devices.SerialCommunication.1.3.4-preview.94\lib\Windows.Devices.SerialCommunication.dll - True - - - ..\..\packages\nanoFramework.Windows.Storage.Streams.1.12.2-preview.5\lib\Windows.Storage.Streams.dll + + ..\..\packages\nanoFramework.System.IO.Ports.1.0.0-preview.17\lib\System.IO.Ports.dll True + True @@ -92,11 +96,11 @@ - + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}. - + - + \ No newline at end of file diff --git a/Tests/UnitTestDebugLogging/packages.config b/Tests/UnitTestDebugLogging/packages.config index 9ab663e..e820d37 100644 --- a/Tests/UnitTestDebugLogging/packages.config +++ b/Tests/UnitTestDebugLogging/packages.config @@ -3,9 +3,9 @@ + + - - - + \ No newline at end of file diff --git a/nanoFramework.Logging.Serial/SerialLogger.cs b/nanoFramework.Logging.Serial/SerialLogger.cs index 01d83a1..36a4cca 100644 --- a/nanoFramework.Logging.Serial/SerialLogger.cs +++ b/nanoFramework.Logging.Serial/SerialLogger.cs @@ -5,9 +5,9 @@ using Microsoft.Extensions.Logging; using System; +using System.IO; +using System.IO.Ports; using System.Reflection; -using Windows.Devices.SerialCommunication; -using Windows.Storage.Streams; namespace nanoFramework.Logging.Serial { @@ -16,18 +16,22 @@ namespace nanoFramework.Logging.Serial /// public class SerialLogger : ILogger { - private readonly DataWriter _outputDataWriter; + private readonly SerialPort _serialPort; /// /// Creates a new instance of the /// /// The serial port to use /// The logger name - public SerialLogger(ref SerialDevice serialDevice, string loggerName) + public SerialLogger(ref SerialPort serialDevice, string loggerName) { - SerialDevice = serialDevice; + _serialPort = serialDevice; + if (!_serialPort.IsOpen) + { + _serialPort.Open(); + } + LoggerName = loggerName; - _outputDataWriter = new DataWriter(serialDevice.OutputStream); MinLogLevel = LogLevel.Debug; } @@ -39,7 +43,7 @@ public SerialLogger(ref SerialDevice serialDevice, string loggerName) /// /// Name of the serial device /// - public SerialDevice SerialDevice { get; } + public SerialPort SerialPort => _serialPort; /// /// Sets the minimum log level @@ -64,8 +68,7 @@ public void Log(LogLevel logLevel, EventId eventId, string state, Exception exce msgSerial = $"{(string)format.Invoke(null, new object[] { LoggerName, logLevel, eventId, state, exception })}\r\n"; } - _outputDataWriter.WriteString(msgSerial); - _outputDataWriter.Store(); + _serialPort.Write(msgSerial); } } } diff --git a/nanoFramework.Logging.Serial/SerialLoggerFactory.cs b/nanoFramework.Logging.Serial/SerialLoggerFactory.cs index bd8b3fd..ec0ce15 100644 --- a/nanoFramework.Logging.Serial/SerialLoggerFactory.cs +++ b/nanoFramework.Logging.Serial/SerialLoggerFactory.cs @@ -4,7 +4,7 @@ // using Microsoft.Extensions.Logging; -using Windows.Devices.SerialCommunication; +using System.IO.Ports; namespace nanoFramework.Logging.Serial { @@ -13,13 +13,13 @@ namespace nanoFramework.Logging.Serial /// public class SerialLoggerFactory : ILoggerFactory { - private SerialDevice _serial; + private SerialPort _serial; private readonly string _comPort; - private readonly uint _baudRate; + private readonly int _baudRate; private readonly ushort _dataBits; - private readonly SerialParity _parity; - private readonly SerialStopBitCount _stopBits; - private readonly SerialHandshake _handshake; + private readonly Parity _parity; + private readonly StopBits _stopBits; + private readonly Handshake _handshake; /// /// Create a new instance of from a . @@ -32,11 +32,11 @@ public class SerialLoggerFactory : ILoggerFactory /// public SerialLoggerFactory( string comPort, - uint baudRate = 9600, + int baudRate = 9600, ushort dataBits = 8, - SerialParity parity = SerialParity.None, - SerialStopBitCount stopBits = SerialStopBitCount.One, - SerialHandshake handshake = SerialHandshake.None) + Parity parity = Parity.None, + StopBits stopBits = StopBits.One, + Handshake handshake = Handshake.None) { _comPort = comPort; _baudRate = baudRate; @@ -49,7 +49,7 @@ public SerialLoggerFactory( /// public ILogger CreateLogger(string categoryName) { - _serial = SerialDevice.FromId(_comPort); + _serial = new SerialPort(_comPort); _serial.BaudRate = _baudRate; _serial.Parity = _parity; _serial.StopBits = _stopBits; diff --git a/nanoFramework.Logging.Serial/nanoFramework.Logging.Serial.nfproj b/nanoFramework.Logging.Serial/nanoFramework.Logging.Serial.nfproj index 206350f..6cbdf32 100644 --- a/nanoFramework.Logging.Serial/nanoFramework.Logging.Serial.nfproj +++ b/nanoFramework.Logging.Serial/nanoFramework.Logging.Serial.nfproj @@ -32,17 +32,19 @@ ..\packages\nanoFramework.Runtime.Events.1.9.0-preview.26\lib\nanoFramework.Runtime.Events.dll True - - ..\packages\nanoFramework.System.Text.1.1.1-preview.51\lib\nanoFramework.System.Text.dll + + ..\packages\nanoFramework.System.Collections.1.2.0-preview.55\lib\nanoFramework.System.Collections.dll True + True - - ..\packages\nanoFramework.Windows.Devices.SerialCommunication.1.3.4-preview.94\lib\Windows.Devices.SerialCommunication.dll + + ..\packages\nanoFramework.System.Text.1.1.1-preview.51\lib\nanoFramework.System.Text.dll True - - ..\packages\nanoFramework.Windows.Storage.Streams.1.12.2-preview.5\lib\Windows.Storage.Streams.dll + + ..\packages\nanoFramework.System.IO.Ports.1.0.0-preview.17\lib\System.IO.Ports.dll True + True @@ -64,4 +66,4 @@ - + \ No newline at end of file diff --git a/nanoFramework.Logging.Serial/packages.config b/nanoFramework.Logging.Serial/packages.config index 55de404..afba816 100644 --- a/nanoFramework.Logging.Serial/packages.config +++ b/nanoFramework.Logging.Serial/packages.config @@ -2,8 +2,8 @@ + + - - \ No newline at end of file