From b1c2cdb70e7a39628d777191daa15373921d53bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Wei=C3=9F?= <72068105+Sandoun@users.noreply.github.com> Date: Wed, 13 Jul 2022 18:28:28 +0200 Subject: [PATCH] Fixed exception on duplicate memory adresses when the adress area was different - counted up version number --- Examples/TestRegisters.cs | 3 ++ MewtocolNet/Mewtocol/DynamicInterface.cs | 64 +++++++++++------------ MewtocolNet/Mewtocol/MewtocolInterface.cs | 2 +- MewtocolNet/MewtocolNet.csproj | 2 +- 4 files changed, 37 insertions(+), 34 deletions(-) diff --git a/Examples/TestRegisters.cs b/Examples/TestRegisters.cs index 0fba57d..9c01b97 100644 --- a/Examples/TestRegisters.cs +++ b/Examples/TestRegisters.cs @@ -10,6 +10,9 @@ namespace Examples { [Register(1000, RegisterType.R)] public bool TestBool1 { get; private set; } + [Register(1000)] + public int TestDuplicate { get; private set; } + //corresponds to a XD input of the PLC [Register(RegisterType.X, SpecialAddress.D)] public bool TestBoolInputXD { get; private set; } diff --git a/MewtocolNet/Mewtocol/DynamicInterface.cs b/MewtocolNet/Mewtocol/DynamicInterface.cs index 380555f..3d40a0a 100644 --- a/MewtocolNet/Mewtocol/DynamicInterface.cs +++ b/MewtocolNet/Mewtocol/DynamicInterface.cs @@ -89,9 +89,7 @@ namespace MewtocolNet { } - foreach (var registerPair in Registers) { - - var reg = registerPair.Value; + foreach (var reg in Registers) { if (reg is NRegister shortReg) { var lastVal = shortReg.Value; @@ -210,7 +208,7 @@ namespace MewtocolNet { toAdd = new BRegister(_address, _type, _name); } - Registers.Add(_address, toAdd); + Registers.Add(toAdd); } @@ -240,7 +238,7 @@ namespace MewtocolNet { } toAdd.collectionType = _colType; - Registers.Add(_address, toAdd); + Registers.Add(toAdd); } @@ -261,7 +259,7 @@ namespace MewtocolNet { public void AddRegister (SpecialAddress _spAddress, RegisterType _type, string _name = null) { //as bool registers - Registers.Add((int)_spAddress, new BRegister(_spAddress, _type, _name)); + Registers.Add(new BRegister(_spAddress, _type, _name)); } @@ -272,7 +270,7 @@ namespace MewtocolNet { reg.collectionType = _colType; //as bool registers - Registers.Add((int)_spAddress, reg); + Registers.Add(reg); } @@ -301,32 +299,35 @@ namespace MewtocolNet { throw new NotSupportedException($"_lenght parameter only allowed for register of type string"); } - if (Registers.Any(x => x.Key == _address)) { - throw new NotSupportedException($"Cannot add a register multiple times, " + - $"make sure that all register attributes or AddRegister assignments have different adresses."); - } + Register toAdd; if (regType == typeof(short)) { - Registers.Add(_address, new NRegister(_address, _name)); + toAdd = new NRegister(_address, _name); } else if (regType == typeof(ushort)) { - Registers.Add(_address, new NRegister(_address, _name)); + toAdd = new NRegister(_address, _name); } else if (regType == typeof(int)) { - Registers.Add(_address, new NRegister(_address, _name)); + toAdd = new NRegister(_address, _name); } else if (regType == typeof(uint)) { - Registers.Add(_address, new NRegister(_address, _name)); + toAdd = new NRegister(_address, _name); } else if (regType == typeof(float)) { - Registers.Add(_address, new NRegister(_address, _name)); + toAdd = new NRegister(_address, _name); } else if (regType == typeof(string)) { - Registers.Add(_address, new SRegister(_address, _length, _name)); + toAdd = new SRegister(_address, _length, _name); } else if (regType == typeof(TimeSpan)) { - Registers.Add(_address, new NRegister(_address, _name)); + toAdd = new NRegister(_address, _name); } else if (regType == typeof(bool)) { - Registers.Add(_address, new BRegister(_address, RegisterType.R, _name)); + toAdd = new BRegister(_address, RegisterType.R, _name); } else { throw new NotSupportedException($"The type {regType} is not allowed for Registers \n" + $"Allowed are: short, ushort, int, uint, float and string"); } + + if (Registers.Any(x => x.GetRegisterPLCName() == toAdd.GetRegisterPLCName())) { + throw new NotSupportedException($"Cannot add a register multiple times, " + + $"make sure that all register attributes or AddRegister assignments have different adresses."); + } + } internal void AddRegister (Type _colType, int _address, int _length = 1, string _name = null, bool _isBitwise = false, Type _enumType = null) { @@ -337,12 +338,7 @@ namespace MewtocolNet { throw new NotSupportedException($"_lenght parameter only allowed for register of type string"); } - if (Registers.Any(x => x.Key == _address) && !_isBitwise) { - throw new NotSupportedException($"Cannot add a register multiple times, " + - $"make sure that all register attributes or AddRegister assignments have different adresses."); - } - - if (Registers.Any(x => x.Key == _address) && _isBitwise) { + if (Registers.Any(x => x.MemoryAdress == _address) && _isBitwise) { return; } @@ -366,15 +362,19 @@ namespace MewtocolNet { reg = new BRegister(_address, RegisterType.R, _name); } - if (reg == null) { throw new NotSupportedException($"The type {regType} is not allowed for Registers \n" + $"Allowed are: short, ushort, int, uint, float and string"); } else { - reg.collectionType = _colType; - Registers.Add(_address, reg); + if (Registers.Any(x => x.GetRegisterPLCName() == reg.GetRegisterPLCName()) && !_isBitwise) { + throw new NotSupportedException($"Cannot add a register multiple times, " + + $"make sure that all register attributes or AddRegister assignments have different adresses."); + } + + reg.collectionType = _colType; + Registers.Add(reg); } } @@ -389,7 +389,7 @@ namespace MewtocolNet { /// public Register GetRegister (string name) { - return Registers.FirstOrDefault(x => x.Value.Name == name).Value; + return Registers.FirstOrDefault(x => x.Name == name); } @@ -400,8 +400,8 @@ namespace MewtocolNet { /// A casted register or the default value public T GetRegister (string name) where T : Register { try { - var reg = Registers.FirstOrDefault(x => x.Value.Name == name); - return reg.Value as T; + var reg = Registers.FirstOrDefault(x => x.Name == name); + return reg as T; } catch (InvalidCastException) { return default(T); } @@ -416,7 +416,7 @@ namespace MewtocolNet { /// public List GetAllRegisters () { - return Registers.Values.ToList(); + return Registers; } diff --git a/MewtocolNet/Mewtocol/MewtocolInterface.cs b/MewtocolNet/Mewtocol/MewtocolInterface.cs index 026db0c..f92fbe5 100644 --- a/MewtocolNet/Mewtocol/MewtocolInterface.cs +++ b/MewtocolNet/Mewtocol/MewtocolInterface.cs @@ -67,7 +67,7 @@ namespace MewtocolNet { /// /// The registered data registers of the PLC /// - public Dictionary Registers { get; set; } = new Dictionary(); + public List Registers { get; set; } = new List(); private string ip; private int port; diff --git a/MewtocolNet/MewtocolNet.csproj b/MewtocolNet/MewtocolNet.csproj index 55f44ae..86110db 100644 --- a/MewtocolNet/MewtocolNet.csproj +++ b/MewtocolNet/MewtocolNet.csproj @@ -2,7 +2,7 @@ netstandard2.0 MewtocolNet - 0.4.2 + 0.4.3 Felix Weiss Womed true