mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 11:11:23 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cdae9a60fb | ||
|
|
b1c2cdb70e | ||
|
|
95bfcf94de | ||
|
|
0a93df287d | ||
|
|
18384ff964 | ||
|
|
e4ddad685a | ||
|
|
e953938a65 |
@@ -30,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 () => {
|
||||||
|
|||||||
@@ -10,6 +10,9 @@ namespace Examples {
|
|||||||
[Register(1000, RegisterType.R)]
|
[Register(1000, RegisterType.R)]
|
||||||
public bool TestBool1 { get; private set; }
|
public bool TestBool1 { get; private set; }
|
||||||
|
|
||||||
|
[Register(1000)]
|
||||||
|
public int TestDuplicate { get; private set; }
|
||||||
|
|
||||||
//corresponds to a XD input of the PLC
|
//corresponds to a XD input of the PLC
|
||||||
[Register(RegisterType.X, SpecialAddress.D)]
|
[Register(RegisterType.X, SpecialAddress.D)]
|
||||||
public bool TestBoolInputXD { get; private set; }
|
public bool TestBoolInputXD { get; private set; }
|
||||||
@@ -50,5 +53,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; }
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,9 +76,11 @@ namespace MewtocolNet {
|
|||||||
while (ContinousReaderRunning) {
|
while (ContinousReaderRunning) {
|
||||||
|
|
||||||
//do priority tasks first
|
//do priority tasks first
|
||||||
if (PriorityTasks.Count > 0) {
|
if (PriorityTasks != null && PriorityTasks.Count > 0) {
|
||||||
|
|
||||||
await PriorityTasks.FirstOrDefault(x => !x.IsCompleted);
|
try {
|
||||||
|
await PriorityTasks?.FirstOrDefault(x => !x.IsCompleted);
|
||||||
|
} catch (NullReferenceException) { }
|
||||||
|
|
||||||
} else if (getPLCinfoCycleCount > 25) {
|
} else if (getPLCinfoCycleCount > 25) {
|
||||||
|
|
||||||
@@ -87,9 +89,7 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var registerPair in Registers) {
|
foreach (var reg in Registers) {
|
||||||
|
|
||||||
var reg = registerPair.Value;
|
|
||||||
|
|
||||||
if (reg is NRegister<short> shortReg) {
|
if (reg is NRegister<short> shortReg) {
|
||||||
var lastVal = shortReg.Value;
|
var lastVal = shortReg.Value;
|
||||||
@@ -208,7 +208,7 @@ namespace MewtocolNet {
|
|||||||
toAdd = new BRegister(_address, _type, _name);
|
toAdd = new BRegister(_address, _type, _name);
|
||||||
}
|
}
|
||||||
|
|
||||||
Registers.Add(_address, toAdd);
|
Registers.Add(toAdd);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,7 +238,7 @@ namespace MewtocolNet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
toAdd.collectionType = _colType;
|
toAdd.collectionType = _colType;
|
||||||
Registers.Add(_address, toAdd);
|
Registers.Add(toAdd);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,7 +259,7 @@ namespace MewtocolNet {
|
|||||||
public void AddRegister (SpecialAddress _spAddress, RegisterType _type, string _name = null) {
|
public void AddRegister (SpecialAddress _spAddress, RegisterType _type, string _name = null) {
|
||||||
|
|
||||||
//as bool registers
|
//as bool registers
|
||||||
Registers.Add((int)_spAddress, new BRegister(_spAddress, _type, _name));
|
Registers.Add(new BRegister(_spAddress, _type, _name));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,7 +270,7 @@ namespace MewtocolNet {
|
|||||||
reg.collectionType = _colType;
|
reg.collectionType = _colType;
|
||||||
|
|
||||||
//as bool registers
|
//as bool registers
|
||||||
Registers.Add((int)_spAddress, reg);
|
Registers.Add(reg);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -299,35 +299,38 @@ namespace MewtocolNet {
|
|||||||
throw new NotSupportedException($"_lenght parameter only allowed for register of type string");
|
throw new NotSupportedException($"_lenght parameter only allowed for register of type string");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Registers.Any(x => x.Key == _address)) {
|
Register toAdd;
|
||||||
throw new NotSupportedException($"Cannot add a register multiple times, " +
|
|
||||||
$"make sure that all register attributes or AddRegister assignments have different adresses.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (regType == typeof(short)) {
|
if (regType == typeof(short)) {
|
||||||
Registers.Add(_address, new NRegister<short>(_address, _name));
|
toAdd = new NRegister<short>(_address, _name);
|
||||||
} else if (regType == typeof(ushort)) {
|
} else if (regType == typeof(ushort)) {
|
||||||
Registers.Add(_address, new NRegister<ushort>(_address, _name));
|
toAdd = new NRegister<ushort>(_address, _name);
|
||||||
} else if (regType == typeof(int)) {
|
} else if (regType == typeof(int)) {
|
||||||
Registers.Add(_address, new NRegister<int>(_address, _name));
|
toAdd = new NRegister<int>(_address, _name);
|
||||||
} else if (regType == typeof(uint)) {
|
} else if (regType == typeof(uint)) {
|
||||||
Registers.Add(_address, new NRegister<uint>(_address, _name));
|
toAdd = new NRegister<uint>(_address, _name);
|
||||||
} else if (regType == typeof(float)) {
|
} else if (regType == typeof(float)) {
|
||||||
Registers.Add(_address, new NRegister<float>(_address, _name));
|
toAdd = new NRegister<float>(_address, _name);
|
||||||
} else if (regType == typeof(string)) {
|
} else if (regType == typeof(string)) {
|
||||||
Registers.Add(_address, new SRegister(_address, _length, _name));
|
toAdd = new SRegister(_address, _length, _name);
|
||||||
} else if (regType == typeof(TimeSpan)) {
|
} else if (regType == typeof(TimeSpan)) {
|
||||||
Registers.Add(_address, new NRegister<TimeSpan>(_address, _name));
|
toAdd = new NRegister<TimeSpan>(_address, _name);
|
||||||
} else if (regType == typeof(bool)) {
|
} else if (regType == typeof(bool)) {
|
||||||
Registers.Add(_address, new BRegister(_address, RegisterType.R, _name));
|
toAdd = new BRegister(_address, RegisterType.R, _name);
|
||||||
} else {
|
} else {
|
||||||
throw new NotSupportedException($"The type {regType} is not allowed for Registers \n" +
|
throw new NotSupportedException($"The type {regType} is not allowed for Registers \n" +
|
||||||
$"Allowed are: short, ushort, int, uint, float and string");
|
$"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<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);
|
||||||
|
|
||||||
@@ -335,12 +338,7 @@ namespace MewtocolNet {
|
|||||||
throw new NotSupportedException($"_lenght parameter only allowed for register of type string");
|
throw new NotSupportedException($"_lenght parameter only allowed for register of type string");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Registers.Any(x => x.Key == _address) && !_isBitwise) {
|
if (Registers.Any(x => x.MemoryAdress == _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) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -351,7 +349,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)) {
|
||||||
@@ -364,15 +362,19 @@ namespace MewtocolNet {
|
|||||||
reg = new BRegister(_address, RegisterType.R, _name);
|
reg = new BRegister(_address, RegisterType.R, _name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (reg == null) {
|
if (reg == null) {
|
||||||
throw new NotSupportedException($"The type {regType} is not allowed for Registers \n" +
|
throw new NotSupportedException($"The type {regType} is not allowed for Registers \n" +
|
||||||
$"Allowed are: short, ushort, int, uint, float and string");
|
$"Allowed are: short, ushort, int, uint, float and string");
|
||||||
} else {
|
} 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -387,7 +389,7 @@ namespace MewtocolNet {
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public Register GetRegister (string name) {
|
public Register GetRegister (string name) {
|
||||||
|
|
||||||
return Registers.FirstOrDefault(x => x.Value.Name == name).Value;
|
return Registers.FirstOrDefault(x => x.Name == name);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -398,8 +400,8 @@ namespace MewtocolNet {
|
|||||||
/// <returns>A casted register or the <code>default</code> value</returns>
|
/// <returns>A casted register or the <code>default</code> value</returns>
|
||||||
public T GetRegister<T> (string name) where T : Register {
|
public T GetRegister<T> (string name) where T : Register {
|
||||||
try {
|
try {
|
||||||
var reg = Registers.FirstOrDefault(x => x.Value.Name == name);
|
var reg = Registers.FirstOrDefault(x => x.Name == name);
|
||||||
return reg.Value as T;
|
return reg as T;
|
||||||
} catch (InvalidCastException) {
|
} catch (InvalidCastException) {
|
||||||
return default(T);
|
return default(T);
|
||||||
}
|
}
|
||||||
@@ -414,7 +416,7 @@ namespace MewtocolNet {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public List<Register> GetAllRegisters () {
|
public List<Register> GetAllRegisters () {
|
||||||
|
|
||||||
return Registers.Values.ToList();
|
return Registers;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ namespace MewtocolNet {
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The registered data registers of the PLC
|
/// The registered data registers of the PLC
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Dictionary<int, Register> Registers { get; set; } = new Dictionary<int, Register>();
|
public List<Register> Registers { get; set; } = new List<Register>();
|
||||||
|
|
||||||
private string ip;
|
private string ip;
|
||||||
private int port;
|
private int port;
|
||||||
@@ -101,6 +101,8 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
internal List<Task> PriorityTasks { get; set; } = new List<Task>();
|
internal List<Task> PriorityTasks { get; set; } = new List<Task>();
|
||||||
|
|
||||||
|
internal int SendExceptionsInRow = 0;
|
||||||
|
|
||||||
#region Initialization
|
#region Initialization
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -193,6 +195,20 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Closes all permanent polling
|
||||||
|
/// </summary>
|
||||||
|
public void Disconnect () {
|
||||||
|
|
||||||
|
if (!IsConnected)
|
||||||
|
return;
|
||||||
|
|
||||||
|
OnMajorSocketException();
|
||||||
|
|
||||||
|
PriorityTasks.Clear();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Attaches a poller to the interface that continously
|
/// Attaches a poller to the interface that continously
|
||||||
/// polls the registered data registers and writes the values to them
|
/// polls the registered data registers and writes the values to them
|
||||||
@@ -264,6 +280,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)) {
|
||||||
|
|
||||||
@@ -376,6 +396,14 @@ 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (foundToUpdate.PropertyType == typeof(TimeSpan)) {
|
||||||
|
foundToUpdate.SetValue(collection, ((NRegister<TimeSpan>)reg).Value);
|
||||||
|
}
|
||||||
|
|
||||||
//setting back strings
|
//setting back strings
|
||||||
|
|
||||||
if (foundToUpdate.PropertyType == typeof(string)) {
|
if (foundToUpdate.PropertyType == typeof(string)) {
|
||||||
@@ -518,7 +546,6 @@ namespace MewtocolNet {
|
|||||||
/// Calculates checksum and sends a command to the PLC then awaits results
|
/// Calculates checksum and sends a command to the PLC then awaits results
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="_msg">MEWTOCOL Formatted request string ex: %01#RT</param>
|
/// <param name="_msg">MEWTOCOL Formatted request string ex: %01#RT</param>
|
||||||
/// <param name="_close">Auto close of frame [true]%01#RT01\r [false]%01#RT</param>
|
|
||||||
/// <returns>Returns the result</returns>
|
/// <returns>Returns the result</returns>
|
||||||
public async Task<CommandResult> SendCommandAsync (string _msg) {
|
public async Task<CommandResult> SendCommandAsync (string _msg) {
|
||||||
|
|
||||||
@@ -535,7 +562,7 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
var awaittask = SendSingleBlock(_msg);
|
var awaittask = SendSingleBlock(_msg);
|
||||||
PriorityTasks.Add(awaittask);
|
PriorityTasks.Add(awaittask);
|
||||||
awaittask.Wait();
|
await awaittask;
|
||||||
|
|
||||||
PriorityTasks.Remove(awaittask);
|
PriorityTasks.Remove(awaittask);
|
||||||
response = awaittask.Result;
|
response = awaittask.Result;
|
||||||
@@ -592,43 +619,57 @@ namespace MewtocolNet {
|
|||||||
await client.ConnectAsync(ip, port);
|
await client.ConnectAsync(ip, port);
|
||||||
|
|
||||||
using (NetworkStream stream = client.GetStream()) {
|
using (NetworkStream stream = client.GetStream()) {
|
||||||
|
|
||||||
var message = _blockString.ToHexASCIIBytes();
|
var message = _blockString.ToHexASCIIBytes();
|
||||||
var messageAscii = BitConverter.ToString(message).Replace("-", " ");
|
var messageAscii = BitConverter.ToString(message).Replace("-", " ");
|
||||||
|
|
||||||
//send request
|
//send request
|
||||||
using (var sendStream = new MemoryStream(message)) {
|
try {
|
||||||
await sendStream.CopyToAsync(stream);
|
using (var sendStream = new MemoryStream(message)) {
|
||||||
Logger.Log($"OUT MSG: {_blockString}", LogLevel.Critical, this);
|
await sendStream.CopyToAsync(stream);
|
||||||
//log message sent
|
Logger.Log($"OUT MSG: {_blockString}", LogLevel.Critical, this);
|
||||||
ASCIIEncoding enc = new ASCIIEncoding();
|
//log message sent
|
||||||
string characters = enc.GetString(message);
|
ASCIIEncoding enc = new ASCIIEncoding();
|
||||||
|
string characters = enc.GetString(message);
|
||||||
|
}
|
||||||
|
} catch (IOException) {
|
||||||
|
Logger.Log($"Critical IO exception on send", LogLevel.Critical, this);
|
||||||
|
return null;
|
||||||
|
} catch (SocketException) {
|
||||||
|
OnMajorSocketException();
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
//await result
|
//await result
|
||||||
StringBuilder response = new StringBuilder();
|
StringBuilder response = new StringBuilder();
|
||||||
byte[] responseBuffer = new byte[256];
|
try {
|
||||||
do {
|
byte[] responseBuffer = new byte[256];
|
||||||
int bytes = stream.Read(responseBuffer, 0, responseBuffer.Length);
|
do {
|
||||||
response.Append(Encoding.UTF8.GetString(responseBuffer, 0, bytes));
|
int bytes = stream.Read(responseBuffer, 0, responseBuffer.Length);
|
||||||
|
response.Append(Encoding.UTF8.GetString(responseBuffer, 0, bytes));
|
||||||
|
}
|
||||||
|
while (stream.DataAvailable);
|
||||||
|
} catch (IOException) {
|
||||||
|
Logger.Log($"Critical IO exception on receive", LogLevel.Critical, this);
|
||||||
|
return null;
|
||||||
|
} catch (SocketException) {
|
||||||
|
OnMajorSocketException();
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
while (stream.DataAvailable);
|
|
||||||
sw.Stop();
|
sw.Stop();
|
||||||
var curCycle = (int)sw.ElapsedMilliseconds;
|
var curCycle = (int)sw.ElapsedMilliseconds;
|
||||||
if (Math.Abs(CycleTimeMs - curCycle) > 2) {
|
if (Math.Abs(CycleTimeMs - curCycle) > 2) {
|
||||||
CycleTimeMs = curCycle;
|
CycleTimeMs = curCycle;
|
||||||
}
|
}
|
||||||
Logger.Log($"IN MSG ({(int)sw.Elapsed.TotalMilliseconds}ms): {_blockString}", LogLevel.Critical, this);
|
|
||||||
|
Logger.Log($"IN MSG ({(int)sw.Elapsed.TotalMilliseconds}ms): {response}", LogLevel.Critical, this);
|
||||||
return response.ToString();
|
return response.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception) {
|
} catch (SocketException) {
|
||||||
|
|
||||||
if (IsConnected) {
|
OnMajorSocketException();
|
||||||
CycleTimeMs = 0;
|
|
||||||
IsConnected = false;
|
|
||||||
Disconnected?.Invoke();
|
|
||||||
}
|
|
||||||
|
|
||||||
KillPoller();
|
|
||||||
Logger.Log("The PLC connection was closed", LogLevel.Error, this);
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -637,6 +678,19 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnMajorSocketException () {
|
||||||
|
|
||||||
|
if (IsConnected) {
|
||||||
|
|
||||||
|
Logger.Log("The PLC connection was closed", LogLevel.Error, this);
|
||||||
|
CycleTimeMs = 0;
|
||||||
|
IsConnected = false;
|
||||||
|
Disconnected?.Invoke();
|
||||||
|
KillPoller();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
#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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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.";
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<PackageId>MewtocolNet</PackageId>
|
<PackageId>MewtocolNet</PackageId>
|
||||||
<Version>0.3.6</Version>
|
<Version>0.4.3</Version>
|
||||||
<Authors>Felix Weiss</Authors>
|
<Authors>Felix Weiss</Authors>
|
||||||
<Company>Womed</Company>
|
<Company>Womed</Company>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ Where is the RS232/Serial support?
|
|||||||
|
|
||||||
Install this package by using [Nuget](https://www.nuget.org/packages/MewtocolNet/) or reference
|
Install this package by using [Nuget](https://www.nuget.org/packages/MewtocolNet/) or reference
|
||||||
```XML
|
```XML
|
||||||
<PackageReference Include="MewtocolNet" Version="0.3.0" />
|
<PackageReference Include="MewtocolNet" Version="0.4.2" />
|
||||||
```
|
```
|
||||||
in your dependencies.
|
in your dependencies.
|
||||||
Alternatively use the dotnet CLI and run
|
Alternatively use the dotnet CLI and run
|
||||||
|
|||||||
Reference in New Issue
Block a user