From 479cdd03ba15397c71f60ab7f4e2635f34b251a0 Mon Sep 17 00:00:00 2001 From: ig Date: Tue, 7 Mar 2023 11:19:15 +0100 Subject: [PATCH] Introduce Endianness to Modbus lib --- csharp/Lib/Protocols/Modbus/Clients/Endianness.cs | 10 ++++++++++ csharp/Lib/Protocols/Modbus/Clients/ModbusClient.cs | 11 ++++++----- .../Lib/Protocols/Modbus/Clients/ModbusTcpClient.cs | 4 ++-- 3 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 csharp/Lib/Protocols/Modbus/Clients/Endianness.cs 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) { }