diff --git a/csharp/Lib/Protocols/Modbus/Clients/ModbusClient.cs b/csharp/Lib/Protocols/Modbus/Clients/ModbusClient.cs index c992d0874..0a4ca6948 100644 --- a/csharp/Lib/Protocols/Modbus/Clients/ModbusClient.cs +++ b/csharp/Lib/Protocols/Modbus/Clients/ModbusClient.cs @@ -1,8 +1,6 @@ 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; @@ -15,9 +13,7 @@ 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... @@ -45,19 +41,14 @@ public abstract class ModbusClient } protected ModbusClient(ModbusConnection connection, - Byte slaveId, - RegisterIndexing registerIndexing = OneBased, - MultiRegisterEndianness endianness = LittleEndian) + Byte slaveId) { 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); + } diff --git a/csharp/Lib/Protocols/Modbus/Clients/ModbusRtuClient.cs b/csharp/Lib/Protocols/Modbus/Clients/ModbusRtuClient.cs index c49456ab0..24171c847 100644 --- a/csharp/Lib/Protocols/Modbus/Clients/ModbusRtuClient.cs +++ b/csharp/Lib/Protocols/Modbus/Clients/ModbusRtuClient.cs @@ -30,9 +30,7 @@ public class ModbusRtuClient : ModbusClient public override ModbusRegisters ReadInputRegisters(UInt16 readAddress, UInt16 nValues) { - var wireReadAddress = LogicToWire(readAddress); - - var cmd = new ReadInputRegistersCommandFrame(SlaveId, wireReadAddress, nValues); + var cmd = new ReadInputRegistersCommandFrame(SlaveId, readAddress, nValues); var crc = CalcCrc(cmd); // TX @@ -56,9 +54,7 @@ public class ModbusRtuClient : ModbusClient public override ModbusRegisters ReadHoldingRegisters(UInt16 readAddress, UInt16 nValues) { - var wireReadAddress = LogicToWire(readAddress); - - var cmd = new ReadHoldingRegistersCommandFrame(SlaveId, wireReadAddress, nValues); + var cmd = new ReadHoldingRegistersCommandFrame(SlaveId, readAddress, nValues); var crc = CalcCrc(cmd.Data); // TX @@ -86,9 +82,7 @@ public class ModbusRtuClient : ModbusClient public override UInt16 WriteRegisters(UInt16 writeAddress, UInt16s values) { - var wireWriteAddress = LogicToWire(writeAddress); - - var cmd = new WriteRegistersCommandFrame(SlaveId, wireWriteAddress, values); + var cmd = new WriteRegistersCommandFrame(SlaveId, writeAddress, values); var crc = CalcCrc(cmd); var nToRead = cmd.ExpectedResponseSize + CrcSize; @@ -111,13 +105,10 @@ public class ModbusRtuClient : ModbusClient public override ModbusRegisters ReadWriteRegisters(UInt16 readAddress, UInt16 nbToRead, UInt16 writeAddress, UInt16s registersToWrite) { - var wireReadAddress = LogicToWire(readAddress); - var wireWriteAddress = LogicToWire(writeAddress); - var cmd = new ReadWriteRegistersCommandFrame(SlaveId, - wireReadAddress, + readAddress, nbToRead, - wireWriteAddress, + writeAddress, registersToWrite); var crc = CalcCrc(cmd); diff --git a/csharp/Lib/Protocols/Modbus/Clients/ModbusTcpClient.cs b/csharp/Lib/Protocols/Modbus/Clients/ModbusTcpClient.cs index 2952d4243..795cb9189 100644 --- a/csharp/Lib/Protocols/Modbus/Clients/ModbusTcpClient.cs +++ b/csharp/Lib/Protocols/Modbus/Clients/ModbusTcpClient.cs @@ -1,11 +1,10 @@ using System.Diagnostics; using InnovEnergy.Lib.Protocols.Modbus.Connections; using InnovEnergy.Lib.Protocols.Modbus.Conversions; -using InnovEnergy.Lib.Protocols.Modbus.Protocol; 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.Protocol.MultiRegisterEndianness; + namespace InnovEnergy.Lib.Protocols.Modbus.Clients; @@ -21,21 +20,16 @@ public class ModbusTcpClient : ModbusClient private UInt16 NextId() => unchecked(++_Id); - public ModbusTcpClient(ModbusConnection connection, - Byte slaveId, - RegisterIndexing registerIndexing = RegisterIndexing.OneBased, - MultiRegisterEndianness endianness = LittleEndian) : base(connection, slaveId, registerIndexing, endianness) + public ModbusTcpClient(ModbusConnection connection, Byte slaveId) : base(connection, slaveId) { } public override IReadOnlyList ReadDiscreteInputs(UInt16 readAddress, UInt16 nValues) { - var wireReadAddress = LogicToWire(readAddress); - var id = NextId(); // TODO: check response id - var cmd = new ReadDiscreteInputsCommandFrame(SlaveId, wireReadAddress, nValues); + var cmd = new ReadDiscreteInputsCommandFrame(SlaveId, readAddress, nValues); var hdr = new MbapHeader(id, cmd.Data.Count); var frm = new ModbusTcpFrame(hdr, cmd); @@ -52,10 +46,9 @@ public class ModbusTcpClient : ModbusClient public override ModbusRegisters ReadInputRegisters(UInt16 readAddress, UInt16 nValues) { - var wireReadAddress = LogicToWire(readAddress); var id = NextId(); // TODO: check response id - var cmd = new ReadInputRegistersCommandFrame(SlaveId, wireReadAddress, nValues); + var cmd = new ReadInputRegistersCommandFrame(SlaveId, readAddress, nValues); var hdr = new MbapHeader(id, cmd.Data.Count); var frm = new ModbusTcpFrame(hdr, cmd); @@ -77,10 +70,8 @@ public class ModbusTcpClient : ModbusClient public override ModbusRegisters ReadHoldingRegisters(UInt16 readAddress, UInt16 nValues) { - var wireReadAddress = LogicToWire(readAddress); - var id = NextId(); // TODO: check response id - var cmd = new ReadHoldingRegistersCommandFrame(SlaveId, wireReadAddress, nValues); + var cmd = new ReadHoldingRegistersCommandFrame(SlaveId, readAddress, nValues); var hdr = new MbapHeader(id, cmd.Data.Count); var frm = new ModbusTcpFrame(hdr, cmd); @@ -99,10 +90,8 @@ public class ModbusTcpClient : ModbusClient public override UInt16 WriteMultipleCoils(UInt16 writeAddress, Coils coils) { - var wireWriteAddress = LogicToWire(writeAddress); - - var id = NextId(); // TODO: check response id - var cmd = new WriteCoilsCommandFrame(SlaveId, wireWriteAddress, coils); + var id = NextId(); // TODO: check response id + var cmd = new WriteCoilsCommandFrame(SlaveId, writeAddress, coils); var hdr = new MbapHeader(id, cmd.Data.Count); var frm = new ModbusTcpFrame(hdr, cmd); @@ -120,10 +109,8 @@ public class ModbusTcpClient : ModbusClient public override UInt16 WriteRegisters(UInt16 writeAddress, UInt16s values) { - var wireWriteAddress = LogicToWire(writeAddress); - var id = NextId(); // TODO: check response id - var cmd = new WriteRegistersCommandFrame(SlaveId, wireWriteAddress, values); + var cmd = new WriteRegistersCommandFrame(SlaveId, writeAddress, values); var hdr = new MbapHeader(id, cmd.Data.Count); var frm = new ModbusTcpFrame(hdr, cmd); @@ -142,15 +129,14 @@ public class ModbusTcpClient : ModbusClient public override ModbusRegisters ReadWriteRegisters(UInt16 readAddress, UInt16 nbToRead, UInt16 writeAddress, UInt16s registersToWrite) { - var wireReadAddress = LogicToWire(readAddress); - var wireWriteAddress = LogicToWire(writeAddress); + var id = NextId(); // TODO: check response id var cmd = new ReadWriteRegistersCommandFrame(SlaveId, - wireReadAddress, + readAddress, nbToRead, - wireWriteAddress, + writeAddress, registersToWrite); var hdr = new MbapHeader(id, cmd.Data.Count); diff --git a/csharp/Lib/Protocols/Modbus/Protocol/Frames/Accessors/MbRegisters.cs b/csharp/Lib/Protocols/Modbus/Protocol/Frames/Accessors/MbRegisters.cs index 6db8492b9..6ff463460 100644 --- a/csharp/Lib/Protocols/Modbus/Protocol/Frames/Accessors/MbRegisters.cs +++ b/csharp/Lib/Protocols/Modbus/Protocol/Frames/Accessors/MbRegisters.cs @@ -1,6 +1,5 @@ using System.Collections; using InnovEnergy.Lib.Utils; -using static InnovEnergy.Lib.Protocols.Modbus.Protocol.MultiRegisterEndianness; namespace InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Accessors; @@ -10,22 +9,11 @@ public struct MbRegisters : IReadOnlyList { private MbWords Words { get; } private UInt16 StartRegister { get; } - private MultiRegisterEndianness Endianness { get; } - public MbRegisters(MbWords words, - UInt16 startRegister, - MultiRegisterEndianness endianness, - RegisterIndexing registerIndexing) + public MbRegisters(MbWords words, UInt16 startRegister) { - Words = words; - Endianness = endianness; - - var start = startRegister - (Int16) registerIndexing; // TODO: check - - if (start < 0) - throw new ArgumentOutOfRangeException(nameof(startRegister)); - - StartRegister = (UInt16)start; + Words = words; + StartRegister = startRegister; } public IEnumerator GetEnumerator() => Words.GetEnumerator(); @@ -74,9 +62,6 @@ public struct MbRegisters : IReadOnlyList var hi = (UInt32) GetUInt16(i); var lo = (UInt32) GetUInt16(++i); - if (Endianness == LittleEndian) - (lo, hi) = (hi, lo); - return hi << 16 | lo; }