From e59aca49157b3e33805d1bdb34be939074e5b644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20K=C3=BChnel?= Date: Mon, 17 Oct 2022 01:18:20 +0200 Subject: [PATCH 1/6] feat(ModbusPlugin): use generic method for getting modbus value --- Plugins.Modbus/Contracts/IModbusClient.cs | 3 + Plugins.Modbus/Contracts/IModbusService.cs | 5 +- .../Controllers/ModbusController.cs | 29 +++++++++- Plugins.Modbus/Plugins.Modbus.csproj | 4 ++ Plugins.Modbus/Services/ModbusClient.cs | 2 +- Plugins.Modbus/Services/ModbusService.cs | 57 +++++++++++++++++++ .../Shared/Enums/ModbusValueType.cs | 11 ++++ 7 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 TeslaSolarCharger/Shared/Enums/ModbusValueType.cs diff --git a/Plugins.Modbus/Contracts/IModbusClient.cs b/Plugins.Modbus/Contracts/IModbusClient.cs index f7f08100a..46a695661 100644 --- a/Plugins.Modbus/Contracts/IModbusClient.cs +++ b/Plugins.Modbus/Contracts/IModbusClient.cs @@ -9,4 +9,7 @@ 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); + + Task GetByteArray(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, + int port, int connectDelay, int timeout); } diff --git a/Plugins.Modbus/Contracts/IModbusService.cs b/Plugins.Modbus/Contracts/IModbusService.cs index 5550865da..d4c8ce21d 100644 --- a/Plugins.Modbus/Contracts/IModbusService.cs +++ b/Plugins.Modbus/Contracts/IModbusService.cs @@ -7,4 +7,7 @@ Task ReadInt32Value(byte unitIdentifier, ushort startingAddress, ushort qua Task ReadInt16Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port, int connectDelaySeconds, int timeoutSeconds, int? minimumResult); Task ReadFloatValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port, int connectDelaySeconds, int timeoutSeconds, int? minimumResult); -} \ No newline at end of file + + Task ReadValue(byte unitIdentifier, ushort startingAddress, ushort quantity, + string ipAddressString, int port, int connectDelay, int timeout, int? minimumResult) where T : struct; +} diff --git a/Plugins.Modbus/Controllers/ModbusController.cs b/Plugins.Modbus/Controllers/ModbusController.cs index ddd9f5f7e..05f169c49 100644 --- a/Plugins.Modbus/Controllers/ModbusController.cs +++ b/Plugins.Modbus/Controllers/ModbusController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using Plugins.Modbus.Contracts; +using TeslaSolarCharger.Shared.Enums; namespace Plugins.Modbus.Controllers { @@ -14,6 +15,27 @@ public ModbusController(IModbusService modbusService) _modbusService = modbusService; } + public Task GetTypedValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, + int port, int connectDelaySeconds, int timeoutSeconds, ModbusValueType modbusValueType, int? minimumResult = null) + { + return modbusValueType switch + { + ModbusValueType.Int => _modbusService.ReadValue(unitIdentifier, startingAddress, quantity, ipAddress, port, + connectDelaySeconds, timeoutSeconds, minimumResult), + ModbusValueType.Float => _modbusService.ReadValue(unitIdentifier, startingAddress, quantity, ipAddress, port, + connectDelaySeconds, timeoutSeconds, minimumResult), + ModbusValueType.Short => _modbusService.ReadValue(unitIdentifier, startingAddress, quantity, ipAddress, port, + connectDelaySeconds, timeoutSeconds, minimumResult), + ModbusValueType.UInt => _modbusService.ReadValue(unitIdentifier, startingAddress, quantity, ipAddress, port, + connectDelaySeconds, timeoutSeconds, minimumResult), + ModbusValueType.UShort => _modbusService.ReadValue(unitIdentifier, startingAddress, quantity, ipAddress, port, + connectDelaySeconds, timeoutSeconds, minimumResult), + ModbusValueType.Ulong => _modbusService.ReadValue(unitIdentifier, startingAddress, quantity, ipAddress, port, + connectDelaySeconds, timeoutSeconds, minimumResult), + _ => throw new ArgumentOutOfRangeException(nameof(modbusValueType), modbusValueType, null) + }; + } + /// /// Gets a Modbus Integer value /// @@ -28,9 +50,9 @@ public ModbusController(IModbusService modbusService) /// [Obsolete] [HttpGet] - public Task GetValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, + public Task GetValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port, int connectDelaySeconds, int timeoutSeconds, int? minimumResult = null) - => GetInt32Value(unitIdentifier, startingAddress, quantity, ipAddress, port, connectDelaySeconds, timeoutSeconds, minimumResult); + => GetTypedValue(unitIdentifier, startingAddress, quantity, ipAddress, port, connectDelaySeconds, timeoutSeconds, ModbusValueType.Int, minimumResult); /// /// Gets a Modbus Int32 value @@ -44,6 +66,7 @@ public Task GetValue(byte unitIdentifier, ushort startingAddress, ushort qu /// /// Sets a minimum return result. This ist important, if your inverter does not send 0 as power if it is off. /// Modbus value converted to Int32 + [Obsolete] [HttpGet] public Task GetInt32Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port, int connectDelaySeconds, int timeoutSeconds, int? minimumResult = null) @@ -61,6 +84,7 @@ public Task GetInt32Value(byte unitIdentifier, ushort startingAddress, usho /// /// Sets a minimum return result. This ist important, if your inverter does not send 0 as power if it is off. /// Modbus value converted to Int16 + [Obsolete] [HttpGet] public Task GetInt16Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port, int connectDelaySeconds, int timeoutSeconds, int? minimumResult = null) @@ -78,6 +102,7 @@ public Task GetInt16Value(byte unitIdentifier, ushort startingAddress, us /// /// Sets a minimum return result. This ist important, if your inverter does not send 0 as power if it is off. /// Modbus value converted to float + [Obsolete] [HttpGet] public Task GetFloatValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port, int connectDelaySeconds, int timeoutSeconds, int? minimumResult = null) diff --git a/Plugins.Modbus/Plugins.Modbus.csproj b/Plugins.Modbus/Plugins.Modbus.csproj index d4643d0a1..ce1915514 100644 --- a/Plugins.Modbus/Plugins.Modbus.csproj +++ b/Plugins.Modbus/Plugins.Modbus.csproj @@ -17,6 +17,10 @@ + + + + PreserveNewest diff --git a/Plugins.Modbus/Services/ModbusClient.cs b/Plugins.Modbus/Services/ModbusClient.cs index e984d7135..657ba5b97 100644 --- a/Plugins.Modbus/Services/ModbusClient.cs +++ b/Plugins.Modbus/Services/ModbusClient.cs @@ -33,7 +33,7 @@ public async Task ReadInt32Value(byte unitIdentifier, ushort startingAddres return intValue < minimumResult ? (int)minimumResult : intValue; } - private async Task GetByteArray(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, + public async Task GetByteArray(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port, int connectDelay, int timeout) { var tmpArrayPowerComplete = diff --git a/Plugins.Modbus/Services/ModbusService.cs b/Plugins.Modbus/Services/ModbusService.cs index b9bb5fa55..e1a07dc2d 100644 --- a/Plugins.Modbus/Services/ModbusService.cs +++ b/Plugins.Modbus/Services/ModbusService.cs @@ -14,6 +14,63 @@ public ModbusService(ILogger logger, IServiceProvider serviceProv _serviceProvider = serviceProvider; } + public async Task ReadValue(byte unitIdentifier, ushort startingAddress, ushort quantity, + string ipAddressString, int port, int connectDelay, int timeout, int? minimumResult) where T : struct + { + _logger.LogTrace("{method}({unitIdentifier}, {startingAddress}, {quantity}, {ipAddressString}, {port}, " + + "{connectDelay}, {timeout}, {minimumResult})", + nameof(ReadInt32Value), unitIdentifier, startingAddress, quantity, ipAddressString, port, + connectDelay, timeout, minimumResult); + + var modbusClient = GetModbusClient(ipAddressString, port); + byte[] byteArray; + try + { + byteArray = await modbusClient.GetByteArray(unitIdentifier, startingAddress, quantity, ipAddressString, port, connectDelay, timeout) + .ConfigureAwait(false); + } + catch (Exception ex) + { + _logger.LogError(ex, "Could not get byte array. Dispose modbus client"); + modbusClient.Dispose(); + _modbusClients.Remove(GetKeyString(ipAddressString, port)); + throw; + } + + if (typeof(T) == typeof(int)) + { + return (T) Convert.ChangeType(BitConverter.ToInt32(byteArray, 0), typeof(T)); + } + + if (typeof(T) == typeof(float)) + { + return (T)Convert.ChangeType(BitConverter.ToSingle(byteArray, 0), typeof(T)); + } + + if (typeof(T) == typeof(short)) + { + return (T)Convert.ChangeType(BitConverter.ToInt16(byteArray, 0), typeof(T)); + } + + if (typeof(T) == typeof(uint)) + { + return (T)Convert.ChangeType(BitConverter.ToUInt32(byteArray, 0), typeof(T)); + } + + if (typeof(T) == typeof(ushort)) + { + return (T)Convert.ChangeType(BitConverter.ToUInt16(byteArray, 0), typeof(T)); + } + + if (typeof(T) == typeof(ulong)) + { + return (T)Convert.ChangeType(BitConverter.ToUInt64(byteArray, 0), typeof(T)); + } + + throw new NotImplementedException($"Can not convert value of type: {typeof(T)}"); + + } + public async Task ReadInt32Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port, int connectDelay, int timeout, int? minimumResult) { diff --git a/TeslaSolarCharger/Shared/Enums/ModbusValueType.cs b/TeslaSolarCharger/Shared/Enums/ModbusValueType.cs new file mode 100644 index 000000000..3557f854f --- /dev/null +++ b/TeslaSolarCharger/Shared/Enums/ModbusValueType.cs @@ -0,0 +1,11 @@ +namespace TeslaSolarCharger.Shared.Enums; + +public enum ModbusValueType +{ + Int, + Float, + Short, + UInt, + UShort, + Ulong, +} From e4d5f99d0c2c41529b0989ee07317ef1c6520f61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20K=C3=BChnel?= Date: Tue, 18 Oct 2022 00:04:14 +0200 Subject: [PATCH 2/6] feat(ModbusService): remove minimum result --- Plugins.Modbus/Contracts/IModbusService.cs | 2 +- Plugins.Modbus/Controllers/ModbusController.cs | 16 ++++++++-------- Plugins.Modbus/Services/ModbusService.cs | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Plugins.Modbus/Contracts/IModbusService.cs b/Plugins.Modbus/Contracts/IModbusService.cs index d4c8ce21d..153115d20 100644 --- a/Plugins.Modbus/Contracts/IModbusService.cs +++ b/Plugins.Modbus/Contracts/IModbusService.cs @@ -9,5 +9,5 @@ Task ReadInt32Value(byte unitIdentifier, ushort startingAddress, ushort qua Task ReadFloatValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port, int connectDelaySeconds, int timeoutSeconds, int? minimumResult); Task ReadValue(byte unitIdentifier, ushort startingAddress, ushort quantity, - string ipAddressString, int port, int connectDelay, int timeout, int? minimumResult) where T : struct; + string ipAddressString, int port, int connectDelay, int timeout) where T : struct; } diff --git a/Plugins.Modbus/Controllers/ModbusController.cs b/Plugins.Modbus/Controllers/ModbusController.cs index 05f169c49..f737e6eed 100644 --- a/Plugins.Modbus/Controllers/ModbusController.cs +++ b/Plugins.Modbus/Controllers/ModbusController.cs @@ -16,22 +16,22 @@ public ModbusController(IModbusService modbusService) } public Task GetTypedValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, - int port, int connectDelaySeconds, int timeoutSeconds, ModbusValueType modbusValueType, int? minimumResult = null) + int port, int connectDelaySeconds, int timeoutSeconds, ModbusValueType modbusValueType) { return modbusValueType switch { ModbusValueType.Int => _modbusService.ReadValue(unitIdentifier, startingAddress, quantity, ipAddress, port, - connectDelaySeconds, timeoutSeconds, minimumResult), + connectDelaySeconds, timeoutSeconds), ModbusValueType.Float => _modbusService.ReadValue(unitIdentifier, startingAddress, quantity, ipAddress, port, - connectDelaySeconds, timeoutSeconds, minimumResult), + connectDelaySeconds, timeoutSeconds), ModbusValueType.Short => _modbusService.ReadValue(unitIdentifier, startingAddress, quantity, ipAddress, port, - connectDelaySeconds, timeoutSeconds, minimumResult), + connectDelaySeconds, timeoutSeconds), ModbusValueType.UInt => _modbusService.ReadValue(unitIdentifier, startingAddress, quantity, ipAddress, port, - connectDelaySeconds, timeoutSeconds, minimumResult), + connectDelaySeconds, timeoutSeconds), ModbusValueType.UShort => _modbusService.ReadValue(unitIdentifier, startingAddress, quantity, ipAddress, port, - connectDelaySeconds, timeoutSeconds, minimumResult), + connectDelaySeconds, timeoutSeconds), ModbusValueType.Ulong => _modbusService.ReadValue(unitIdentifier, startingAddress, quantity, ipAddress, port, - connectDelaySeconds, timeoutSeconds, minimumResult), + connectDelaySeconds, timeoutSeconds), _ => throw new ArgumentOutOfRangeException(nameof(modbusValueType), modbusValueType, null) }; } @@ -52,7 +52,7 @@ public Task GetTypedValue(byte unitIdentifier, ushort startingAddress, u [HttpGet] public Task GetValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port, int connectDelaySeconds, int timeoutSeconds, int? minimumResult = null) - => GetTypedValue(unitIdentifier, startingAddress, quantity, ipAddress, port, connectDelaySeconds, timeoutSeconds, ModbusValueType.Int, minimumResult); + => GetTypedValue(unitIdentifier, startingAddress, quantity, ipAddress, port, connectDelaySeconds, timeoutSeconds, ModbusValueType.Int); /// /// Gets a Modbus Int32 value diff --git a/Plugins.Modbus/Services/ModbusService.cs b/Plugins.Modbus/Services/ModbusService.cs index e1a07dc2d..7b2ddc68e 100644 --- a/Plugins.Modbus/Services/ModbusService.cs +++ b/Plugins.Modbus/Services/ModbusService.cs @@ -15,12 +15,12 @@ public ModbusService(ILogger logger, IServiceProvider serviceProv } public async Task ReadValue(byte unitIdentifier, ushort startingAddress, ushort quantity, - string ipAddressString, int port, int connectDelay, int timeout, int? minimumResult) where T : struct + string ipAddressString, int port, int connectDelay, int timeout) where T : struct { _logger.LogTrace("{method}({unitIdentifier}, {startingAddress}, {quantity}, {ipAddressString}, {port}, " + - "{connectDelay}, {timeout}, {minimumResult})", + "{connectDelay}, {timeout})", nameof(ReadInt32Value), unitIdentifier, startingAddress, quantity, ipAddressString, port, - connectDelay, timeout, minimumResult); + connectDelay, timeout); var modbusClient = GetModbusClient(ipAddressString, port); byte[] byteArray; From f10a469983a7c1080453db2fd8d4dd995c50e14d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20K=C3=BChnel?= Date: Tue, 18 Oct 2022 00:07:59 +0200 Subject: [PATCH 3/6] refactor(ModbusController): use only one method --- Plugins.Modbus/Controllers/ModbusController.cs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Plugins.Modbus/Controllers/ModbusController.cs b/Plugins.Modbus/Controllers/ModbusController.cs index f737e6eed..2c44b48cc 100644 --- a/Plugins.Modbus/Controllers/ModbusController.cs +++ b/Plugins.Modbus/Controllers/ModbusController.cs @@ -46,12 +46,11 @@ public Task GetTypedValue(byte unitIdentifier, ushort startingAddress, u /// The modbus port of the modbus device /// /// - /// Sets a minimum return result. This ist important, if your inverter does not send 0 as power if it is off. /// [Obsolete] [HttpGet] public Task GetValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, - int port, int connectDelaySeconds, int timeoutSeconds, int? minimumResult = null) + int port, int connectDelaySeconds, int timeoutSeconds) => GetTypedValue(unitIdentifier, startingAddress, quantity, ipAddress, port, connectDelaySeconds, timeoutSeconds, ModbusValueType.Int); /// @@ -64,13 +63,12 @@ public Task GetValue(byte unitIdentifier, ushort startingAddress, ushort /// The modbus port of the modbus device /// /// - /// Sets a minimum return result. This ist important, if your inverter does not send 0 as power if it is off. /// Modbus value converted to Int32 [Obsolete] [HttpGet] - public Task GetInt32Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, - int port, int connectDelaySeconds, int timeoutSeconds, int? minimumResult = null) - => _modbusService.ReadInt32Value(unitIdentifier, startingAddress, quantity, ipAddress, port, connectDelaySeconds, timeoutSeconds, minimumResult); + public Task GetInt32Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, + int port, int connectDelaySeconds, int timeoutSeconds) + => GetTypedValue(unitIdentifier, startingAddress, quantity, ipAddress, port, connectDelaySeconds, timeoutSeconds, ModbusValueType.Int); /// /// Gets a Modbus Int16 value @@ -86,9 +84,9 @@ public Task GetInt32Value(byte unitIdentifier, ushort startingAddress, usho /// Modbus value converted to Int16 [Obsolete] [HttpGet] - public Task GetInt16Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, + public Task GetInt16Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port, int connectDelaySeconds, int timeoutSeconds, int? minimumResult = null) - => _modbusService.ReadInt16Value(unitIdentifier, startingAddress, quantity, ipAddress, port, connectDelaySeconds, timeoutSeconds, minimumResult); + => GetTypedValue(unitIdentifier, startingAddress, quantity, ipAddress, port, connectDelaySeconds, timeoutSeconds, ModbusValueType.Short); /// /// Gets a Modbus Float value @@ -104,9 +102,9 @@ public Task GetInt16Value(byte unitIdentifier, ushort startingAddress, us /// Modbus value converted to float [Obsolete] [HttpGet] - public Task GetFloatValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, + public Task GetFloatValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port, int connectDelaySeconds, int timeoutSeconds, int? minimumResult = null) - => _modbusService.ReadFloatValue(unitIdentifier, startingAddress, quantity, ipAddress, port, connectDelaySeconds, timeoutSeconds, minimumResult); + => GetTypedValue(unitIdentifier, startingAddress, quantity, ipAddress, port, connectDelaySeconds, timeoutSeconds, ModbusValueType.Float); } } From bd9bd0f5adf6ac771ecefc86090556e20fc70cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20K=C3=BChnel?= Date: Tue, 18 Oct 2022 00:14:26 +0200 Subject: [PATCH 4/6] feat(ModbusService): use minimum value of 1 second timeout --- Plugins.Modbus/Services/ModbusService.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Plugins.Modbus/Services/ModbusService.cs b/Plugins.Modbus/Services/ModbusService.cs index 7b2ddc68e..65b949340 100644 --- a/Plugins.Modbus/Services/ModbusService.cs +++ b/Plugins.Modbus/Services/ModbusService.cs @@ -24,6 +24,11 @@ public async Task ReadValue(byte unitIdentifier, ushort startingAddre var modbusClient = GetModbusClient(ipAddressString, port); byte[] byteArray; + if (timeout < 1) + { + _logger.LogDebug("Timeout is reduced to minimum value of 1 second"); + timeout = 1; + } try { byteArray = await modbusClient.GetByteArray(unitIdentifier, startingAddress, quantity, ipAddressString, port, connectDelay, timeout) From 2a9c997c57aee13734f04f19a649a2709bf714ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20K=C3=BChnel?= Date: Tue, 18 Oct 2022 00:16:44 +0200 Subject: [PATCH 5/6] refactor(ModbusService): remove unnecessary code --- Plugins.Modbus/Contracts/IModbusService.cs | 6 -- Plugins.Modbus/Services/ModbusService.cs | 75 +--------------------- 2 files changed, 1 insertion(+), 80 deletions(-) diff --git a/Plugins.Modbus/Contracts/IModbusService.cs b/Plugins.Modbus/Contracts/IModbusService.cs index 153115d20..a1fcb1340 100644 --- a/Plugins.Modbus/Contracts/IModbusService.cs +++ b/Plugins.Modbus/Contracts/IModbusService.cs @@ -2,12 +2,6 @@ public interface IModbusService { - Task ReadInt32Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, - int port, int connectDelay, int timeout, int? minimumResult); - - Task ReadInt16Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port, int connectDelaySeconds, int timeoutSeconds, int? minimumResult); - Task ReadFloatValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddress, int port, int connectDelaySeconds, int timeoutSeconds, int? minimumResult); - Task ReadValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port, int connectDelay, int timeout) where T : struct; } diff --git a/Plugins.Modbus/Services/ModbusService.cs b/Plugins.Modbus/Services/ModbusService.cs index 65b949340..c49306e2c 100644 --- a/Plugins.Modbus/Services/ModbusService.cs +++ b/Plugins.Modbus/Services/ModbusService.cs @@ -19,7 +19,7 @@ public async Task ReadValue(byte unitIdentifier, ushort startingAddre { _logger.LogTrace("{method}({unitIdentifier}, {startingAddress}, {quantity}, {ipAddressString}, {port}, " + "{connectDelay}, {timeout})", - nameof(ReadInt32Value), unitIdentifier, startingAddress, quantity, ipAddressString, port, + nameof(ReadValue), unitIdentifier, startingAddress, quantity, ipAddressString, port, connectDelay, timeout); var modbusClient = GetModbusClient(ipAddressString, port); @@ -76,79 +76,6 @@ public async Task ReadValue(byte unitIdentifier, ushort startingAddre } - public async Task ReadInt32Value(byte unitIdentifier, ushort startingAddress, ushort quantity, - string ipAddressString, int port, int connectDelay, int timeout, int? minimumResult) - { - _logger.LogTrace("{method}({unitIdentifier}, {startingAddress}, {quantity}, {ipAddressString}, {port}, " + - "{connectDelay}, {timeout}, {minimumResult})", - nameof(ReadInt32Value), unitIdentifier, startingAddress, quantity, ipAddressString, port, - connectDelay, timeout,minimumResult); - - var modbusClient = GetModbusClient(ipAddressString, port); - - 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, - int connectDelay, int timeout, int? minimumResult) - { - _logger.LogTrace("{method}({unitIdentifier}, {startingAddress}, {quantity}, {ipAddressString}, {port}, " + - "{connectDelay}, {timeout}, {minimumResult})", - nameof(ReadInt16Value), unitIdentifier, startingAddress, quantity, ipAddressString, port, - connectDelay, timeout, minimumResult); - var modbusClient = GetModbusClient(ipAddressString, port); - - 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, - int connectDelay, int timeout, int? minimumResult) - { - _logger.LogTrace("{method}({unitIdentifier}, {startingAddress}, {quantity}, {ipAddressString}, {port}, " + - "{connectDelay}, {timeout}, {minimumResult})", - nameof(ReadInt16Value), unitIdentifier, startingAddress, quantity, ipAddressString, port, - connectDelay, timeout, minimumResult); - var modbusClient = GetModbusClient(ipAddressString, port); - 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) { IModbusClient modbusClient; From 914ca8d1f9bbced1e834b7a03cb8bd2446c99a73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20K=C3=BChnel?= Date: Tue, 18 Oct 2022 00:21:11 +0200 Subject: [PATCH 6/6] refactor(ModbusClient): remove unnecessary code --- Plugins.Modbus/Contracts/IModbusClient.cs | 7 --- Plugins.Modbus/Services/ModbusClient.cs | 72 +---------------------- 2 files changed, 2 insertions(+), 77 deletions(-) diff --git a/Plugins.Modbus/Contracts/IModbusClient.cs b/Plugins.Modbus/Contracts/IModbusClient.cs index 46a695661..ed3246d7b 100644 --- a/Plugins.Modbus/Contracts/IModbusClient.cs +++ b/Plugins.Modbus/Contracts/IModbusClient.cs @@ -2,14 +2,7 @@ public interface IModbusClient : IDisposable { - Task ReadInt32Value(byte unitIdentifier, ushort startingAddress, ushort quantity, - string ipAddressString, - int port, int connectDelay, int timeout, int? minimumResult); - 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); - Task GetByteArray(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port, int connectDelay, int timeout); } diff --git a/Plugins.Modbus/Services/ModbusClient.cs b/Plugins.Modbus/Services/ModbusClient.cs index 657ba5b97..db6f6b058 100644 --- a/Plugins.Modbus/Services/ModbusClient.cs +++ b/Plugins.Modbus/Services/ModbusClient.cs @@ -15,26 +15,8 @@ public ModbusClient(ILogger logger) _semaphoreSlim = new SemaphoreSlim(1); } - public async Task ReadInt32Value(byte unitIdentifier, ushort startingAddress, ushort quantity, - string ipAddressString, int port, int connectDelay, int timeout, int? minimumResult) - { - _logger.LogTrace("{method}({unitIdentifier}, {startingAddress}, {quantity}, {ipAddressString}, {port}, " + - "{connectDelay}, {timeout}, {minimumResult})", - nameof(ReadInt32Value), unitIdentifier, startingAddress, quantity, ipAddressString, port, - connectDelay, timeout, minimumResult); - - var tmpArrayPowerComplete = await GetByteArray(unitIdentifier, startingAddress, quantity, ipAddressString, port, connectDelay, timeout).ConfigureAwait(false); - _logger.LogTrace("Converting {array} to Int value...", Convert.ToHexString(tmpArrayPowerComplete)); - var intValue = BitConverter.ToInt32(tmpArrayPowerComplete, 0); - if (minimumResult == null) - { - return intValue; - } - return intValue < minimumResult ? (int)minimumResult : intValue; - } - public async Task GetByteArray(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, - int port, int connectDelay, int timeout) + int port, int connectDelay, int timeout) { var tmpArrayPowerComplete = await GetRegisterValue(unitIdentifier, startingAddress, quantity, ipAddressString, port, connectDelay, timeout) @@ -56,44 +38,8 @@ public bool DiconnectIfConnected() return false; } - public async Task ReadInt16Value(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port, - int connectDelay, int timeout, int? minimumResult) - { - _logger.LogTrace("{method}({unitIdentifier}, {startingAddress}, {quantity}, {ipAddressString}, {port}, " + - "{connectDelay}, {timeout}, {minimumResult})", - nameof(ReadInt16Value), unitIdentifier, startingAddress, quantity, ipAddressString, port, - connectDelay, timeout, minimumResult); - - var tmpArrayPowerComplete = await GetByteArray(unitIdentifier, startingAddress, quantity, ipAddressString, port, connectDelay, timeout).ConfigureAwait(false); - _logger.LogTrace("Converting {array} to Int value...", Convert.ToHexString(tmpArrayPowerComplete)); - var intValue = BitConverter.ToInt16(tmpArrayPowerComplete, 0); - if (minimumResult == null) - { - return intValue; - } - return intValue < minimumResult ? (short)minimumResult : intValue; - } - - public async Task ReadFloatValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, int port, - int connectDelay, int timeout, int? minimumResult) - { - _logger.LogTrace("{method}({unitIdentifier}, {startingAddress}, {quantity}, {ipAddressString}, {port}, " + - "{connectDelay}, {timeout}, {minimumResult})", - nameof(ReadInt16Value), unitIdentifier, startingAddress, quantity, ipAddressString, port, - connectDelay, timeout, minimumResult); - - var tmpArrayPowerComplete = await GetByteArray(unitIdentifier, startingAddress, quantity, ipAddressString, port, connectDelay, timeout).ConfigureAwait(false); - _logger.LogTrace("Converting {array} to Int value...", Convert.ToHexString(tmpArrayPowerComplete)); - var intValue = BitConverter.ToSingle(tmpArrayPowerComplete, 0); - if (minimumResult == null) - { - return intValue; - } - return intValue < minimumResult ? (short)minimumResult : intValue; - } - private async Task GetRegisterValue(byte unitIdentifier, ushort startingAddress, ushort quantity, string ipAddressString, - int port, int connectDelay, int timeout) + int port, int connectDelay, int timeout) { ReadTimeout = (int)TimeSpan.FromSeconds(timeout).TotalMilliseconds; WriteTimeout = (int)TimeSpan.FromSeconds(timeout).TotalMilliseconds; @@ -136,18 +82,4 @@ private async Task GetRegisterValue(byte unitIdentifier, ushort starting }); } } - - public static byte[] StringToByteArray(string hex) - { - return Enumerable.Range(0, hex.Length) - .Where(x => x % 2 == 0) - .Select(x => Convert.ToByte(hex.Substring(x, 2), 16)) - .ToArray(); - } - - public void Dispose() - { - DiconnectIfConnected(); - _semaphoreSlim.Dispose(); - } }