diff --git a/Examples/Program.cs b/Examples/Program.cs index fa9e26e..fb02c1c 100644 --- a/Examples/Program.cs +++ b/Examples/Program.cs @@ -30,12 +30,7 @@ namespace Examples { //reading a value from the register collection Console.WriteLine($"BitValue is: {registers.BitValue}"); - - interf.GetRegister(nameof(registers.TestBool1)).PropertyChanged += (s, e) => { - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine(interf.GetRegister(nameof(registers.TestBool1)).StringValue); - Console.ResetColor(); - }; + Console.WriteLine($"TestEnum is: {registers.TestEnum}"); //writing a value to the registers Task.Factory.StartNew(async () => { diff --git a/Examples/TestRegisters.cs b/Examples/TestRegisters.cs index 7a37431..0fba57d 100644 --- a/Examples/TestRegisters.cs +++ b/Examples/TestRegisters.cs @@ -50,5 +50,21 @@ namespace Examples { [Register(7012)] public TimeSpan TestTime { get; private set; } + public enum CurrentState { + Undefined = 0, + State1 = 1, + State2 = 2, + //State3 = 3, + State4 = 4, + State5 = 5, + StateBetween = 100, + State6 = 6, + State7 = 7, + } + + [Register(50)] + public CurrentState TestEnum { get; private set; } + + } } diff --git a/MewtocolNet/Mewtocol/DynamicInterface.cs b/MewtocolNet/Mewtocol/DynamicInterface.cs index be0786f..6732b30 100644 --- a/MewtocolNet/Mewtocol/DynamicInterface.cs +++ b/MewtocolNet/Mewtocol/DynamicInterface.cs @@ -327,7 +327,7 @@ namespace MewtocolNet { } - internal void AddRegister (Type _colType, int _address, int _length = 1, string _name = null, bool _isBitwise = false) { + internal void AddRegister (Type _colType, int _address, int _length = 1, string _name = null, bool _isBitwise = false, Type _enumType = null) { Type regType = typeof(T); @@ -351,7 +351,7 @@ namespace MewtocolNet { } else if (regType == typeof(ushort)) { reg = new NRegister(_address, _name); } else if (regType == typeof(int)) { - reg = new NRegister(_address, _name, _isBitwise); + reg = new NRegister(_address, _name, _isBitwise, _enumType); } else if (regType == typeof(uint)) { reg = new NRegister(_address, _name); } else if (regType == typeof(float)) { diff --git a/MewtocolNet/Mewtocol/MewtocolInterface.cs b/MewtocolNet/Mewtocol/MewtocolInterface.cs index 51dcd2d..aa4eb55 100644 --- a/MewtocolNet/Mewtocol/MewtocolInterface.cs +++ b/MewtocolNet/Mewtocol/MewtocolInterface.cs @@ -264,6 +264,10 @@ namespace MewtocolNet { AddRegister(collection.GetType(), cAttribute.MemoryArea, cAttribute.StringLength, _name: propName); } + if (prop.PropertyType.IsEnum) { + AddRegister(collection.GetType(), cAttribute.MemoryArea, _name: propName, _enumType: prop.PropertyType); + } + //read number as bit array if (prop.PropertyType == typeof(BitArray)) { @@ -376,6 +380,10 @@ namespace MewtocolNet { foundToUpdate.SetValue(collection, ((NRegister)reg).Value); } + if (foundToUpdate.PropertyType.IsEnum) { + foundToUpdate.SetValue(collection, ((NRegister)reg).Value); + } + //setting back strings if (foundToUpdate.PropertyType == typeof(string)) { diff --git a/MewtocolNet/Mewtocol/Subregisters/NRegister.cs b/MewtocolNet/Mewtocol/Subregisters/NRegister.cs index 43618ad..2eff9a4 100644 --- a/MewtocolNet/Mewtocol/Subregisters/NRegister.cs +++ b/MewtocolNet/Mewtocol/Subregisters/NRegister.cs @@ -19,8 +19,33 @@ namespace MewtocolNet.Registers { /// Defines a register containing a number /// /// Memory start adress max 99999 - /// The format in which the variable is stored - public NRegister(int _adress, string _name = null, bool isBitwise = false) { + /// Name of the register + public NRegister (int _adress, string _name = null) { + + if (_adress > 99999) + throw new NotSupportedException("Memory adresses cant be greater than 99999"); + memoryAdress = _adress; + name = _name; + Type numType = typeof(T); + if (numType == typeof(short)) { + memoryLength = 0; + } else if (numType == typeof(ushort)) { + memoryLength = 0; + } else if (numType == typeof(int)) { + memoryLength = 1; + } else if (numType == typeof(uint)) { + memoryLength = 1; + } else if (numType == typeof(float)) { + memoryLength = 1; + } else if (numType == typeof(TimeSpan)) { + memoryLength = 1; + } else { + throw new NotSupportedException($"The type {numType} is not allowed for Number Registers"); + } + + } + + internal NRegister(int _adress, string _name = null, bool isBitwise = false, Type _enumType = null) { if (_adress > 99999) throw new NotSupportedException("Memory adresses cant be greater than 99999"); memoryAdress = _adress; @@ -43,6 +68,7 @@ namespace MewtocolNet.Registers { } isUsedBitwise = isBitwise; + enumType = _enumType; } diff --git a/MewtocolNet/Mewtocol/Subregisters/Register.cs b/MewtocolNet/Mewtocol/Subregisters/Register.cs index a1d0bf9..a685b87 100644 --- a/MewtocolNet/Mewtocol/Subregisters/Register.cs +++ b/MewtocolNet/Mewtocol/Subregisters/Register.cs @@ -67,6 +67,7 @@ namespace MewtocolNet.Registers { public string ContainerName => GetContainerName(); internal bool isUsedBitwise { get; set; } + internal Type enumType { get; set; } internal Register () { ValueChanged += (obj) => { @@ -110,6 +111,18 @@ namespace MewtocolNet.Registers { /// public string GetValueString () { + if (enumType != null && this is NRegister intEnumReg) { + var dict = new Dictionary(); + foreach (var name in Enum.GetNames(enumType)) { + dict.Add((int)Enum.Parse(enumType, name), name); + } + + if(dict.ContainsKey(intEnumReg.Value)) { + return $"{intEnumReg.Value} ({dict[intEnumReg.Value]})"; + } else { + return $"{intEnumReg.Value} (Missing Enum)"; + } + } if (this is NRegister shortReg) { return $"{shortReg.Value}{(isUsedBitwise ? $" [{shortReg.GetBitwise().ToBitString()}]" : "")}"; } @@ -133,7 +146,6 @@ namespace MewtocolNet.Registers { } if (this is SRegister stringReg) { return stringReg.Value.ToString(); - } return "Type of the register is not supported."; diff --git a/MewtocolNet/MewtocolNet.csproj b/MewtocolNet/MewtocolNet.csproj index 560c60b..57b2871 100644 --- a/MewtocolNet/MewtocolNet.csproj +++ b/MewtocolNet/MewtocolNet.csproj @@ -2,7 +2,7 @@ netstandard2.0 MewtocolNet - 0.3.6 + 0.4.0 Felix Weiss Womed true