diff --git a/csharp/Lib/Protocols/Modbus/Clients/Endianness.cs b/csharp/Lib/Protocols/Modbus/Clients/Endianness.cs new file mode 100644 index 000000000..d53103dec --- /dev/null +++ b/csharp/Lib/Protocols/Modbus/Clients/Endianness.cs @@ -0,0 +1,10 @@ +namespace InnovEnergy.Lib.Protocols.Modbus.Clients; + +[Flags] +public enum Endianness +{ + LittleEndian32BitIntegers = 0, + LittleEndian32Floats = 0, + BigEndian32BitIntegers = 1, + BigEndian32Floats = 2, +} \ No newline at end of file diff --git a/csharp/Lib/Protocols/Modbus/Clients/ModbusClient.cs b/csharp/Lib/Protocols/Modbus/Clients/ModbusClient.cs index 66576d9d1..1bc736622 100644 --- a/csharp/Lib/Protocols/Modbus/Clients/ModbusClient.cs +++ b/csharp/Lib/Protocols/Modbus/Clients/ModbusClient.cs @@ -1,6 +1,6 @@ -using System.Threading.Channels; using InnovEnergy.Lib.Protocols.Modbus.Connections; using InnovEnergy.Lib.Protocols.Modbus.Conversions; +using static InnovEnergy.Lib.Protocols.Modbus.Clients.Endianness; namespace InnovEnergy.Lib.Protocols.Modbus.Clients; @@ -13,8 +13,9 @@ public abstract class ModbusClient { protected ModbusConnection Connection { get; } protected Byte SlaveId { get; } - - + protected Endianness Endianness { get; } + + // TODO: add additional functions: coils... public abstract Coils ReadDiscreteInputs (UInt16 readAddress, UInt16 nValues); @@ -40,11 +41,11 @@ public abstract class ModbusClient return WriteRegisters(writeAddress, (IReadOnlyList)values); } - - protected ModbusClient(ModbusConnection connection, Byte slaveId) + protected ModbusClient(ModbusConnection connection, Byte slaveId, Endianness endianness = LittleEndian32BitIntegers | LittleEndian32Floats) { Connection = connection; SlaveId = slaveId; + Endianness = endianness; } public void CloseConnection() => Connection.Close(); diff --git a/csharp/Lib/Protocols/Modbus/Clients/ModbusTcpClient.cs b/csharp/Lib/Protocols/Modbus/Clients/ModbusTcpClient.cs index 89824106c..c0506cbf3 100644 --- a/csharp/Lib/Protocols/Modbus/Clients/ModbusTcpClient.cs +++ b/csharp/Lib/Protocols/Modbus/Clients/ModbusTcpClient.cs @@ -4,13 +4,13 @@ using InnovEnergy.Lib.Protocols.Modbus.Conversions; using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Commands; using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Replies; using InnovEnergy.Lib.Protocols.Modbus.Tcp; +using static InnovEnergy.Lib.Protocols.Modbus.Clients.Endianness; namespace InnovEnergy.Lib.Protocols.Modbus.Clients; using UInt16s = IReadOnlyList; using Coils = IReadOnlyList; - public class ModbusTcpClient : ModbusClient { public const UInt16 DefaultPort = 502; @@ -20,7 +20,7 @@ public class ModbusTcpClient : ModbusClient private UInt16 NextId() => unchecked(++_Id); - public ModbusTcpClient(ModbusConnection connection, Byte slaveId) : base(connection, slaveId) + public ModbusTcpClient(ModbusConnection connection, Byte slaveId, Endianness endianness = LittleEndian32BitIntegers | LittleEndian32Floats) : base(connection, slaveId, endianness) { }