[ig] revert accidentally commited changes to Modbus lib

This commit is contained in:
atef 2023-04-04 14:51:06 +02:00
parent b088665f39
commit d564b3c3b6
4 changed files with 22 additions and 69 deletions

View File

@ -1,8 +1,6 @@
using InnovEnergy.Lib.Protocols.Modbus.Connections; using InnovEnergy.Lib.Protocols.Modbus.Connections;
using InnovEnergy.Lib.Protocols.Modbus.Conversions; using InnovEnergy.Lib.Protocols.Modbus.Conversions;
using InnovEnergy.Lib.Protocols.Modbus.Protocol; 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; namespace InnovEnergy.Lib.Protocols.Modbus.Clients;
@ -15,9 +13,7 @@ public abstract class ModbusClient
{ {
protected ModbusConnection Connection { get; } protected ModbusConnection Connection { get; }
protected Byte SlaveId { get; } protected Byte SlaveId { get; }
protected RegisterIndexing RegisterIndexing { get; }
protected MultiRegisterEndianness Endianness { get; }
// TODO: add additional functions: coils... // TODO: add additional functions: coils...
@ -45,19 +41,14 @@ public abstract class ModbusClient
} }
protected ModbusClient(ModbusConnection connection, protected ModbusClient(ModbusConnection connection,
Byte slaveId, Byte slaveId)
RegisterIndexing registerIndexing = OneBased,
MultiRegisterEndianness endianness = LittleEndian)
{ {
Connection = connection; Connection = connection;
SlaveId = slaveId; SlaveId = slaveId;
RegisterIndexing = registerIndexing;
Endianness = endianness;
} }
public void CloseConnection() => Connection.Close(); public void CloseConnection() => Connection.Close();
protected UInt16 LogicToWire(UInt16 address) => RegisterIndexing.LogicToSerialized(address);
protected UInt16 SerializedToLogic(UInt16 address) => RegisterIndexing.SerializedToLogic(address);
} }

View File

