using InnovEnergy.Lib.Protocols.Modbus.Connections; using InnovEnergy.Lib.Protocols.Modbus.Conversions; using InnovEnergy.Lib.Protocols.Modbus.Protocol; using static InnovEnergy.Lib.Protocols.Modbus.Protocol.MultiRegisterEndianness; using static InnovEnergy.Lib.Protocols.Modbus.Protocol.RegisterIndexing; namespace InnovEnergy.Lib.Protocols.Modbus.Clients; using Coils = IReadOnlyList; // TODO: ModbusClient = Framer(TCP/RTU) + Connection(Serial/TCP) + Encoder(binary/ascii) + quirk(+1/0) // Transport public abstract class ModbusClient { protected ModbusConnection Connection { get; } protected Byte SlaveId { get; } protected RegisterIndexing RegisterIndexing { get; } protected MultiRegisterEndianness Endianness { get; } // TODO: add additional functions: coils... public abstract Coils ReadDiscreteInputs (UInt16 readAddress, UInt16 nValues); public abstract ModbusRegisters ReadInputRegisters (UInt16 readAddress, UInt16 nValues); public abstract ModbusRegisters ReadHoldingRegisters(UInt16 readAddress, UInt16 nValues); public abstract UInt16 WriteMultipleCoils (UInt16 writeAddress, Coils coils); public abstract UInt16 WriteRegisters (UInt16 writeAddress, IReadOnlyList values); public abstract ModbusRegisters ReadWriteRegisters (UInt16 readAddress, UInt16 nbToRead, UInt16 writeAddress, IReadOnlyList registersToWrite); public UInt16 WriteMultipleCoils(UInt16 writeAddress, params Boolean[] coils) { return WriteMultipleCoils(writeAddress, (IReadOnlyList)coils); } public UInt16 WriteRegisters(ModbusRegisters registers) { return WriteRegisters(registers.StartRegister, registers); } public UInt16 WriteRegisters(UInt16 writeAddress, params UInt16[] values) { return WriteRegisters(writeAddress, (IReadOnlyList)values); } protected ModbusClient(ModbusConnection connection, Byte slaveId, RegisterIndexing registerIndexing = OneBased, MultiRegisterEndianness endianness = LittleEndian) { Connection = connection; SlaveId = slaveId; RegisterIndexing = registerIndexing; Endianness = endianness; } public void CloseConnection() => Connection.Close(); protected UInt16 LogicToWire(UInt16 address) => RegisterIndexing.LogicToSerialized(address); protected UInt16 SerializedToLogic(UInt16 address) => RegisterIndexing.SerializedToLogic(address); }