Skip to content

Commit

Permalink
Merge pull request #342 from pkuehnel/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
pkuehnel authored Oct 3, 2022
2 parents 136b733 + f488f8d commit 5f69c46
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Plugins.Modbus/Contracts/IModbusClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Plugins.Modbus.Contracts;

public interface IModbusClient
public interface IModbusClient : IDisposable
{
Task<int> ReadInt32Value(byte unitIdentifier, ushort startingAddress, ushort quantity,
string ipAddressString,
Expand All @@ -9,4 +9,4 @@ Task<int> ReadInt32Value(byte unitIdentifier, ushort startingAddress, ushort qua
bool DiconnectIfConnected();
Task<short> ReadInt16Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port, int connectDelay, int timeout, int? minimumResult);
Task<float> ReadFloatValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port, int connectDelay, int timeout, int? minimumResult);
}
}
11 changes: 10 additions & 1 deletion Plugins.Modbus/Services/ModbusClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,14 @@ private async Task<byte[]> 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;
Expand All @@ -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();
}
}
58 changes: 47 additions & 11 deletions Plugins.Modbus/Services/ModbusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,20 @@ public async Task<int> 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<short> ReadInt16Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port,
Expand All @@ -38,9 +49,19 @@ public async Task<short> 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<float> ReadFloatValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port,
Expand All @@ -51,10 +72,19 @@ public async Task<float> 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)
Expand All @@ -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");
Expand All @@ -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...");
Expand Down

0 comments on commit 5f69c46

Please sign in to comment.