diff --git a/csharp/Lib/Devices/Adam6060/Adam6060Control.cs b/csharp/Lib/Devices/Adam6060/Adam6060Control.cs deleted file mode 100644 index 10167564e..000000000 --- a/csharp/Lib/Devices/Adam6060/Adam6060Control.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -namespace InnovEnergy.Lib.Devices.Adam6060; - -public class Adam6060Control -{ - internal const UInt16 RelaysStartRegister = 17; - internal const UInt16 NbRelays = 6; - - public Boolean Relay0 { get; init; } // Address(0X) 00017 - public Boolean Relay1 { get; init; } // Address(0X) 00018 - public Boolean Relay2 { get; init; } // Address(0X) 00019 - public Boolean Relay3 { get; init; } // Address(0X) 00020 - public Boolean Relay4 { get; init; } // Address(0X) 00021 - public Boolean Relay5 { get; init; } // Address(0X) 00022 -} \ No newline at end of file diff --git a/csharp/Lib/Devices/Adam6060/Adam6060Device.cs b/csharp/Lib/Devices/Adam6060/Adam6060Device.cs index d910baf41..114aa3d32 100644 --- a/csharp/Lib/Devices/Adam6060/Adam6060Device.cs +++ b/csharp/Lib/Devices/Adam6060/Adam6060Device.cs @@ -1,103 +1,18 @@ +using InnovEnergy.Lib.Protocols.Modbus.Channels; using InnovEnergy.Lib.Protocols.Modbus.Clients; -using InnovEnergy.Lib.Protocols.Modbus.Connections; -using static InnovEnergy.Lib.Devices.Adam6060.Adam6060Status; -using static InnovEnergy.Lib.Devices.Adam6060.Adam6060Control; +using InnovEnergy.Lib.Protocols.Modbus.Slaves; namespace InnovEnergy.Lib.Devices.Adam6060; -public class Adam6060Device +public class Adam6060Device : ModbusDevice { - 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) + public Adam6060Device(String hostname, Byte slaveId, UInt16 port = 502) : + this(new TcpChannel(hostname, port), slaveId) { - Hostname = hostname; - Port = port; - SlaveAddress = slaveAddress; } - private void OpenConnection() + public Adam6060Device(Channel channel, Byte slaveId) : base(new ModbusTcpClient(channel, slaveId)) { - if (Modbus is null) - { - var connection = new ModbusTcpConnection(Hostname, Port); - Modbus = new ModbusTcpClient(connection, SlaveAddress); - } - } - - public Adam6060Status? ReadStatus() - { - try - { - OpenConnection(); - return TryReadStatus(); - } - catch - { - 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); - - return new Adam6060Status - { - DigitalInput0 = inputs[0], - DigitalInput1 = inputs[1], - DigitalInput2 = inputs[2], - DigitalInput3 = inputs[3], - DigitalInput4 = inputs[4], - DigitalInput5 = inputs[5], - - Relay0 = relays[0], - Relay1 = relays[1], - Relay2 = relays[2], - Relay3 = relays[3], - Relay4 = relays[4], - Relay5 = relays[5], - }; - } - - public Boolean WriteControl(Adam6060Control control) - { - try - { - OpenConnection(); - - Modbus!.WriteMultipleCoils(RelaysStartRegister, control.Relay0, - control.Relay1, - control.Relay2, - control.Relay3, - control.Relay4, - control.Relay5); - return true; - } - catch - { - CloseConnection(); - return false; - } } } \ No newline at end of file diff --git a/csharp/Lib/Devices/Adam6060/Adam6060Registers.cs b/csharp/Lib/Devices/Adam6060/Adam6060Registers.cs new file mode 100644 index 000000000..58511cf8f --- /dev/null +++ b/csharp/Lib/Devices/Adam6060/Adam6060Registers.cs @@ -0,0 +1,23 @@ +using System.Diagnostics.CodeAnalysis; +using InnovEnergy.Lib.Protocols.Modbus.Reflection.Attributes; + +namespace InnovEnergy.Lib.Devices.Adam6060; + +[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local")] +[SuppressMessage("ReSharper", "UnusedMember.Global")] +public class Adam6060Registers +{ + [DiscreteInput(1)] public Boolean DigitalInput0 { get; private set; } + [DiscreteInput(2)] public Boolean DigitalInput1 { get; private set; } + [DiscreteInput(3)] public Boolean DigitalInput2 { get; private set; } + [DiscreteInput(4)] public Boolean DigitalInput3 { get; private set; } + [DiscreteInput(5)] public Boolean DigitalInput4 { get; private set; } + [DiscreteInput(6)] public Boolean DigitalInput5 { get; private set; } + + [Coil(17)] public Boolean Relay0 { get; set; } + [Coil(18)] public Boolean Relay1 { get; set; } + [Coil(19)] public Boolean Relay2 { get; set; } + [Coil(20)] public Boolean Relay3 { get; set; } + [Coil(21)] public Boolean Relay4 { get; set; } + [Coil(22)] public Boolean Relay5 { get; set; } +} \ No newline at end of file diff --git a/csharp/Lib/Devices/Adam6060/Adam6060Status.cs b/csharp/Lib/Devices/Adam6060/Adam6060Status.cs deleted file mode 100644 index 5036fd1c6..000000000 --- a/csharp/Lib/Devices/Adam6060/Adam6060Status.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace InnovEnergy.Lib.Devices.Adam6060; - -public class Adam6060Status : Adam6060Control -{ - internal const UInt16 DigitalInputsStartRegister = 1; - internal const UInt16 NbDigitalInputs = 6; - - public Boolean DigitalInput0 { get; init; } //Address(0X) 00001 - public Boolean DigitalInput1 { get; init; } //Address(0X) 00002 - public Boolean DigitalInput2 { get; init; } //Address(0X) 00003 - public Boolean DigitalInput3 { get; init; } //Address(0X) 00004 - public Boolean DigitalInput4 { get; init; } //Address(0X) 00005 - public Boolean DigitalInput5 { get; init; } //Address(0X) 00006 -} \ No newline at end of file