2023-02-16 12:57:06 +00:00
|
|
|
using InnovEnergy.Lib.Protocols.Modbus.Connections;
|
|
|
|
using InnovEnergy.Lib.Protocols.Modbus.Conversions;
|
2023-03-10 12:57:39 +00:00
|
|
|
using InnovEnergy.Lib.Protocols.Modbus.Protocol;
|
|
|
|
using static InnovEnergy.Lib.Protocols.Modbus.Protocol.MultiRegisterEndianness;
|
|
|
|
using static InnovEnergy.Lib.Protocols.Modbus.Protocol.RegisterIndexing;
|
2023-02-16 12:57:06 +00:00
|
|
|
|
|
|
|
namespace InnovEnergy.Lib.Protocols.Modbus.Clients;
|
|
|
|
|
|
|
|
using Coils = IReadOnlyList<Boolean>;
|
|
|
|
|
|
|
|
// TODO: ModbusClient = Framer(TCP/RTU) + Connection(Serial/TCP) + Encoder(binary/ascii) + quirk(+1/0)
|
|
|
|
// Transport
|
|
|
|
|
|
|
|
public abstract class ModbusClient
|
|
|
|
{
|
2023-03-10 12:57:39 +00:00
|
|
|
protected ModbusConnection Connection { get; }
|
|
|
|
protected Byte SlaveId { get; }
|
|
|
|
protected RegisterIndexing RegisterIndexing { get; }
|
|
|
|
protected MultiRegisterEndianness Endianness { get; }
|
2023-03-07 10:19:15 +00:00
|
|
|
|
|
|
|
|
2023-02-16 12:57:06 +00:00
|
|
|
// 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<UInt16> values);
|
|
|
|
public abstract ModbusRegisters ReadWriteRegisters (UInt16 readAddress, UInt16 nbToRead,
|
|
|
|
UInt16 writeAddress, IReadOnlyList<UInt16> registersToWrite);
|
|
|
|
|
|
|
|
public UInt16 WriteMultipleCoils(UInt16 writeAddress, params Boolean[] coils)
|
|
|
|
{
|
|
|
|
return WriteMultipleCoils(writeAddress, (IReadOnlyList<Boolean>)coils);
|
|
|
|
}
|
|
|
|
|
|
|
|
public UInt16 WriteRegisters(ModbusRegisters registers)
|
|
|
|
{
|
|
|
|
return WriteRegisters(registers.StartRegister, registers);
|
|
|
|
}
|
|
|
|
|
|
|
|
public UInt16 WriteRegisters(UInt16 writeAddress, params UInt16[] values)
|
|
|
|
{
|
|
|
|
return WriteRegisters(writeAddress, (IReadOnlyList<UInt16>)values);
|
|
|
|
}
|
|
|
|
|
2023-03-10 12:57:39 +00:00
|
|
|
protected ModbusClient(ModbusConnection connection,
|
|
|
|
Byte slaveId,
|
|
|
|
RegisterIndexing registerIndexing = OneBased,
|
|
|
|
MultiRegisterEndianness endianness = LittleEndian)
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
2023-03-10 12:57:39 +00:00
|
|
|
Connection = connection;
|
|
|
|
SlaveId = slaveId;
|
|
|
|
RegisterIndexing = registerIndexing;
|
|
|
|
Endianness = endianness;
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CloseConnection() => Connection.Close();
|
2023-03-10 12:57:39 +00:00
|
|
|
|
|
|
|
protected UInt16 LogicToWire(UInt16 address) => RegisterIndexing.LogicToSerialized(address);
|
|
|
|
protected UInt16 SerializedToLogic(UInt16 address) => RegisterIndexing.SerializedToLogic(address);
|
|
|
|
}
|
|
|
|
|