Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion Tests/UnitTestDebugLogging/SerialTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 16 additions & 12 deletions Tests/UnitTestDebugLogging/UnitTestDebugLogging.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,33 @@
<HintPath>..\..\packages\nanoFramework.Runtime.Events.1.9.0-preview.26\lib\nanoFramework.Runtime.Events.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nanoFramework.System.Collections, Version=1.2.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.System.Collections.1.2.0-preview.55\lib\nanoFramework.System.Collections.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="nanoFramework.System.Text, Version=1.1.1.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.System.Text.1.1.1-preview.51\lib\nanoFramework.System.Text.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nanoFramework.TestFramework, Version=1.0.121.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.TestFramework.1.0.121\lib\nanoFramework.TestFramework.dll</HintPath>
<Reference Include="nanoFramework.TestFramework, Version=1.0.122.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.TestFramework.1.0.122\lib\nanoFramework.TestFramework.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="nanoFramework.UnitTestLauncher, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.TestFramework.1.0.121\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
<HintPath>..\..\packages\nanoFramework.TestFramework.1.0.122\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="System.IO.FileSystem, Version=1.0.1.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.System.IO.FileSystem.1.0.1-preview.16\lib\System.IO.FileSystem.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Windows.Devices.SerialCommunication, Version=1.3.4.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.Windows.Devices.SerialCommunication.1.3.4-preview.94\lib\Windows.Devices.SerialCommunication.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Windows.Storage.Streams, Version=1.12.2.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.Windows.Storage.Streams.1.12.2-preview.5\lib\Windows.Storage.Streams.dll</HintPath>
<Reference Include="System.IO.Ports, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.System.IO.Ports.1.0.0-preview.17\lib\System.IO.Ports.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
</ItemGroup>
<ItemGroup>
Expand All @@ -92,11 +96,11 @@
<ProjectConfigurationsDeclaredAsItems />
</ProjectCapabilities>
</ProjectExtensions>
<Import Project="..\..\packages\nanoFramework.TestFramework.1.0.121\build\nanoFramework.TestFramework.targets" Condition="Exists('..\..\packages\nanoFramework.TestFramework.1.0.121\build\nanoFramework.TestFramework.targets')" />
<Import Project="..\..\packages\nanoFramework.TestFramework.1.0.122\build\nanoFramework.TestFramework.targets" Condition="Exists('..\..\packages\nanoFramework.TestFramework.1.0.122\build\nanoFramework.TestFramework.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>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}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\nanoFramework.TestFramework.1.0.121\build\nanoFramework.TestFramework.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\nanoFramework.TestFramework.1.0.121\build\nanoFramework.TestFramework.targets'))" />
<Error Condition="!Exists('..\..\packages\nanoFramework.TestFramework.1.0.122\build\nanoFramework.TestFramework.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\nanoFramework.TestFramework.1.0.122\build\nanoFramework.TestFramework.targets'))" />
</Target>
</Project>
</Project>
6 changes: 3 additions & 3 deletions Tests/UnitTestDebugLogging/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<package id="nanoFramework.CoreLibrary" version="1.10.4-preview.11" targetFramework="netnanoframework10" />
<package id="nanoFramework.Hardware.Esp32" version="1.3.3-preview.14" targetFramework="netnanoframework10" />
<package id="nanoFramework.Runtime.Events" version="1.9.0-preview.26" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.Collections" version="1.2.0-preview.55" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.IO.FileSystem" version="1.0.1-preview.16" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.IO.Ports" version="1.0.0-preview.17" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.Text" version="1.1.1-preview.51" targetFramework="netnanoframework10" />
<package id="nanoFramework.TestFramework" version="1.0.121" targetFramework="netnanoframework10" developmentDependency="true" />
<package id="nanoFramework.Windows.Devices.SerialCommunication" version="1.3.4-preview.94" targetFramework="netnanoframework10" />
<package id="nanoFramework.Windows.Storage.Streams" version="1.12.2-preview.5" targetFramework="netnanoframework10" />
<package id="nanoFramework.TestFramework" version="1.0.122" targetFramework="netnanoframework10" developmentDependency="true" />
</packages>
21 changes: 12 additions & 9 deletions nanoFramework.Logging.Serial/SerialLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -16,18 +16,22 @@ namespace nanoFramework.Logging.Serial
/// </summary>
public class SerialLogger : ILogger
{
private readonly DataWriter _outputDataWriter;
private readonly SerialPort _serialPort;

/// <summary>
/// Creates a new instance of the <see cref="SerialLogger"/>
/// </summary>
/// <param name="serialDevice">The serial port to use</param>
/// <param name="loggerName">The logger name</param>
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;
}

