diff --git a/AMWD.Protocols.Modbus.Proxy/ModbusTcpProxy.cs b/AMWD.Protocols.Modbus.Proxy/ModbusTcpProxy.cs index d258960..92d271e 100644 --- a/AMWD.Protocols.Modbus.Proxy/ModbusTcpProxy.cs +++ b/AMWD.Protocols.Modbus.Proxy/ModbusTcpProxy.cs @@ -46,7 +46,7 @@ namespace AMWD.Protocols.Modbus.Proxy ListenAddress = listenAddress ?? IPAddress.Loopback; - if (ushort.MinValue < listenPort || listenPort < ushort.MaxValue) + if (listenPort < ushort.MinValue || ushort.MaxValue < listenPort) throw new ArgumentOutOfRangeException(nameof(listenPort)); try diff --git a/AMWD.Protocols.Modbus.Serial/ModbusRtuServer.cs b/AMWD.Protocols.Modbus.Serial/ModbusRtuServer.cs index a04859f..4441aa9 100644 --- a/AMWD.Protocols.Modbus.Serial/ModbusRtuServer.cs +++ b/AMWD.Protocols.Modbus.Serial/ModbusRtuServer.cs @@ -186,7 +186,7 @@ namespace AMWD.Protocols.Modbus.Serial private Task StopAsyncInternal(CancellationToken cancellationToken) { - _stopCts.Cancel(); + _stopCts?.Cancel(); _serialPort.Close(); _serialPort.DataReceived -= OnDataReceived; @@ -328,7 +328,7 @@ namespace AMWD.Protocols.Modbus.Serial ushort firstAddress = requestBytes.GetBigEndianUInt16(2); ushort count = requestBytes.GetBigEndianUInt16(4); - if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_DISCRETE_READ_COUNT) + if (count < RtuProtocol.MIN_READ_COUNT || RtuProtocol.MAX_DISCRETE_READ_COUNT < count) { responseBytes[1] |= 0x80; responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue); @@ -385,7 +385,7 @@ namespace AMWD.Protocols.Modbus.Serial ushort firstAddress = requestBytes.GetBigEndianUInt16(2); ushort count = requestBytes.GetBigEndianUInt16(4); - if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_DISCRETE_READ_COUNT) + if (count < RtuProtocol.MIN_READ_COUNT || RtuProtocol.MAX_DISCRETE_READ_COUNT < count) { responseBytes[1] |= 0x80; responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue); @@ -442,7 +442,7 @@ namespace AMWD.Protocols.Modbus.Serial ushort firstAddress = requestBytes.GetBigEndianUInt16(2); ushort count = requestBytes.GetBigEndianUInt16(4); - if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_REGISTER_READ_COUNT) + if (count < RtuProtocol.MIN_READ_COUNT || RtuProtocol.MAX_REGISTER_READ_COUNT < count) { responseBytes[1] |= 0x80; responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue); @@ -496,7 +496,7 @@ namespace AMWD.Protocols.Modbus.Serial ushort firstAddress = requestBytes.GetBigEndianUInt16(2); ushort count = requestBytes.GetBigEndianUInt16(4); - if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_REGISTER_READ_COUNT) + if (count < RtuProtocol.MIN_READ_COUNT || RtuProtocol.MAX_REGISTER_READ_COUNT < count) { responseBytes[1] |= 0x80; responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue); diff --git a/AMWD.Protocols.Modbus.Tcp/ModbusTcpConnection.cs b/AMWD.Protocols.Modbus.Tcp/ModbusTcpConnection.cs index 41a0ada..ae8b7f0 100644 --- a/AMWD.Protocols.Modbus.Tcp/ModbusTcpConnection.cs +++ b/AMWD.Protocols.Modbus.Tcp/ModbusTcpConnection.cs @@ -34,8 +34,8 @@ namespace AMWD.Protocols.Modbus.Tcp private readonly Task _processingTask; private readonly AsyncQueue _requestQueue = new(); - private TimeSpan _readTimeout = TimeSpan.FromMilliseconds(1); - private TimeSpan _writeTimeout = TimeSpan.FromMilliseconds(1); + private TimeSpan _readTimeout = TimeSpan.FromSeconds(1); + private TimeSpan _writeTimeout = TimeSpan.FromSeconds(1); #endregion Fields diff --git a/AMWD.Protocols.Modbus.Tcp/ModbusTcpServer.cs b/AMWD.Protocols.Modbus.Tcp/ModbusTcpServer.cs index 2e285e0..06b194f 100644 --- a/AMWD.Protocols.Modbus.Tcp/ModbusTcpServer.cs +++ b/AMWD.Protocols.Modbus.Tcp/ModbusTcpServer.cs @@ -36,6 +36,8 @@ namespace AMWD.Protocols.Modbus.Tcp private readonly ReaderWriterLockSlim _deviceListLock = new(); private readonly Dictionary _devices = []; + private TimeSpan _readWriteTimeout = TimeSpan.FromSeconds(1); + #endregion Fields #region Constructors @@ -49,7 +51,7 @@ namespace AMWD.Protocols.Modbus.Tcp { ListenAddress = listenAddress ?? IPAddress.Loopback; - if (ushort.MinValue < listenPort || listenPort < ushort.MaxValue) + if (listenPort < ushort.MinValue || ushort.MaxValue < listenPort) throw new ArgumentOutOfRangeException(nameof(listenPort)); try @@ -105,7 +107,17 @@ namespace AMWD.Protocols.Modbus.Tcp /// /// Gets or sets the read/write timeout. /// - public TimeSpan ReadWriteTimeout { get; set; } + public TimeSpan ReadWriteTimeout + { + get => _readWriteTimeout; + set + { + if (value < TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(value)); + + _readWriteTimeout = value; + } + } #endregion Properties @@ -151,11 +163,11 @@ namespace AMWD.Protocols.Modbus.Tcp private async Task StopAsyncInternal(CancellationToken cancellationToken = default) { - _stopCts.Cancel(); + _stopCts?.Cancel(); - _listener.Stop(); + _listener?.Stop(); #if NET8_0_OR_GREATER - _listener.Dispose(); + _listener?.Dispose(); #endif try { @@ -354,7 +366,7 @@ namespace AMWD.Protocols.Modbus.Tcp ushort firstAddress = requestBytes.GetBigEndianUInt16(8); ushort count = requestBytes.GetBigEndianUInt16(10); - if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_DISCRETE_READ_COUNT) + if (count < TcpProtocol.MIN_READ_COUNT || TcpProtocol.MAX_DISCRETE_READ_COUNT < count) { responseBytes[7] |= 0x80; responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue); @@ -406,7 +418,7 @@ namespace AMWD.Protocols.Modbus.Tcp ushort firstAddress = requestBytes.GetBigEndianUInt16(8); ushort count = requestBytes.GetBigEndianUInt16(10); - if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_DISCRETE_READ_COUNT) + if (count < TcpProtocol.MIN_READ_COUNT || TcpProtocol.MAX_DISCRETE_READ_COUNT < count) { responseBytes[7] |= 0x80; responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue); @@ -458,7 +470,7 @@ namespace AMWD.Protocols.Modbus.Tcp ushort firstAddress = requestBytes.GetBigEndianUInt16(8); ushort count = requestBytes.GetBigEndianUInt16(10); - if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_REGISTER_READ_COUNT) + if (count < TcpProtocol.MIN_READ_COUNT || TcpProtocol.MAX_REGISTER_READ_COUNT < count) { responseBytes[7] |= 0x80; responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue); @@ -507,7 +519,7 @@ namespace AMWD.Protocols.Modbus.Tcp ushort firstAddress = requestBytes.GetBigEndianUInt16(8); ushort count = requestBytes.GetBigEndianUInt16(10); - if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_REGISTER_READ_COUNT) + if (count < TcpProtocol.MIN_READ_COUNT || TcpProtocol.MAX_REGISTER_READ_COUNT < count) { responseBytes[7] |= 0x80; responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue);