Updated serial implementations

This commit is contained in:
2025-01-23 22:05:44 +01:00
parent fb67e0b77e
commit 6fc7cfda9a
7 changed files with 200 additions and 22 deletions

View File

@@ -16,7 +16,7 @@ namespace AMWD.Protocols.Modbus.Serial
/// </summary>
/// <param name="portName">The name of the serial port to use.</param>
public ModbusSerialClient(string portName)
: this(new ModbusSerialConnection { PortName = portName })
: this(new ModbusSerialConnection(portName))
{ }
/// <summary>
@@ -41,8 +41,8 @@ namespace AMWD.Protocols.Modbus.Serial
Protocol = new RtuProtocol();
}
/// <inheritdoc cref="SerialPort.GetPortNames" />
public static string[] AvailablePortNames => SerialPort.GetPortNames();
/// <inheritdoc cref="ModbusSerialConnection.AvailablePortNames" />
public static string[] AvailablePortNames => ModbusSerialConnection.AvailablePortNames;
/// <inheritdoc cref="IModbusConnection.IdleTimeout"/>
public TimeSpan IdleTimeout

View File

@@ -39,10 +39,15 @@ namespace AMWD.Protocols.Modbus.Serial
/// <summary>
/// Initializes a new instance of the <see cref="ModbusSerialConnection"/> class.
/// </summary>
public ModbusSerialConnection()
public ModbusSerialConnection(string portName)
{
if (string.IsNullOrWhiteSpace(portName))
throw new ArgumentNullException(nameof(portName));
_serialPort = new SerialPortWrapper
{
PortName = portName,
BaudRate = (int)BaudRate.Baud19200,
DataBits = 8,
Handshake = Handshake.None,
@@ -59,6 +64,9 @@ namespace AMWD.Protocols.Modbus.Serial
#region Properties
/// <inheritdoc cref="SerialPort.GetPortNames" />
public static string[] AvailablePortNames => SerialPort.GetPortNames();
/// <inheritdoc/>
public string Name => "Serial";
@@ -68,20 +76,6 @@ namespace AMWD.Protocols.Modbus.Serial
/// <inheritdoc/>
public virtual TimeSpan ConnectTimeout { get; set; } = TimeSpan.MaxValue;
/// <inheritdoc/>
public virtual TimeSpan ReadTimeout
{
get => TimeSpan.FromMilliseconds(_serialPort.ReadTimeout);
set => _serialPort.ReadTimeout = (int)value.TotalMilliseconds;
}
/// <inheritdoc/>
public virtual TimeSpan WriteTimeout
{
get => TimeSpan.FromMilliseconds(_serialPort.WriteTimeout);
set => _serialPort.WriteTimeout = (int)value.TotalMilliseconds;
}
/// <summary>
/// Gets or sets a value indicating whether the RS485 driver has to be enabled via software switch.
/// </summary>
@@ -171,6 +165,20 @@ namespace AMWD.Protocols.Modbus.Serial
set => _serialPort.StopBits = value;
}
/// <inheritdoc/>
public virtual TimeSpan ReadTimeout
{
get => TimeSpan.FromMilliseconds(_serialPort.ReadTimeout);
set => _serialPort.ReadTimeout = (int)value.TotalMilliseconds;
}
/// <inheritdoc/>
public virtual TimeSpan WriteTimeout
{
get => TimeSpan.FromMilliseconds(_serialPort.WriteTimeout);
set => _serialPort.WriteTimeout = (int)value.TotalMilliseconds;
}
#endregion SerialPort Properties
#endregion Properties

View File

@@ -20,6 +20,39 @@ namespace AMWD.Protocols.Modbus.Serial.Utils
#endregion Fields
#region Constructor
public SerialPortWrapper()
{
_serialPort.DataReceived += OnDataReceived;
_serialPort.PinChanged += OnPinChanged;
_serialPort.ErrorReceived += OnErrorReceived;
}
#endregion Constructor
#region Events
/// <inheritdoc cref="SerialPort.DataReceived"/>
public virtual event SerialDataReceivedEventHandler DataReceived;
/// <inheritdoc cref="SerialPort.PinChanged"/>
public virtual event SerialPinChangedEventHandler PinChanged;
/// <inheritdoc cref="SerialPort.ErrorReceived"/>
public virtual event SerialErrorReceivedEventHandler ErrorReceived;
private void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
=> DataReceived?.Invoke(sender, e);
private void OnPinChanged(object sender, SerialPinChangedEventArgs e)
=> PinChanged?.Invoke(sender, e);
private void OnErrorReceived(object sender, SerialErrorReceivedEventArgs e)
=> ErrorReceived?.Invoke(sender, e);
#endregion Events
#region Properties
/// <inheritdoc cref="SerialPort.Handshake"/>
@@ -82,6 +115,10 @@ namespace AMWD.Protocols.Modbus.Serial.Utils
set => _serialPort.Parity = value;
}
/// <inheritdoc cref="SerialPort.BytesToWrite"/>
public virtual int BytesToWrite
=> _serialPort.BytesToWrite;
/// <inheritdoc cref="SerialPort.BaudRate"/>
public virtual int BaudRate
{
@@ -89,6 +126,10 @@ namespace AMWD.Protocols.Modbus.Serial.Utils
set => _serialPort.BaudRate = value;
}
/// <inheritdoc cref="SerialPort.BytesToRead"/>
public virtual int BytesToRead
=> _serialPort.BytesToRead;
#endregion Properties
#region Methods
@@ -101,6 +142,14 @@ namespace AMWD.Protocols.Modbus.Serial.Utils
public virtual void Open()
=> _serialPort.Open();
/// <inheritdoc cref="SerialPort.Read(byte[], int, int)"/>
public virtual int Read(byte[] buffer, int offset, int count)
=> _serialPort.Read(buffer, offset, count);
/// <inheritdoc cref="SerialPort.Write(byte[], int, int)"/>
public virtual void Write(byte[] buffer, int offset, int count)
=> _serialPort.Write(buffer, offset, count);
/// <inheritdoc cref="SerialPort.Dispose"/>
public virtual void Dispose()
=> _serialPort.Dispose();
@@ -117,7 +166,7 @@ namespace AMWD.Protocols.Modbus.Serial.Utils
/// <remarks>
/// There seems to be a bug with the async stream implementation on Windows.
/// <br/>
/// See this StackOverflow answer: <see href="https://stackoverflow.com/a/54610437/11906695" />
/// See this StackOverflow answer: <see href="https://stackoverflow.com/a/54610437/11906695" />.
/// </remarks>
/// <param name="buffer">The buffer to write the data into.</param>
/// <param name="offset">The byte offset in buffer at which to begin writing data from the serial port.</param>

