diff --git a/Plugins.Modbus/Contracts/IModbusClient.cs b/Plugins.Modbus/Contracts/IModbusClient.cs index c55c5b482..f7f08100a 100644 --- a/Plugins.Modbus/Contracts/IModbusClient.cs +++ b/Plugins.Modbus/Contracts/IModbusClient.cs @@ -1,6 +1,6 @@ namespace Plugins.Modbus.Contracts; -public interface IModbusClient +public interface IModbusClient : IDisposable { Task ReadInt32Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, @@ -9,4 +9,4 @@ Task ReadInt32Value(byte unitIdentifier, ushort startingAddress, ushort qua bool DiconnectIfConnected(); Task ReadInt16Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port, int connectDelay, int timeout, int? minimumResult); Task ReadFloatValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port, int connectDelay, int timeout, int? minimumResult); -} \ No newline at end of file +} diff --git a/Plugins.Modbus/Services/ModbusClient.cs b/Plugins.Modbus/Services/ModbusClient.cs index 65d564cd8..e984d7135 100644 --- a/Plugins.Modbus/Services/ModbusClient.cs +++ b/Plugins.Modbus/Services/ModbusClient.cs @@ -111,11 +111,14 @@ private async Task GetRegisterValue(byte unitIdentifier, ushort starting var tmpArrayPowerComplete = ReadHoldingRegisters(unitIdentifier, startingAddress, quantity).ToArray(); return tmpArrayPowerComplete; } - catch (Exception) + catch (Exception ex) { + _logger.LogError(ex, "Could not get register value."); if (IsConnected) { + _logger.LogDebug("Disconnecting Modcus Client..."); Disconnect(); + _logger.LogDebug("Modbus Client disconnected."); } throw; @@ -141,4 +144,10 @@ public static byte[] StringToByteArray(string hex) .Select(x => Convert.ToByte(hex.Substring(x, 2), 16)) .ToArray(); } + + public void Dispose() + { + DiconnectIfConnected(); + _semaphoreSlim.Dispose(); + } } diff --git a/Plugins.Modbus/Services/ModbusService.cs b/Plugins.Modbus/Services/ModbusService.cs index dac715403..b9bb5fa55 100644 --- a/Plugins.Modbus/Services/ModbusService.cs +++ b/Plugins.Modbus/Services/ModbusService.cs @@ -24,9 +24,20 @@ public async Task ReadInt32Value(byte unitIdentifier, ushort startingAddres var modbusClient = GetModbusClient(ipAddressString, port); - var value = await modbusClient.ReadInt32Value(unitIdentifier, startingAddress, quantity, ipAddressString, port, - connectDelay, timeout, minimumResult).ConfigureAwait(false); - return value; + try + { + var value = await modbusClient.ReadInt32Value(unitIdentifier, startingAddress, quantity, ipAddressString, port, + connectDelay, timeout, minimumResult).ConfigureAwait(false); + return value; + } + catch (Exception ex) + { + _logger.LogError(ex, "Could not get value. Dispose modbus client"); + modbusClient.Dispose(); + _modbusClients.Remove(GetKeyString(ipAddressString, port)); + throw; + } + } public async Task ReadInt16Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port, @@ -38,9 +49,19 @@ public async Task ReadInt16Value(byte unitIdentifier, ushort startingAddr connectDelay, timeout, minimumResult); var modbusClient = GetModbusClient(ipAddressString, port); - var value = await modbusClient.ReadInt16Value(unitIdentifier, startingAddress, quantity, ipAddressString, port, - connectDelay, timeout, minimumResult).ConfigureAwait(false); - return value; + try + { + var value = await modbusClient.ReadInt16Value(unitIdentifier, startingAddress, quantity, ipAddressString, port, + connectDelay, timeout, minimumResult).ConfigureAwait(false); + return value; + } + catch (Exception ex) + { + _logger.LogError(ex, "Could not get value. Dispose modbus client"); + modbusClient.Dispose(); + _modbusClients.Remove(GetKeyString(ipAddressString, port)); + throw; + } } public async Task ReadFloatValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port, @@ -51,10 +72,19 @@ public async Task ReadFloatValue(byte unitIdentifier, ushort startingAddr nameof(ReadInt16Value), unitIdentifier, startingAddress, quantity, ipAddressString, port, connectDelay, timeout, minimumResult); var modbusClient = GetModbusClient(ipAddressString, port); - - var value = await modbusClient.ReadFloatValue(unitIdentifier, startingAddress, quantity, ipAddressString, port, - connectDelay, timeout, minimumResult).ConfigureAwait(false); - return value; + try + { + var value = await modbusClient.ReadFloatValue(unitIdentifier, startingAddress, quantity, ipAddressString, port, + connectDelay, timeout, minimumResult).ConfigureAwait(false); + return value; + } + catch (Exception ex) + { + _logger.LogError(ex, "Could not get value. Dispose modbus client"); + modbusClient.Dispose(); + _modbusClients.Remove(GetKeyString(ipAddressString, port)); + throw; + } } private IModbusClient GetModbusClient(string ipAddressString, int port) @@ -66,7 +96,7 @@ private IModbusClient GetModbusClient(string ipAddressString, int port) AppDomain.CurrentDomain.ProcessExit += OnProcessExit; } - var keyString = $"{ipAddressString}:{port}"; + var keyString = GetKeyString(ipAddressString, port); if (_modbusClients.Any(c => c.Key == keyString)) { _logger.LogDebug("Use exising modbusClient"); @@ -82,6 +112,12 @@ private IModbusClient GetModbusClient(string ipAddressString, int port) return modbusClient; } + private string GetKeyString(string ipAddressString, int port) + { + var keyString = $"{ipAddressString}:{port}"; + return keyString; + } + private void OnProcessExit(object? sender, EventArgs e) { _logger.LogTrace("Closing all open connections...");