Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollo3zehn committed Aug 16, 2022
2 parents c268739 + a53397b commit c64541c
Show file tree
Hide file tree
Showing 39 changed files with 153 additions and 171 deletions.
3 changes: 0 additions & 3 deletions sample/SampleServerClientRtu/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace FluentModbus.SampleMaster
{
Expand Down
2 changes: 1 addition & 1 deletion sample/SampleServerClientRtu/SampleServerClientRtu.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>$(TargetFrameworkVersion)</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 0 additions & 3 deletions sample/SampleServerClientTcp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace FluentModbus.SampleMaster
{
Expand Down
2 changes: 1 addition & 1 deletion sample/SampleServerClientTcp/SampleServerClientTcp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>$(TargetFrameworkVersion)</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions src/FluentModbus/CastMemoryManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Buffers;
using System.Buffers;
using System.Runtime.InteropServices;

namespace FluentModbus
Expand Down
3 changes: 1 addition & 2 deletions src/FluentModbus/Client/ModbusClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;

namespace FluentModbus
{
Expand Down
7 changes: 2 additions & 5 deletions src/FluentModbus/Client/ModbusClientAsync.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@

/* This is automatically translated code. */
/* This is automatically translated code. */

#pragma warning disable CS1998

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

namespace FluentModbus
{
public abstract partial class ModbusClient
public abstract partial class ModbusClient
{
/// <summary>
/// Sends the requested modbus message and waits for the response.
Expand Down
51 changes: 33 additions & 18 deletions src/FluentModbus/Client/ModbusRtuClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.IO;
using System.IO.Ports;
using System.IO.Ports;

namespace FluentModbus
{
Expand All @@ -11,8 +9,8 @@ public partial class ModbusRtuClient : ModbusClient
{
#region Field

private IModbusRtuSerialPort _serialPort;
private ModbusFrameBuffer _frameBuffer;
private IModbusRtuSerialPort _serialPort = default!;
private ModbusFrameBuffer _frameBuffer = default!;

#endregion

Expand All @@ -37,7 +35,7 @@ public bool IsConnected
{
get
{
return _serialPort != null ? _serialPort.IsOpen : false;
return _serialPort is not null ? _serialPort.IsOpen : false;
}
}

Expand Down Expand Up @@ -93,7 +91,7 @@ public void Connect(string port)
/// <param name="endianness">Specifies the endianness of the data exchanged with the Modbus server.</param>
public void Connect(string port, ModbusEndianness endianness)
{
var serialPort = new ModbusRtuSerialPort(new SerialPort(port)
var serialPort = ModbusRtuSerialPort.CreateInternal(new SerialPort(port)
{
BaudRate = BaudRate,
Handshake = Handshake,
Expand All @@ -107,20 +105,20 @@ public void Connect(string port, ModbusEndianness endianness)
}

/// <summary>
/// Closes the opened COM port and frees all resources.
/// Connect to the specified <paramref name="serialPort"/>.
/// </summary>
public void Close()
{
_serialPort?.Close();
_frameBuffer?.Dispose();
}

internal void Connect(IModbusRtuSerialPort serialPort)
/// <param name="serialPort">The externally managed <see cref="ModbusRtuSerialPort"/>.</param>
public void Connect(IModbusRtuSerialPort serialPort)
{
Connect(serialPort, ModbusEndianness.LittleEndian);
}

internal void Connect(IModbusRtuSerialPort serialPort, ModbusEndianness endianness)
/// <summary>
/// Connect to the specified <paramref name="serialPort"/>.
/// </summary>
/// <param name="serialPort">The externally managed <see cref="ModbusRtuSerialPort"/>.</param>
/// <param name="endianness">Specifies the endianness of the data exchanged with the Modbus server.</param>
public void Connect(IModbusRtuSerialPort serialPort, ModbusEndianness endianness)
{
/* According to the spec (https://www.modbus.org/docs/Modbus_over_serial_line_V1_02.pdf),
* section 2.5.1 RTU Transmission Mode: "... the use of no parity requires 2 stop bits."
Expand All @@ -135,14 +133,31 @@ internal void Connect(IModbusRtuSerialPort serialPort, ModbusEndianness endianne

_frameBuffer = new ModbusFrameBuffer(256);

_serialPort?.Close();
if (_serialPort is ModbusRtuSerialPort maybeInternalPort && maybeInternalPort.IsInternal)
maybeInternalPort.Close();

_serialPort = serialPort;
_serialPort.Open();

if (!_serialPort.IsOpen)
_serialPort.Open();
}

/// <summary>
/// Closes the opened COM port and frees all resources.
/// </summary>
public void Close()
{
if (_serialPort is ModbusRtuSerialPort maybeInternalPort && maybeInternalPort.IsInternal)
maybeInternalPort.Close();

_frameBuffer?.Dispose();
}

///<inheritdoc/>
protected override Span<byte> TransceiveFrame(byte unitIdentifier, ModbusFunctionCode functionCode, Action<ExtendedBinaryWriter> extendFrame)
{
// WARNING: IF YOU EDIT THIS METHOD, REFLECT ALL CHANGES ALSO IN TransceiveFrameAsync!

int frameLength;
byte rawFunctionCode;
ushort crc;
Expand Down
10 changes: 2 additions & 8 deletions src/FluentModbus/Client/ModbusRtuClientAsync.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@

/* This is automatically translated code. */

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
/* This is automatically translated code. */

namespace FluentModbus
{
public partial class ModbusRtuClient
public partial class ModbusRtuClient
{
///<inheritdoc/>
protected override async Task<Memory<byte>> TransceiveFrameAsync(byte unitIdentifier, ModbusFunctionCode functionCode, Action<ExtendedBinaryWriter> extendFrame, CancellationToken cancellationToken = default)
Expand Down
19 changes: 12 additions & 7 deletions src/FluentModbus/Client/ModbusTcpClient.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.IO;
using System.Net;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;

namespace FluentModbus
{
Expand All @@ -17,9 +14,9 @@ public partial class ModbusTcpClient : ModbusClient
private ushort _transactionIdentifierBase;
private object _transactionIdentifierLock;

private TcpClient _tcpClient;
private NetworkStream _networkStream;
private ModbusFrameBuffer _frameBuffer;
private TcpClient? _tcpClient;
private NetworkStream _networkStream = default!;
private ModbusFrameBuffer _frameBuffer = default!;

#endregion

Expand Down Expand Up @@ -100,7 +97,13 @@ public void Connect(string remoteEndpoint, ModbusEndianness endianness)
if (!ModbusUtils.TryParseEndpoint(remoteEndpoint.AsSpan(), out var parsedRemoteEndpoint))
throw new FormatException("An invalid IPEndPoint was specified.");

#if NETSTANDARD2_0
Connect(parsedRemoteEndpoint!, endianness);
#endif

#if NETSTANDARD2_1_OR_GREATER
Connect(parsedRemoteEndpoint, endianness);
#endif
}

/// <summary>
Expand Down Expand Up @@ -170,6 +173,8 @@ public void Disconnect()
///<inheritdoc/>
protected override Span<byte> TransceiveFrame(byte unitIdentifier, ModbusFunctionCode functionCode, Action<ExtendedBinaryWriter> extendFrame)
{
// WARNING: IF YOU EDIT THIS METHOD, REFLECT ALL CHANGES ALSO IN TransceiveFrameAsync!

int frameLength;
int partialLength;

Expand Down
11 changes: 3 additions & 8 deletions src/FluentModbus/Client/ModbusTcpClientAsync.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@

/* This is automatically translated code. */

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
/* This is automatically translated code. */


namespace FluentModbus
{
public partial class ModbusTcpClient
public partial class ModbusTcpClient
{
///<inheritdoc/>
protected override async Task<Memory<byte>> TransceiveFrameAsync(byte unitIdentifier, ModbusFunctionCode functionCode, Action<ExtendedBinaryWriter> extendFrame, CancellationToken cancellationToken = default)
Expand Down
4 changes: 1 addition & 3 deletions src/FluentModbus/ExtendedBinaryReader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;

namespace FluentModbus
{
Expand Down
5 changes: 1 addition & 4 deletions src/FluentModbus/ExtendedBinaryWriter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.IO;

namespace FluentModbus
namespace FluentModbus
{
/// <summary>
/// A binary writer with extended capability to handle big-endian data.
Expand Down
5 changes: 1 addition & 4 deletions src/FluentModbus/IModbusRtuSerialPort.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Threading;
using System.Threading.Tasks;

namespace FluentModbus
namespace FluentModbus
{
/// <summary>
/// A serial port for Modbus RTU communication.
Expand Down
4 changes: 1 addition & 3 deletions src/FluentModbus/ModbusException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace FluentModbus
namespace FluentModbus
{
/// <summary>
/// This exception is used for Modbus protocol errors.
Expand Down
4 changes: 1 addition & 3 deletions src/FluentModbus/ModbusFrameBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Buffers;
using System.IO;
using System.Buffers;

namespace FluentModbus
{
Expand Down
Loading

0 comments on commit c64541c

Please sign in to comment.