View File

@@ -13,13 +13,15 @@ namespace AMWD.Protocols.Modbus.Tests.Serial
[TestInitialize]
public void Initialize()
{
string portName = "COM-42";
_genericConnectionMock = new Mock<IModbusConnection>();
_genericConnectionMock.Setup(c => c.IdleTimeout).Returns(TimeSpan.FromSeconds(40));
_genericConnectionMock.Setup(c => c.ConnectTimeout).Returns(TimeSpan.FromSeconds(30));
_genericConnectionMock.Setup(c => c.ReadTimeout).Returns(TimeSpan.FromSeconds(20));
_genericConnectionMock.Setup(c => c.WriteTimeout).Returns(TimeSpan.FromSeconds(10));
_serialConnectionMock = new Mock<ModbusSerialConnection>();
_serialConnectionMock = new Mock<ModbusSerialConnection>(portName);
_serialConnectionMock.Setup(c => c.IdleTimeout).Returns(TimeSpan.FromSeconds(10));
_serialConnectionMock.Setup(c => c.ConnectTimeout).Returns(TimeSpan.FromSeconds(20));
@@ -28,7 +30,7 @@ namespace AMWD.Protocols.Modbus.Tests.Serial
_serialConnectionMock.Setup(c => c.DriverEnabledRS485).Returns(true);
_serialConnectionMock.Setup(c => c.InterRequestDelay).Returns(TimeSpan.FromSeconds(50));
_serialConnectionMock.Setup(c => c.PortName).Returns("COM-42");
_serialConnectionMock.Setup(c => c.PortName).Returns(portName);
_serialConnectionMock.Setup(c => c.BaudRate).Returns(BaudRate.Baud2400);
_serialConnectionMock.Setup(c => c.DataBits).Returns(7);
_serialConnectionMock.Setup(c => c.Handshake).Returns(Handshake.XOnXOff);
@@ -231,5 +233,18 @@ namespace AMWD.Protocols.Modbus.Tests.Serial
_serialConnectionMock.VerifyNoOtherCalls();
}
[TestMethod]
public void ShouldPrintCleanString()
{
// Arrange
using var client = new ModbusSerialClient(_serialConnectionMock.Object);
// Act
string str = client.ToString();
// Assert
SnapshotAssert.AreEqual(str);
}
}
}

View File