Expand All @@ -39,7 +43,7 @@ public SerialLogger(ref SerialDevice serialDevice, string loggerName)
/// <summary>
/// Name of the serial device
/// </summary>
public SerialDevice SerialDevice { get; }
public SerialPort SerialPort => _serialPort;

/// <summary>
/// Sets the minimum log level
Expand All @@ -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);
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions nanoFramework.Logging.Serial/SerialLoggerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//

using Microsoft.Extensions.Logging;
using Windows.Devices.SerialCommunication;
using System.IO.Ports;

namespace nanoFramework.Logging.Serial
{
Expand All @@ -13,13 +13,13 @@ namespace nanoFramework.Logging.Serial
/// </summary>
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;

/// <summary>
/// Create a new instance of <see cref="SerialLoggerFactory"/> from a <see cref="SerialDevice"/>.
Expand All @@ -32,11 +32,11 @@ public class SerialLoggerFactory : ILoggerFactory
/// <param name="handshake"></param>
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;
Expand All @@ -49,7 +49,7 @@ public SerialLoggerFactory(
/// <inheritdoc/>
public ILogger CreateLogger(string categoryName)
{
_serial = SerialDevice.FromId(_comPort);
_serial = new SerialPort(_comPort);
_serial.BaudRate = _baudRate;
_serial.Parity = _parity;
_serial.StopBits = _stopBits;
Expand Down
16 changes: 9 additions & 7 deletions nanoFramework.Logging.Serial/nanoFramework.Logging.Serial.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@
<HintPath>..\packages\nanoFramework.Runtime.Events.1.9.0-preview.26\lib\nanoFramework.Runtime.Events.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nanoFramework.System.Text, Version=1.1.1.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.System.Text.1.1.1-preview.51\lib\nanoFramework.System.Text.dll</HintPath>
<Reference Include="nanoFramework.System.Collections, Version=1.2.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.System.Collections.1.2.0-preview.55\lib\nanoFramework.System.Collections.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="Windows.Devices.SerialCommunication, Version=1.3.4.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.Windows.Devices.SerialCommunication.1.3.4-preview.94\lib\Windows.Devices.SerialCommunication.dll</HintPath>
<Reference Include="nanoFramework.System.Text, Version=1.1.1.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.System.Text.1.1.1-preview.51\lib\nanoFramework.System.Text.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Windows.Storage.Streams, Version=1.12.2.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.Windows.Storage.Streams.1.12.2-preview.5\lib\Windows.Storage.Streams.dll</HintPath>
<Reference Include="System.IO.Ports, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.System.IO.Ports.1.0.0-preview.17\lib\System.IO.Ports.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
</ItemGroup>
<ItemGroup>
Expand All @@ -64,4 +66,4 @@
</PropertyGroup>
<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets'))" />
</Target>
</Project>
</Project>
4 changes: 2 additions & 2 deletions nanoFramework.Logging.Serial/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<packages>
<package id="nanoFramework.CoreLibrary" version="1.10.4-preview.11" targetFramework="netnanoframework10" />
<package id="nanoFramework.Runtime.Events" version="1.9.0-preview.26" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.Collections" version="1.2.0-preview.55" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.IO.Ports" version="1.0.0-preview.17" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.Text" version="1.1.1-preview.51" targetFramework="netnanoframework10" />
<package id="nanoFramework.Windows.Devices.SerialCommunication" version="1.3.4-preview.94" targetFramework="netnanoframework10" />
<package id="nanoFramework.Windows.Storage.Streams" version="1.12.2-preview.5" targetFramework="netnanoframework10" />
<package id="Nerdbank.GitVersioning" version="3.4.194" developmentDependency="true" targetFramework="netnanoframework10" />
</packages>