mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 11:11:23 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e953938a65 | ||
|
|
83f17a4eae |
@@ -2,6 +2,7 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MewtocolNet;
|
using MewtocolNet;
|
||||||
using MewtocolNet.Logging;
|
using MewtocolNet.Logging;
|
||||||
|
using MewtocolNet.Registers;
|
||||||
|
|
||||||
namespace Examples {
|
namespace Examples {
|
||||||
|
|
||||||
@@ -29,12 +30,7 @@ namespace Examples {
|
|||||||
|
|
||||||
//reading a value from the register collection
|
//reading a value from the register collection
|
||||||
Console.WriteLine($"BitValue is: {registers.BitValue}");
|
Console.WriteLine($"BitValue is: {registers.BitValue}");
|
||||||
|
Console.WriteLine($"TestEnum is: {registers.TestEnum}");
|
||||||
interf.GetRegister(nameof(registers.TestBool1)).PropertyChanged += (s, e) => {
|
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
|
||||||
Console.WriteLine(interf.GetRegister(nameof(registers.TestBool1)).StringValue);
|
|
||||||
Console.ResetColor();
|
|
||||||
};
|
|
||||||
|
|
||||||
//writing a value to the registers
|
//writing a value to the registers
|
||||||
Task.Factory.StartNew(async () => {
|
Task.Factory.StartNew(async () => {
|
||||||
|
|||||||
@@ -50,5 +50,21 @@ namespace Examples {
|
|||||||
[Register(7012)]
|
[Register(7012)]
|
||||||
public TimeSpan TestTime { get; private set; }
|
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; }
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -327,7 +327,7 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void AddRegister<T> (Type _colType, int _address, int _length = 1, string _name = null, bool _isBitwise = false) {
|
internal void AddRegister<T> (Type _colType, int _address, int _length = 1, string _name = null, bool _isBitwise = false, Type _enumType = null) {
|
||||||
|
|
||||||
Type regType = typeof(T);
|
Type regType = typeof(T);
|
||||||
|
|
||||||
@@ -351,7 +351,7 @@ namespace MewtocolNet {
|
|||||||
} else if (regType == typeof(ushort)) {
|
} else if (regType == typeof(ushort)) {
|
||||||
reg = new NRegister<ushort>(_address, _name);
|
reg = new NRegister<ushort>(_address, _name);
|
||||||
} else if (regType == typeof(int)) {
|
} else if (regType == typeof(int)) {
|
||||||
reg = new NRegister<int>(_address, _name, _isBitwise);
|
reg = new NRegister<int>(_address, _name, _isBitwise, _enumType);
|
||||||
} else if (regType == typeof(uint)) {
|
} else if (regType == typeof(uint)) {
|
||||||
reg = new NRegister<uint>(_address, _name);
|
reg = new NRegister<uint>(_address, _name);
|
||||||
} else if (regType == typeof(float)) {
|
} else if (regType == typeof(float)) {
|
||||||
|
|||||||
@@ -264,6 +264,10 @@ namespace MewtocolNet {
|
|||||||
AddRegister<string>(collection.GetType(), cAttribute.MemoryArea, cAttribute.StringLength, _name: propName);
|
AddRegister<string>(collection.GetType(), cAttribute.MemoryArea, cAttribute.StringLength, _name: propName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (prop.PropertyType.IsEnum) {
|
||||||
|
AddRegister<int>(collection.GetType(), cAttribute.MemoryArea, _name: propName, _enumType: prop.PropertyType);
|
||||||
|
}
|
||||||
|
|
||||||
//read number as bit array
|
//read number as bit array
|
||||||
if (prop.PropertyType == typeof(BitArray)) {
|
if (prop.PropertyType == typeof(BitArray)) {
|
||||||
|
|
||||||
@@ -278,18 +282,13 @@ namespace MewtocolNet {
|
|||||||
//read number as bit array by invdividual properties
|
//read number as bit array by invdividual properties
|
||||||
if (prop.PropertyType == typeof(bool) && cAttribute.AssignedBitIndex != -1) {
|
if (prop.PropertyType == typeof(bool) && cAttribute.AssignedBitIndex != -1) {
|
||||||
|
|
||||||
if (cAttribute.BitCount == BitCount.B16) {
|
//var bitwiseCount = Registers.Count(x => x.Value.isUsedBitwise);
|
||||||
AddRegister<short>(collection.GetType(), cAttribute.MemoryArea, _name: propName, _isBitwise: true);
|
|
||||||
} else {
|
|
||||||
AddRegister<int>(collection.GetType(), cAttribute.MemoryArea, _name: propName, _isBitwise: true);
|
|
||||||
}
|
|
||||||
|
|
||||||
//attach for bools to be read when bitregister
|
if (cAttribute.BitCount == BitCount.B16) {
|
||||||
//RegisterChanged += (reg) => {
|
AddRegister<short>(collection.GetType(), cAttribute.MemoryArea, _name: $"Auto_Bitwise_DT{cAttribute.MemoryArea}", _isBitwise: true);
|
||||||
// if (reg.Name == propName) {
|
} else {
|
||||||
// prop.SetValue()
|
AddRegister<int>(collection.GetType(), cAttribute.MemoryArea, _name: $"Auto_Bitwise_DDT{cAttribute.MemoryArea}", _isBitwise: true);
|
||||||
// }
|
}
|
||||||
//};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -381,6 +380,10 @@ namespace MewtocolNet {
|
|||||||
foundToUpdate.SetValue(collection, ((NRegister<float>)reg).Value);
|
foundToUpdate.SetValue(collection, ((NRegister<float>)reg).Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (foundToUpdate.PropertyType.IsEnum) {
|
||||||
|
foundToUpdate.SetValue(collection, ((NRegister<int>)reg).Value);
|
||||||
|
}
|
||||||
|
|
||||||
//setting back strings
|
//setting back strings
|
||||||
|
|
||||||
if (foundToUpdate.PropertyType == typeof(string)) {
|
if (foundToUpdate.PropertyType == typeof(string)) {
|
||||||
|
|||||||
@@ -183,7 +183,7 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
var result = await SendCommandAsync(requeststring);
|
var result = await SendCommandAsync(requeststring);
|
||||||
|
|
||||||
return result.Success && result.Response.StartsWith($"%{ GetStationNumber()}#WC");
|
return result.Success && result.Response.StartsWith($"%{ GetStationNumber()}$WC");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -313,7 +313,7 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
var result = await SendCommandAsync(requeststring);
|
var result = await SendCommandAsync(requeststring);
|
||||||
|
|
||||||
return result.Success && result.Response.StartsWith($"%{ GetStationNumber()}#WD");
|
return result.Success && result.Response.StartsWith($"%{ GetStationNumber()}$WD");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -369,7 +369,7 @@ namespace MewtocolNet {
|
|||||||
var result = await SendCommandAsync(requeststring);
|
var result = await SendCommandAsync(requeststring);
|
||||||
|
|
||||||
|
|
||||||
return result.Success && result.Response.StartsWith($"%{ GetStationNumber()}#WD");
|
return result.Success && result.Response.StartsWith($"%{ GetStationNumber()}$WD");
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -19,8 +19,33 @@ namespace MewtocolNet.Registers {
|
|||||||
/// Defines a register containing a number
|
/// Defines a register containing a number
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="_adress">Memory start adress max 99999</param>
|
/// <param name="_adress">Memory start adress max 99999</param>
|
||||||
/// <param name="_format">The format in which the variable is stored</param>
|
/// <param name="_name">Name of the register</param>
|
||||||
public NRegister(int _adress, string _name = null, bool isBitwise = false) {
|
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");
|
if (_adress > 99999) throw new NotSupportedException("Memory adresses cant be greater than 99999");
|
||||||
memoryAdress = _adress;
|
memoryAdress = _adress;
|
||||||
@@ -43,6 +68,7 @@ namespace MewtocolNet.Registers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
isUsedBitwise = isBitwise;
|
isUsedBitwise = isBitwise;
|
||||||
|
enumType = _enumType;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
namespace MewtocolNet.Registers {
|
using System;
|
||||||
|
|
||||||
|
namespace MewtocolNet.Registers {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Result for a read/write operation
|
/// Result for a read/write operation
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -11,6 +13,19 @@
|
|||||||
string errmsg = Result.Success ? "" : $", Error [{Result.ErrorDescription}]";
|
string errmsg = Result.Success ? "" : $", Error [{Result.ErrorDescription}]";
|
||||||
return $"Result [{Result.Success}], Register [{Register.ToString()}]{errmsg}";
|
return $"Result [{Result.Success}], Register [{Register.ToString()}]{errmsg}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Trys to get the value of there is one
|
||||||
|
/// </summary>
|
||||||
|
public bool TryGetValue (out T value) {
|
||||||
|
if(Result.Success) {
|
||||||
|
value = Register.Value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
value = default(T);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ namespace MewtocolNet.Registers {
|
|||||||
public string ContainerName => GetContainerName();
|
public string ContainerName => GetContainerName();
|
||||||
|
|
||||||
internal bool isUsedBitwise { get; set; }
|
internal bool isUsedBitwise { get; set; }
|
||||||
|
internal Type enumType { get; set; }
|
||||||
|
|
||||||
internal Register () {
|
internal Register () {
|
||||||
ValueChanged += (obj) => {
|
ValueChanged += (obj) => {
|
||||||
@@ -110,6 +111,18 @@ namespace MewtocolNet.Registers {
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public string GetValueString () {
|
public string GetValueString () {
|
||||||
|
|
||||||
|
if (enumType != null && this is NRegister<int> intEnumReg) {
|
||||||
|
var dict = new Dictionary<int, string>();
|
||||||
|
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<short> shortReg) {
|
if (this is NRegister<short> shortReg) {
|
||||||
return $"{shortReg.Value}{(isUsedBitwise ? $" [{shortReg.GetBitwise().ToBitString()}]" : "")}";
|
return $"{shortReg.Value}{(isUsedBitwise ? $" [{shortReg.GetBitwise().ToBitString()}]" : "")}";
|
||||||
}
|
}
|
||||||
@@ -133,7 +146,6 @@ namespace MewtocolNet.Registers {
|
|||||||
}
|
}
|
||||||
if (this is SRegister stringReg) {
|
if (this is SRegister stringReg) {
|
||||||
return stringReg.Value.ToString();
|
return stringReg.Value.ToString();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return "Type of the register is not supported.";
|
return "Type of the register is not supported.";
|
||||||
@@ -212,6 +224,10 @@ namespace MewtocolNet.Registers {
|
|||||||
|
|
||||||
internal string GetRegisterPLCName () {
|
internal string GetRegisterPLCName () {
|
||||||
|
|
||||||
|
if (this is BRegister bReg && bReg.SpecialAddress != SpecialAddress.None) {
|
||||||
|
return $"{GetRegisterString()}{bReg.SpecialAddress}";
|
||||||
|
}
|
||||||
|
|
||||||
return $"{GetRegisterString()}{MemoryAdress}";
|
return $"{GetRegisterString()}{MemoryAdress}";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<PackageId>MewtocolNet</PackageId>
|
<PackageId>MewtocolNet</PackageId>
|
||||||
<Version>0.3.5</Version>
|
<Version>0.4.0</Version>
|
||||||
<Authors>Felix Weiss</Authors>
|
<Authors>Felix Weiss</Authors>
|
||||||
<Company>Womed</Company>
|
<Company>Womed</Company>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
|
|||||||
Reference in New Issue
Block a user