From eb73beb1cdb6b741481c03a98e9c366938155b36 Mon Sep 17 00:00:00 2001 From: ig Date: Thu, 9 Mar 2023 13:16:26 +0100 Subject: [PATCH] Implement failure resilient TCP connections for Adam and AMPT --- .../Lib/Devices/AMPT/AmptCommunicationUnit.cs | 30 ++++++--- csharp/Lib/Devices/Adam6060/Adam6060Device.cs | 61 ++++++++++++++----- 2 files changed, 67 insertions(+), 24 deletions(-) diff --git a/csharp/Lib/Devices/AMPT/AmptCommunicationUnit.cs b/csharp/Lib/Devices/AMPT/AmptCommunicationUnit.cs index 4238877ac..6491f4e8b 100644 --- a/csharp/Lib/Devices/AMPT/AmptCommunicationUnit.cs +++ b/csharp/Lib/Devices/AMPT/AmptCommunicationUnit.cs @@ -27,30 +27,42 @@ public class AmptCommunicationUnit { try { - var modbus = OpenConnection(); - return TryReadStatus(modbus); + OpenConnection(); + return TryReadStatus(); } - catch + catch { - Modbus?.CloseConnection(); + CloseConnection(); return null; } } - private ModbusTcpClient OpenConnection() + private void CloseConnection() + { + try + { + Modbus?.CloseConnection(); + } + catch + { + // ignored + } + + Modbus = null; + } + + private void OpenConnection() { if (Modbus is null) { var connection = new ModbusTcpConnection(Hostname, Port); Modbus = new ModbusTcpClient(connection, SlaveAddress); } - - return Modbus; } - private static AmptCommunicationUnitStatus TryReadStatus(ModbusTcpClient modbus) + private AmptCommunicationUnitStatus TryReadStatus() { - var r = modbus.ReadHoldingRegisters(1, 116); + var r = Modbus!.ReadHoldingRegisters(1, 116); var currentFactor = Pow(10.0m, r.GetInt16(73)); var voltageFactor = Pow(10.0m, r.GetInt16(74)); diff --git a/csharp/Lib/Devices/Adam6060/Adam6060Device.cs b/csharp/Lib/Devices/Adam6060/Adam6060Device.cs index 09af1a1a0..d910baf41 100644 --- a/csharp/Lib/Devices/Adam6060/Adam6060Device.cs +++ b/csharp/Lib/Devices/Adam6060/Adam6060Device.cs @@ -7,31 +7,60 @@ namespace InnovEnergy.Lib.Devices.Adam6060; public class Adam6060Device { - private ModbusTcpClient Modbus { get; } + public String Hostname { get; } + public UInt16 Port { get; } + public Byte SlaveAddress { get; } + + private ModbusTcpClient? Modbus { get; set; } public Adam6060Device(String hostname, UInt16 port = 5004, Byte slaveAddress = 2) { - var connection = new ModbusTcpConnection(hostname, port); - Modbus = new ModbusTcpClient(connection, slaveAddress); + Hostname = hostname; + Port = port; + SlaveAddress = slaveAddress; + } + + private void OpenConnection() + { + if (Modbus is null) + { + var connection = new ModbusTcpConnection(Hostname, Port); + Modbus = new ModbusTcpClient(connection, SlaveAddress); + } } public Adam6060Status? ReadStatus() { try { + OpenConnection(); return TryReadStatus(); } - catch (Exception) + catch { - Modbus.CloseConnection(); + CloseConnection(); return null; } } + private void CloseConnection() + { + try + { + Modbus?.CloseConnection(); + } + catch + { + // ignored + } + + Modbus = null; + } + private Adam6060Status TryReadStatus() { - var inputs = Modbus.ReadDiscreteInputs(DigitalInputsStartRegister, NbDigitalInputs); - var relays = Modbus.ReadDiscreteInputs(RelaysStartRegister, NbRelays); + var inputs = Modbus!.ReadDiscreteInputs(DigitalInputsStartRegister, NbDigitalInputs); + var relays = Modbus!.ReadDiscreteInputs(RelaysStartRegister, NbRelays); return new Adam6060Status { @@ -55,17 +84,19 @@ public class Adam6060Device { try { - Modbus.WriteMultipleCoils(RelaysStartRegister, control.Relay0, - control.Relay1, - control.Relay2, - control.Relay3, - control.Relay4, - control.Relay5); + OpenConnection(); + + Modbus!.WriteMultipleCoils(RelaysStartRegister, control.Relay0, + control.Relay1, + control.Relay2, + control.Relay3, + control.Relay4, + control.Relay5); return true; } - catch (Exception) + catch { - Modbus.CloseConnection(); + CloseConnection(); return false; } }