@ -30,9 +30,7 @@ public class ModbusRtuClient : ModbusClient
public override ModbusRegisters ReadInputRegisters(UInt16 readAddress, UInt16 nValues) public override ModbusRegisters ReadInputRegisters(UInt16 readAddress, UInt16 nValues)
{ {
var wireReadAddress = LogicToWire(readAddress); var cmd = new ReadInputRegistersCommandFrame(SlaveId, readAddress, nValues);
var cmd = new ReadInputRegistersCommandFrame(SlaveId, wireReadAddress, nValues);
var crc = CalcCrc(cmd); var crc = CalcCrc(cmd);
// TX // TX
@ -56,9 +54,7 @@ public class ModbusRtuClient : ModbusClient
public override ModbusRegisters ReadHoldingRegisters(UInt16 readAddress, UInt16 nValues) public override ModbusRegisters ReadHoldingRegisters(UInt16 readAddress, UInt16 nValues)
{ {
var wireReadAddress = LogicToWire(readAddress); var cmd = new ReadHoldingRegistersCommandFrame(SlaveId, readAddress, nValues);
var cmd = new ReadHoldingRegistersCommandFrame(SlaveId, wireReadAddress, nValues);
var crc = CalcCrc(cmd.Data); var crc = CalcCrc(cmd.Data);
// TX // TX
@ -86,9 +82,7 @@ public class ModbusRtuClient : ModbusClient
public override UInt16 WriteRegisters(UInt16 writeAddress, UInt16s values) public override UInt16 WriteRegisters(UInt16 writeAddress, UInt16s values)
{ {
var wireWriteAddress = LogicToWire(writeAddress); var cmd = new WriteRegistersCommandFrame(SlaveId, writeAddress, values);
var cmd = new WriteRegistersCommandFrame(SlaveId, wireWriteAddress, values);
var crc = CalcCrc(cmd); var crc = CalcCrc(cmd);
var nToRead = cmd.ExpectedResponseSize + CrcSize; 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) 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, var cmd = new ReadWriteRegistersCommandFrame(SlaveId,
wireReadAddress, readAddress,
nbToRead, nbToRead,
wireWriteAddress, writeAddress,
registersToWrite); registersToWrite);
var crc = CalcCrc(cmd); var crc = CalcCrc(cmd);

View File

@ -1,11 +1,10 @@
using System.Diagnostics; using System.Diagnostics;
using InnovEnergy.Lib.Protocols.Modbus.Connections; using InnovEnergy.Lib.Protocols.Modbus.Connections;
using InnovEnergy.Lib.Protocols.Modbus.Conversions; 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.Commands;
using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Replies; using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Replies;
using InnovEnergy.Lib.Protocols.Modbus.Tcp; using InnovEnergy.Lib.Protocols.Modbus.Tcp;
using static InnovEnergy.Lib.Protocols.Modbus.Protocol.MultiRegisterEndianness;
namespace InnovEnergy.Lib.Protocols.Modbus.Clients; namespace InnovEnergy.Lib.Protocols.Modbus.Clients;
@ -21,21 +20,16 @@ public class ModbusTcpClient : ModbusClient
private UInt16 NextId() => unchecked(++_Id); private UInt16 NextId() => unchecked(++_Id);
public ModbusTcpClient(ModbusConnection connection, public ModbusTcpClient(ModbusConnection connection, Byte slaveId) : base(connection, slaveId)
Byte slaveId,
RegisterIndexing registerIndexing = RegisterIndexing.OneBased,
MultiRegisterEndianness endianness = LittleEndian) : base(connection, slaveId, registerIndexing, endianness)
{ {
} }
public override IReadOnlyList<Boolean> ReadDiscreteInputs(UInt16 readAddress, UInt16 nValues) public override IReadOnlyList<Boolean> ReadDiscreteInputs(UInt16 readAddress, UInt16 nValues)
{ {
var wireReadAddress = LogicToWire(readAddress);
var id = NextId(); // TODO: check response id 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 hdr = new MbapHeader(id, cmd.Data.Count);
var frm = new ModbusTcpFrame(hdr, cmd); var frm = new ModbusTcpFrame(hdr, cmd);
@ -52,10 +46,9 @@ public class ModbusTcpClient : ModbusClient
public override ModbusRegisters ReadInputRegisters(UInt16 readAddress, UInt16 nValues) public override ModbusRegisters ReadInputRegisters(UInt16 readAddress, UInt16 nValues)
{ {
var wireReadAddress = LogicToWire(readAddress);
var id = NextId(); // TODO: check response id 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 hdr = new MbapHeader(id, cmd.Data.Count);
var frm = new ModbusTcpFrame(hdr, cmd); var frm = new ModbusTcpFrame(hdr, cmd);
@ -77,10 +70,8 @@ public class ModbusTcpClient : ModbusClient
public override ModbusRegisters ReadHoldingRegisters(UInt16 readAddress, UInt16 nValues) public override ModbusRegisters ReadHoldingRegisters(UInt16 readAddress, UInt16 nValues)
{ {
var wireReadAddress = LogicToWire(readAddress);
var id = NextId(); // TODO: check response id 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 hdr = new MbapHeader(id, cmd.Data.Count);
var frm = new ModbusTcpFrame(hdr, cmd); var frm = new ModbusTcpFrame(hdr, cmd);
@ -99,10 +90,8 @@ public class ModbusTcpClient : ModbusClient
public override UInt16 WriteMultipleCoils(UInt16 writeAddress, Coils coils) public override UInt16 WriteMultipleCoils(UInt16 writeAddress, Coils coils)
{ {
var wireWriteAddress = LogicToWire(writeAddress); var id = NextId(); // TODO: check response id
var cmd = new WriteCoilsCommandFrame(SlaveId, writeAddress, coils);
var id = NextId(); // TODO: check response id
var cmd = new WriteCoilsCommandFrame(SlaveId, wireWriteAddress, coils);
var hdr = new MbapHeader(id, cmd.Data.Count); var hdr = new MbapHeader(id, cmd.Data.Count);
var frm = new ModbusTcpFrame(hdr, cmd); var frm = new ModbusTcpFrame(hdr, cmd);
@ -120,10 +109,8 @@ public class ModbusTcpClient : ModbusClient
public override UInt16 WriteRegisters(UInt16 writeAddress, UInt16s values) public override UInt16 WriteRegisters(UInt16 writeAddress, UInt16s values)
{ {
var wireWriteAddress = LogicToWire(writeAddress);
var id = NextId(); // TODO: check response id 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 hdr = new MbapHeader(id, cmd.Data.Count);
var frm = new ModbusTcpFrame(hdr, cmd); 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) 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 id = NextId(); // TODO: check response id
var cmd = new ReadWriteRegistersCommandFrame(SlaveId, var cmd = new ReadWriteRegistersCommandFrame(SlaveId,
wireReadAddress, readAddress,
nbToRead, nbToRead,
wireWriteAddress, writeAddress,
registersToWrite); registersToWrite);
var hdr = new MbapHeader(id, cmd.Data.Count); var hdr = new MbapHeader(id, cmd.Data.Count);

View File

@ -1,6 +1,5 @@
using System.Collections; using System.Collections;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using static InnovEnergy.Lib.Protocols.Modbus.Protocol.MultiRegisterEndianness;
namespace InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Accessors; namespace InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Accessors;
@ -10,22 +9,11 @@ public struct MbRegisters : IReadOnlyList<UInt16>
{ {
private MbWords Words { get; } private MbWords Words { get; }
private UInt16 StartRegister { get; } private UInt16 StartRegister { get; }
private MultiRegisterEndianness Endianness { get; }
public MbRegisters(MbWords words, public MbRegisters(MbWords words, UInt16 startRegister)
UInt16 startRegister,
MultiRegisterEndianness endianness,
RegisterIndexing registerIndexing)
{ {
Words = words; Words = words;
Endianness = endianness; StartRegister = startRegister;
var start = startRegister - (Int16) registerIndexing; // TODO: check
if (start < 0)
throw new ArgumentOutOfRangeException(nameof(startRegister));
StartRegister = (UInt16)start;
} }
public IEnumerator<UInt16> GetEnumerator() => Words.GetEnumerator(); public IEnumerator<UInt16> GetEnumerator() => Words.GetEnumerator();
@ -74,9 +62,6 @@ public struct MbRegisters : IReadOnlyList<UInt16>
var hi = (UInt32) GetUInt16(i); var hi = (UInt32) GetUInt16(i);
var lo = (UInt32) GetUInt16(++i); var lo = (UInt32) GetUInt16(++i);
if (Endianness == LittleEndian)
(lo, hi) = (hi, lo);
return hi << 16 | lo; return hi << 16 | lo;
} }