@@ -90,6 +90,21 @@ namespace AMWD.Protocols.Modbus.Tests.Serial
connection.Dispose();
}
[DataTestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowArgumentNullExceptionOnCreate(string portName)
{
// Arrange
// Act
using var test = new ModbusSerialClient(portName);
// Assert - ArgumentNullException
}
[TestMethod]
[ExpectedException(typeof(ObjectDisposedException))]
public async Task ShouldThrowDisposedExceptionOnInvokeAsync()
@@ -467,7 +482,7 @@ namespace AMWD.Protocols.Modbus.Tests.Serial
return Task.FromResult(0);
});
var connection = new ModbusSerialConnection();
var connection = new ModbusSerialConnection("some-port");
// Replace real connection with mock
var connectionField = connection.GetType().GetField("_serialPort", BindingFlags.NonPublic | BindingFlags.Instance);

View File

@@ -0,0 +1,8 @@
Serial Client COM-42
BaudRate: 2400
DataBits: 7
StopBits: 1.5
Parity: space
Handshake: xonxoff
RtsEnable: true
DriverEnabledRS485: true

View File

@@ -0,0 +1,83 @@
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
namespace AMWD.Protocols.Modbus.Tests
{
// ================================================================================================================================ //
// Source: https://git.am-wd.de/am-wd/common/-/blob/fb26e441a48214aaae72003c4a5ac33d5c7b929a/src/AMWD.Common.Test/SnapshotAssert.cs //
// ================================================================================================================================ //
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal sealed class SnapshotAssert
{
/// <summary>
/// Tests whether the specified string is equal to the saved snapshot.
/// </summary>
/// <param name="actual">The current aggregated content string.</param>
/// <param name="message">An optional message to display if the assertion fails.</param>
/// <param name="callerFilePath">The absolute file path of the calling file (filled automatically on compile time).</param>
/// <param name="callerMemberName">The name of the calling method (filled automatically on compile time).</param>
public static void AreEqual(string actual, string message = null, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null)
{
string cleanLineEnding = actual
.Replace("\r\n", "\n") // Windows
.Replace("\r", "\n"); // MacOS
AreEqual(Encoding.UTF8.GetBytes(cleanLineEnding), message, callerFilePath, callerMemberName);
}
/// <summary>
/// Tests whether the specified byte array is equal to the saved snapshot.
/// </summary>
/// <param name="actual">The current aggregated content bytes.</param>
/// <param name="message">An optional message to display if the assertion fails.</param>
/// <param name="callerFilePath">The absolute file path of the calling file (filled automatically on compile time).</param>
/// <param name="callerMemberName">The name of the calling method (filled automatically on compile time).</param>
public static void AreEqual(byte[] actual, string message = null, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null)
=> AreEqual(actual, null, message, callerFilePath, callerMemberName);
/// <summary>
/// Tests whether the specified byte array is equal to the saved snapshot.
/// </summary>
/// <remarks>
/// The past has shown, that e.g. wkhtmltopdf prints the current timestamp at the beginning of the PDF file.
/// Therefore you can specify which sequences of bytes should be excluded from the comparison.
/// </remarks>
/// <param name="actual">The current aggregated content bytes.</param>
/// <param name="excludedSequences">The excluded sequences.</param>
/// <param name="message">An optional message to display if the assertion fails.</param>
/// <param name="callerFilePath">The absolute file path of the calling file (filled automatically on compile time).</param>
/// <param name="callerMemberName">The name of the calling method (filled automatically on compile time).</param>
public static void AreEqual(byte[] actual, List<(int Start, int Length)> excludedSequences = null, string message = null, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null)
{
string callerDirectory = Path.GetDirectoryName(callerFilePath);
string callerFileName = Path.GetFileNameWithoutExtension(callerFilePath);
string snapshotDirectory = Path.Combine(callerDirectory, "Snapshots", callerFileName);
string snapshotFilePath = Path.Combine(snapshotDirectory, $"{callerMemberName}.snap.bin");
if (File.Exists(snapshotFilePath))
{
byte[] expected = File.ReadAllBytes(snapshotFilePath);
if (actual.Length != expected.Length)
Assert.Fail(message);
for (int i = 0; i < actual.Length; i++)
{
if (excludedSequences?.Any(s => s.Start <= i && i < s.Start + s.Length) == true)
continue;
if (actual[i] != expected[i])
Assert.Fail(message);
}
}
else
{
if (!Directory.Exists(snapshotDirectory))
Directory.CreateDirectory(snapshotDirectory);
File.WriteAllBytes(snapshotFilePath, actual);
}
}
}
}