Skip to content

Commit

Permalink
Update to .net 7
Browse files Browse the repository at this point in the history
Update NuGet packages
Refactoring
  • Loading branch information
HakamFostok committed Jan 22, 2023
1 parent f6eb07b commit dd5a608
Show file tree
Hide file tree
Showing 32 changed files with 99 additions and 93 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<!--<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>-->
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.38.0.46746">
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.51.0.59060">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
1 change: 0 additions & 1 deletion Modbus/Data/DataStoreEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ private DataStoreEventArgs(ushort startAddress, ModbusDataType modbusDataType)
/// <summary>
/// Data that was read or written.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
public DiscriminatedUnion<ReadOnlyCollection<bool>, ReadOnlyCollection<ushort>> Data { get; private set; }

internal static DataStoreEventArgs CreateDataStoreEventArgs<T>(ushort startAddress, ModbusDataType modbusDataType, IEnumerable<T> data)
Expand Down
2 changes: 0 additions & 2 deletions Modbus/Data/IModbusMessageDataCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ namespace Modbus.Data;
/// <summary>
/// Modbus message containing data.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
public interface IModbusMessageDataCollection
{
/// <summary>
/// Gets the network bytes.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
byte[] NetworkBytes { get; }

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Modbus/Device/ModbusDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public abstract class ModbusDevice : IDisposable
{
private ModbusTransport _transport;

internal ModbusDevice(ModbusTransport transport)
private protected ModbusDevice(ModbusTransport transport)
{
_transport = transport;
}
Expand Down
5 changes: 0 additions & 5 deletions Modbus/Device/ModbusIpMaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Modbus.Device;
/// <summary>
/// Modbus IP master device.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Breaking change.")]
public class ModbusIpMaster : ModbusMaster
{
/// <summary>
Expand All @@ -28,7 +27,6 @@ private ModbusIpMaster(ModbusTransport transport)
/// Modbus IP master factory method.
/// </summary>
/// <returns>New instance of Modbus IP master device using provided TCP client.</returns>
[SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Breaking change.")]
public static ModbusIpMaster CreateIp(TcpClient tcpClient)
{
ArgumentNullException.ThrowIfNull(tcpClient);
Expand All @@ -40,7 +38,6 @@ public static ModbusIpMaster CreateIp(TcpClient tcpClient)
/// Modbus IP master factory method.
/// </summary>
/// <returns>New instance of Modbus IP master device using provided UDP client.</returns>
[SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Breaking change.")]
public static ModbusIpMaster CreateIp(UdpClient udpClient)
{
ArgumentNullException.ThrowIfNull(udpClient);
Expand All @@ -58,7 +55,6 @@ public static ModbusIpMaster CreateIp(UdpClient udpClient)
/// Modbus IP master factory method.
/// </summary>
/// <returns>New instance of Modbus IP master device using provided serial port.</returns>
[SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Breaking change.")]
public static ModbusIpMaster CreateIp(SerialPort serialPort)
{
ArgumentNullException.ThrowIfNull(serialPort);
Expand All @@ -71,7 +67,6 @@ public static ModbusIpMaster CreateIp(SerialPort serialPort)
/// Modbus IP master factory method.
/// </summary>
/// <returns>New instance of Modbus IP master device using provided stream resource.</returns>
[SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Breaking change.")]
public static ModbusIpMaster CreateIp(IStreamResource streamResource)
{
ArgumentNullException.ThrowIfNull(streamResource);
Expand Down
6 changes: 2 additions & 4 deletions Modbus/Device/ModbusMaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Modbus.Device;
/// </summary>
public abstract class ModbusMaster : ModbusDevice, IModbusMaster
{
internal ModbusMaster(ModbusTransport transport)
private protected ModbusMaster(ModbusTransport transport)
: base(transport)
{
}
Expand Down Expand Up @@ -361,9 +361,7 @@ public Task<ushort[]> ReadWriteMultipleRegistersAsync(
/// </summary>
/// <typeparam name="TResponse">The type of the response.</typeparam>
/// <param name="request">The request.</param>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
[SuppressMessage("Microsoft.Usage", "CA2223:MembersShouldDifferByMoreThanReturnType")]
public TResponse ExecuteCustomMessage<TResponse>(IModbusMessage request)
public TResponse? ExecuteCustomMessage<TResponse>(IModbusMessage request)
where TResponse : IModbusMessage, new() =>
Transport.UnicastMessage<TResponse>(request);

Expand Down
2 changes: 1 addition & 1 deletion Modbus/Device/ModbusMasterTcpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Modbus.Device;
/// <summary>
/// Represents an incoming connection from a Modbus master. Contains the slave's logic to process the connection.
/// </summary>
internal class ModbusMasterTcpConnection : ModbusDevice, IDisposable
internal class ModbusMasterTcpConnection : ModbusDevice
{
private readonly ModbusTcpSlave _slave;
private readonly Task _requestHandlerTask;
Expand Down
1 change: 0 additions & 1 deletion Modbus/Device/ModbusSerialMaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ private ModbusSerialMaster(ModbusTransport transport)
/// <summary>
/// Gets the Modbus Transport.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
ModbusSerialTransport IModbusSerialMaster.Transport =>
(ModbusSerialTransport)Transport;

Expand Down
3 changes: 1 addition & 2 deletions Modbus/Device/ModbusSlave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Modbus.Device;
/// </summary>
public abstract class ModbusSlave : ModbusDevice
{
internal ModbusSlave(byte unitId, ModbusTransport transport)
private protected ModbusSlave(byte unitId, ModbusTransport transport)
: base(transport)
{
DataStore = DataStoreFactory.CreateDefaultDataStore();
Expand Down Expand Up @@ -156,7 +156,6 @@ internal static WriteMultipleRegistersResponse WriteMultipleRegisters(
return response;
}

[SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily", Justification = "Cast is not unneccessary.")]
internal IModbusMessage ApplyRequest(IModbusMessage request)
{
IModbusMessage response;
Expand Down
4 changes: 2 additions & 2 deletions Modbus/Device/ModbusTcpSlave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ModbusTcpSlave : ModbusSlave
private readonly ConcurrentDictionary<string, ModbusMasterTcpConnection> _masters =
new();

private TcpListener _server;
private TcpListener? _server;
#if TIMER
private Timer _timer;
#endif
Expand Down Expand Up @@ -165,7 +165,7 @@ private void OnTimer(object sender, ElapsedEventArgs e)
}
}
#endif
private void OnMasterConnectionClosedHandler(object sender, TcpConnectionEventArgs e)
private void OnMasterConnectionClosedHandler(object? sender, TcpConnectionEventArgs e)
{
if (!_masters.TryRemove(e.EndPoint, out ModbusMasterTcpConnection _))
{
Expand Down
2 changes: 1 addition & 1 deletion Modbus/IO/ModbusSerialTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Modbus.IO;
/// </summary>
public abstract class ModbusSerialTransport : ModbusTransport
{
internal ModbusSerialTransport(IStreamResource streamResource)
private protected ModbusSerialTransport(IStreamResource streamResource)
: base(streamResource)
{
Debug.Assert(streamResource is not null, "Argument streamResource cannot be null.");
Expand Down
4 changes: 2 additions & 2 deletions Modbus/IO/ModbusTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public abstract class ModbusTransport : IDisposable
/// <summary>
/// This constructor is called by the NullTransport.
/// </summary>
internal ModbusTransport()
private protected ModbusTransport()
{
}

internal ModbusTransport(IStreamResource streamResource)
private protected ModbusTransport(IStreamResource streamResource)
{
Debug.Assert(streamResource is not null, "Argument streamResource cannot be null.");

Expand Down
4 changes: 2 additions & 2 deletions Modbus/Message/AbstractModbusMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ public abstract class AbstractModbusMessage
/// <summary>
/// Abstract Modbus message.
/// </summary>
internal AbstractModbusMessage()
private protected AbstractModbusMessage()
{
MessageImpl = new ModbusMessageImpl();
}

/// <summary>
/// Abstract Modbus message.
/// </summary>
internal AbstractModbusMessage(byte slaveAddress, byte functionCode)
private protected AbstractModbusMessage(byte slaveAddress, byte functionCode)
{
MessageImpl = new ModbusMessageImpl(slaveAddress, functionCode);
}
Expand Down
6 changes: 3 additions & 3 deletions Modbus/Message/AbstractModbusMessageWithData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ namespace Modbus.Message;
public abstract class AbstractModbusMessageWithData<TData> : AbstractModbusMessage
where TData : IModbusMessageDataCollection
{
internal AbstractModbusMessageWithData()
private protected AbstractModbusMessageWithData()
{
}

internal AbstractModbusMessageWithData(byte slaveAddress, byte functionCode)
private protected AbstractModbusMessageWithData(byte slaveAddress, byte functionCode)
: base(slaveAddress, functionCode)
{
}

public TData Data
public TData? Data
{
get => (TData)MessageImpl.Data;
set => MessageImpl.Data = value;
Expand Down
1 change: 0 additions & 1 deletion Modbus/Message/DiagnosticsRequestResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public DiagnosticsRequestResponse(ushort subFunctionCode, byte slaveAddress, Reg

public override int MinimumFrameSize => 6;

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "May implement addtional sub function codes in the future.")]
public ushort SubFunctionCode
{
get => MessageImpl.SubFunctionCode.Value;
Expand Down
2 changes: 0 additions & 2 deletions Modbus/Message/IModbusMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ public interface IModbusMessage
/// <summary>
/// Composition of the slave address and protocol data unit.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
byte[] MessageFrame { get; }

/// <summary>
/// Composition of the function code and message data.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
byte[] ProtocolDataUnit { get; }

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Modbus/Message/ReadCoilsInputsResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public ReadCoilsInputsResponse()
{
}

public ReadCoilsInputsResponse(byte functionCode, byte slaveAddress, byte byteCount, DiscreteCollection data)
public ReadCoilsInputsResponse(byte functionCode, byte slaveAddress, byte byteCount, DiscreteCollection? data)
: base(slaveAddress, functionCode)
{
ByteCount = byteCount;
Expand Down
8 changes: 4 additions & 4 deletions Modbus/Modbus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<AssemblyVersion>2.0.1.0</AssemblyVersion>
<FileVersion>2.0.1.0</FileVersion>
<Version>2.0.1</Version>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<FileVersion>3.0.0.0</FileVersion>
<Version>3.0.0</Version>
</PropertyGroup>

<ItemGroup>
Expand All @@ -50,7 +50,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.IO.Ports" Version="6.0.0" />
<PackageReference Include="System.IO.Ports" Version="7.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>

Expand Down
12 changes: 8 additions & 4 deletions Modbus/SlaveException.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using Modbus.Message;
using Modbus.Message;

namespace Modbus;

/// <summary>
/// Represents slave errors that occur during communication.
/// Represents slave errors that occur during communication.
/// </summary>
/// <seealso cref="Exception" />
[Serializable]
public class SlaveException : Exception
{
private const string SlaveAddressPropertyName = "SlaveAdress";
Expand Down Expand Up @@ -45,7 +46,6 @@ internal SlaveException(SlaveExceptionResponse slaveExceptionResponse)
_slaveExceptionResponse = slaveExceptionResponse;
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Used by test code.")]
internal SlaveException(string message, SlaveExceptionResponse slaveExceptionResponse)
: base(message)
{
Expand Down Expand Up @@ -88,4 +88,8 @@ public override string Message
/// <value>The slave address.</value>
public byte SlaveAddress =>
_slaveExceptionResponse is not null ? _slaveExceptionResponse.SlaveAddress : (byte)0;

protected SlaveException(
System.Runtime.Serialization.SerializationInfo serializationInfo,
System.Runtime.Serialization.StreamingContext streamingContext) : base(serializationInfo, streamingContext) { }
}
2 changes: 1 addition & 1 deletion Modbus/Unme.Common/DisposableUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

internal static class DisposableUtility
{
public static void Dispose<T>(ref T item)
public static void Dispose<T>(ref T? item)
where T : class, IDisposable
{
if (item is null)
Expand Down
8 changes: 0 additions & 8 deletions Modbus/Utility/DiscriminatedUnion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ public enum DiscriminatedUnionOption
/// <summary>
/// Option A.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "A")]
A,

/// <summary>
/// Option B.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "B")]
B
}

Expand All @@ -33,7 +31,6 @@ public class DiscriminatedUnion<TA, TB>
/// <summary>
/// Gets the value of option A.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "A")]
public TA A
{
get
Expand All @@ -51,7 +48,6 @@ public TA A
/// <summary>
/// Gets the value of option B.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "B")]
public TB B
{
get
Expand All @@ -74,16 +70,12 @@ public TB B
/// <summary>
/// Factory method for creating DiscriminatedUnion with option A set.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes", Justification = "Factory method.")]
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "0#a")]
public static DiscriminatedUnion<TA, TB> CreateA(TA a) =>
new() { Option = DiscriminatedUnionOption.A, optionA = a };

/// <summary>
/// Factory method for creating DiscriminatedUnion with option B set.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes", Justification = "Factory method.")]
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "0#b")]
public static DiscriminatedUnion<TA, TB> CreateB(TB b) =>
new() { Option = DiscriminatedUnionOption.B, optionB = b };

Expand Down
6 changes: 3 additions & 3 deletions Modbus/Utility/ModbusUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static byte[] GetAsciiBytes(params ushort[] numbers) =>
/// </summary>
/// <param name="networkBytes">The network order byte array.</param>
/// <returns>The host order ushort array.</returns>
public static ushort[] NetworkBytesToHostUInt16(byte[] networkBytes)
public static ushort[] NetworkBytesToHostUInt16(byte[]? networkBytes)
{
ArgumentNullException.ThrowIfNull(networkBytes);

Expand Down Expand Up @@ -159,7 +159,7 @@ public static byte[] HexToBytes(string hex)
/// </summary>
/// <param name="data">The data used in LRC.</param>
/// <returns>LRC value.</returns>
public static byte CalculateLrc(byte[] data)
public static byte CalculateLrc(byte[]? data)
{
ArgumentNullException.ThrowIfNull(data);

Expand All @@ -180,7 +180,7 @@ public static byte CalculateLrc(byte[] data)
/// </summary>
/// <param name="data">The data used in CRC.</param>
/// <returns>CRC value.</returns>
public static byte[] CalculateCrc(byte[] data)
public static byte[] CalculateCrc(byte[]? data)
{
ArgumentNullException.ThrowIfNull(data);

Expand Down
Loading

0 comments on commit dd5a608

Please sign in to comment.