Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Toxantron committed Mar 6, 2022
2 parents 2079753 + bccca0c commit 37bcd6c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 44 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ env:

jobs:
Build:
runs-on: windows-latest
runs-on: windows-2019
steps:
- uses: actions/checkout@v2

Expand All @@ -44,7 +44,7 @@ jobs:

UnitTests:
needs: [Build]
runs-on: windows-latest
runs-on: windows-2019
steps:
- uses: actions/checkout@v2

Expand All @@ -66,7 +66,7 @@ jobs:

IntegrationTests:
needs: [Build]
runs-on: windows-latest
runs-on: windows-2019
steps:
- uses: actions/checkout@v2

Expand All @@ -88,7 +88,7 @@ jobs:

Codecov:
needs: [UnitTests, IntegrationTests]
runs-on: windows-latest
runs-on: windows-2019
steps:
- name: Download test results
uses: actions/download-artifact@v2
Expand All @@ -103,7 +103,7 @@ jobs:

Documentation:
needs: [Build]
runs-on: windows-latest
runs-on: windows-2019
steps:
- uses: actions/checkout@v2

Expand All @@ -121,7 +121,7 @@ jobs:
Publish:
needs: [Build, UnitTests, IntegrationTests]
if: ${{ github.event_name == 'push' }}
runs-on: windows-latest
runs-on: windows-2019
steps:
- uses: actions/checkout@v2

Expand Down Expand Up @@ -161,4 +161,4 @@ jobs:
MORYX_NUGET_APIKEY: ${{secrets.NUGET_TOKEN}}
MORYX_PACKAGE_TARGET: "https://api.nuget.org/v3/index.json"
MORYX_PACKAGE_TARGET_V3: "https://api.nuget.org/v3/index.json"
run: ./Build.ps1 -Publish
run: ./Build.ps1 -Publish
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.4.0
3.4.1
4 changes: 2 additions & 2 deletions src/Moryx.Communication.Serial/SerialBinaryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace Moryx.Communication.Serial
/// </summary>
public class SerialBinaryConfig : BinaryConnectionConfig
{
///
public override string PluginName => SerialBinaryConnection.ConnectionName;
/// <inheritdoc />
public override string PluginName => nameof(SerialBinaryConnection);

/// <summary>
/// Port to use for communication
Expand Down
51 changes: 29 additions & 22 deletions src/Moryx.Communication.Serial/SerialBinaryConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@ namespace Moryx.Communication.Serial
/// standard serial ports.
/// </summary>
[ExpectedConfig(typeof(SerialBinaryConfig))]
[Plugin(LifeCycle.Transient, typeof(IBinaryConnection), Name = ConnectionName)]
[Plugin(LifeCycle.Transient, typeof(IBinaryConnection), Name = nameof(SerialBinaryConnection))]
public class SerialBinaryConnection : IBinaryConnection, ILoggingComponent
{
/// <summary>
/// Unique plugin name for this type
/// </summary>
internal const string ConnectionName = "SerialBinaryConnection";

/// <summary>
/// Named injected logger instance
/// </summary>
Expand All @@ -49,6 +44,19 @@ public class SerialBinaryConnection : IBinaryConnection, ILoggingComponent
/// </summary>
private IReadContext _readContext;

private BinaryConnectionState _currentState;

/// <inheritdoc />
public BinaryConnectionState CurrentState
{
get => _currentState;
private set
{
_currentState = value;
NotifyConnectionState?.Invoke(this, _currentState);
}
}

/// <summary>
/// Create connection instance
/// </summary>
Expand All @@ -57,19 +65,20 @@ public SerialBinaryConnection(IMessageValidator validator)
_validator = validator;
}

///
/// <inheritdoc />
public void Initialize(BinaryConnectionConfig config)
{
_config = (SerialBinaryConfig)config;
_serialPort = SerialPortFactory.FromConfig(_config, Logger);
_serialPort.DataReceived += OnDataReceived;
}

///
/// <inheritdoc />
public void Start()
{
_readContext = _validator.Interpreter.CreateContext();
_serialPort.Open();

CurrentState = BinaryConnectionState.Connected;
}

Expand All @@ -86,13 +95,13 @@ public void Stop()
CurrentState = BinaryConnectionState.Disconnected;
}

///
/// <inheritdoc />
public void Dispose()
{
Stop();
}

///
/// <inheritdoc />
public void Reconnect()
{
}
Expand All @@ -103,14 +112,6 @@ public void Reconnect(int delayMs)
throw new NotImplementedException();
}

/// <inheritdoc />
public BinaryConnectionState CurrentState { get; private set; }

#pragma warning disable 67
/// <inheritdoc />
public event EventHandler<BinaryConnectionState> NotifyConnectionState;
#pragma warning restore 67

/// <inheritdoc />
public void Send(BinaryMessage message)
{
Expand All @@ -119,10 +120,13 @@ public void Send(BinaryMessage message)
_serialPort.Write(bytes, 0, bytes.Length);
}

///
public Task SendAsync(BinaryMessage message)
/// <inheritdoc />
public async Task SendAsync(BinaryMessage message)
{
throw new NotSupportedException();
// Create bytes from message
var bytes = _validator.Interpreter.SerializeMessage(message);
await _serialPort.BaseStream.WriteAsync(bytes, 0, bytes.Length);
await _serialPort.BaseStream.FlushAsync();
}

private void OnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
Expand All @@ -140,8 +144,11 @@ private void PublishCompleteMessage(BinaryMessage binaryMessage)
Received?.Invoke(this, binaryMessage);
}

///
/// <inheritdoc />
public event EventHandler<BinaryMessage> Received;

/// <inheritdoc />
public event EventHandler<BinaryConnectionState> NotifyConnectionState;
}

}
19 changes: 8 additions & 11 deletions src/Moryx.Communication.Serial/SerialPortFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
// Copyright (c) 2022, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using System;
using System.IO.Ports;
using System.Text;
using Moryx.Logging;
Expand All @@ -11,11 +14,11 @@ internal static SerialPort FromConfig(SerialBinaryConfig config, IModuleLogger l
{
logger.Log(LogLevel.Debug, "ConfigComPort started");

// Create a new _serialPort object with default settings.
var serialPort = new SerialPort(config.Port);

try
{
// Create a new _serialPort object with default settings.
var serialPort = new SerialPort(config.Port);

// Allow the user to set the appropriate properties.
serialPort.BaudRate = config.BaudRate;
serialPort.Parity = config.Parity;
Expand All @@ -30,12 +33,6 @@ internal static SerialPort FromConfig(SerialBinaryConfig config, IModuleLogger l
serialPort.ReadBufferSize = config.ReadBufferSize;
serialPort.WriteBufferSize = config.WriteBufferSize;

logger.Log(LogLevel.Debug, "ConfigPort: Opening");

serialPort.Open();

logger.Log(LogLevel.Debug, "ConfigPort: Opened");

return serialPort;
}
catch (Exception e)
Expand All @@ -44,7 +41,7 @@ internal static SerialPort FromConfig(SerialBinaryConfig config, IModuleLogger l

var msg = new StringBuilder("Known devices:");

foreach (string s in SerialPort.GetPortNames())
foreach (var s in SerialPort.GetPortNames())
{
msg.AppendLine().AppendFormat(" Device {0}", s);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Moryx.Model.PostgreSQL/Moryx.Model.PostgreSQL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net45'">
<PackageReference Include="Npgsql" Version="4.0.11" />
<PackageReference Include="Npgsql" Version="4.0.10" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net461'">
Expand Down

0 comments on commit 37bcd6c

Please sign in to comment.