mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 11:11:23 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cdae9a60fb | ||
|
|
b1c2cdb70e | ||
|
|
95bfcf94de | ||
|
|
0a93df287d | ||
|
|
18384ff964 | ||
|
|
e4ddad685a | ||
|
|
e953938a65 | ||
|
|
83f17a4eae | ||
|
|
4c719843f2 | ||
|
|
8f9e66d5d3 |
@@ -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,6 +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}");
|
||||||
|
|
||||||
//writing a value to the registers
|
//writing a value to the registers
|
||||||
Task.Factory.StartNew(async () => {
|
Task.Factory.StartNew(async () => {
|
||||||
@@ -38,9 +40,6 @@ namespace Examples {
|
|||||||
|
|
||||||
await Task.Delay(2000);
|
await Task.Delay(2000);
|
||||||
|
|
||||||
//inverts the boolean register
|
|
||||||
await interf.SetRegisterAsync(nameof(registers.TestBool1), !registers.TestBool1);
|
|
||||||
|
|
||||||
Console.WriteLine("Testregister was toggled");
|
Console.WriteLine("Testregister was toggled");
|
||||||
|
|
||||||
//adds 10 each time the plc connects to the PLCs INT regíster
|
//adds 10 each time the plc connects to the PLCs INT regíster
|
||||||
|
|||||||
@@ -7,9 +7,12 @@ namespace Examples {
|
|||||||
public class TestRegisters : RegisterCollectionBase {
|
public class TestRegisters : RegisterCollectionBase {
|
||||||
|
|
||||||
//corresponds to a R100 boolean register in the PLC
|
//corresponds to a R100 boolean register in the PLC
|
||||||
[Register(100, 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; }
|
||||||
@@ -42,10 +45,29 @@ namespace Examples {
|
|||||||
[Register(1204, 9, BitCount.B16)]
|
[Register(1204, 9, BitCount.B16)]
|
||||||
public bool BitValue { get; private set; }
|
public bool BitValue { get; private set; }
|
||||||
|
|
||||||
|
[Register(1204, 5, BitCount.B16)]
|
||||||
|
public bool FillTest { get; private set; }
|
||||||
|
|
||||||
//corresponds to a DT7012 - DT7013 as a 32bit time value that gets parsed as a timespan (TIME)
|
//corresponds to a DT7012 - DT7013 as a 32bit time value that gets parsed as a timespan (TIME)
|
||||||
//the smallest value to communicate to the PLC is 10ms
|
//the smallest value to communicate to the PLC is 10ms
|
||||||
[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,71 +89,55 @@ 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;
|
||||||
var readout = (await ReadNumRegister(shortReg)).Register.Value;
|
var readout = (await ReadNumRegister(shortReg)).Register.Value;
|
||||||
if (lastVal != readout) {
|
if (lastVal != readout) {
|
||||||
shortReg.LastValue = readout;
|
|
||||||
InvokeRegisterChanged(shortReg);
|
InvokeRegisterChanged(shortReg);
|
||||||
shortReg.TriggerNotifyChange();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (reg is NRegister<ushort> ushortReg) {
|
if (reg is NRegister<ushort> ushortReg) {
|
||||||
var lastVal = ushortReg.Value;
|
var lastVal = ushortReg.Value;
|
||||||
var readout = (await ReadNumRegister(ushortReg)).Register.Value;
|
var readout = (await ReadNumRegister(ushortReg)).Register.Value;
|
||||||
if (lastVal != readout) {
|
if (lastVal != readout) {
|
||||||
ushortReg.LastValue = readout;
|
|
||||||
InvokeRegisterChanged(ushortReg);
|
InvokeRegisterChanged(ushortReg);
|
||||||
ushortReg.TriggerNotifyChange();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (reg is NRegister<int> intReg) {
|
if (reg is NRegister<int> intReg) {
|
||||||
var lastVal = intReg.Value;
|
var lastVal = intReg.Value;
|
||||||
var readout = (await ReadNumRegister(intReg)).Register.Value;
|
var readout = (await ReadNumRegister(intReg)).Register.Value;
|
||||||
if (lastVal != readout) {
|
if (lastVal != readout) {
|
||||||
intReg.LastValue = readout;
|
|
||||||
InvokeRegisterChanged(intReg);
|
InvokeRegisterChanged(intReg);
|
||||||
intReg.TriggerNotifyChange();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (reg is NRegister<uint> uintReg) {
|
if (reg is NRegister<uint> uintReg) {
|
||||||
var lastVal = uintReg.Value;
|
var lastVal = uintReg.Value;
|
||||||
var readout = (await ReadNumRegister(uintReg)).Register.Value;
|
var readout = (await ReadNumRegister(uintReg)).Register.Value;
|
||||||
if (lastVal != readout) {
|
if (lastVal != readout) {
|
||||||
uintReg.LastValue = readout;
|
|
||||||
InvokeRegisterChanged(uintReg);
|
InvokeRegisterChanged(uintReg);
|
||||||
uintReg.TriggerNotifyChange();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (reg is NRegister<float> floatReg) {
|
if (reg is NRegister<float> floatReg) {
|
||||||
var lastVal = floatReg.Value;
|
var lastVal = floatReg.Value;
|
||||||
var readout = (await ReadNumRegister(floatReg)).Register.Value;
|
var readout = (await ReadNumRegister(floatReg)).Register.Value;
|
||||||
if (lastVal != readout) {
|
if (lastVal != readout) {
|
||||||
floatReg.LastValue = readout;
|
|
||||||
InvokeRegisterChanged(floatReg);
|
InvokeRegisterChanged(floatReg);
|
||||||
floatReg.TriggerNotifyChange();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (reg is NRegister<TimeSpan> tsReg) {
|
if (reg is NRegister<TimeSpan> tsReg) {
|
||||||
var lastVal = tsReg.Value;
|
var lastVal = tsReg.Value;
|
||||||
var readout = (await ReadNumRegister(tsReg)).Register.Value;
|
var readout = (await ReadNumRegister(tsReg)).Register.Value;
|
||||||
if (lastVal != readout) {
|
if (lastVal != readout) {
|
||||||
tsReg.LastValue = readout;
|
|
||||||
InvokeRegisterChanged(tsReg);
|
InvokeRegisterChanged(tsReg);
|
||||||
tsReg.TriggerNotifyChange();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (reg is BRegister boolReg) {
|
if (reg is BRegister boolReg) {
|
||||||
var lastVal = boolReg.Value;
|
var lastVal = boolReg.Value;
|
||||||
var readout = (await ReadBoolRegister(boolReg)).Register.Value;
|
var readout = (await ReadBoolRegister(boolReg)).Register.Value;
|
||||||
if (lastVal != readout) {
|
if (lastVal != readout) {
|
||||||
boolReg.LastValue = readout;
|
|
||||||
InvokeRegisterChanged(boolReg);
|
InvokeRegisterChanged(boolReg);
|
||||||
boolReg.TriggerNotifyChange();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (reg is SRegister stringReg) {
|
if (reg is SRegister stringReg) {
|
||||||
@@ -159,7 +145,6 @@ namespace MewtocolNet {
|
|||||||
var readout = (await ReadStringRegister(stringReg)).Register.Value;
|
var readout = (await ReadStringRegister(stringReg)).Register.Value;
|
||||||
if (lastVal != readout) {
|
if (lastVal != readout) {
|
||||||
InvokeRegisterChanged(stringReg);
|
InvokeRegisterChanged(stringReg);
|
||||||
stringReg.TriggerNotifyChange();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -200,30 +185,60 @@ namespace MewtocolNet {
|
|||||||
/// <param name="_name">A naming definition for QOL, doesn't effect PLC and is optional</param>
|
/// <param name="_name">A naming definition for QOL, doesn't effect PLC and is optional</param>
|
||||||
public void AddRegister (int _address, RegisterType _type, string _name = null) {
|
public void AddRegister (int _address, RegisterType _type, string _name = null) {
|
||||||
|
|
||||||
|
Register toAdd = null;
|
||||||
|
|
||||||
//as number registers
|
//as number registers
|
||||||
if (_type == RegisterType.DT_short) {
|
if (_type == RegisterType.DT_short) {
|
||||||
Registers.Add(_address, new NRegister<short>(_address, _name));
|
toAdd = new NRegister<short>(_address, _name);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (_type == RegisterType.DT_ushort) {
|
if (_type == RegisterType.DT_ushort) {
|
||||||
Registers.Add(_address, new NRegister<ushort>(_address, _name));
|
toAdd = new NRegister<ushort>(_address, _name);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (_type == RegisterType.DDT_int) {
|
if (_type == RegisterType.DDT_int) {
|
||||||
Registers.Add(_address, new NRegister<int>(_address, _name));
|
toAdd = new NRegister<int>(_address, _name);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (_type == RegisterType.DDT_uint) {
|
if (_type == RegisterType.DDT_uint) {
|
||||||
Registers.Add(_address, new NRegister<uint>(_address, _name));
|
toAdd = new NRegister<uint>(_address, _name);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (_type == RegisterType.DDT_float) {
|
if (_type == RegisterType.DDT_float) {
|
||||||
Registers.Add(_address, new NRegister<float>(_address, _name));
|
toAdd = new NRegister<float>(_address, _name);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//as bool registers
|
if(toAdd == null) {
|
||||||
Registers.Add(_address, new BRegister(_address, _type, _name));
|
toAdd = new BRegister(_address, _type, _name);
|
||||||
|
}
|
||||||
|
|
||||||
|
Registers.Add(toAdd);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void AddRegister (Type _colType, int _address, RegisterType _type, string _name = null) {
|
||||||
|
|
||||||
|
Register toAdd = null;
|
||||||
|
|
||||||
|
//as number registers
|
||||||
|
if (_type == RegisterType.DT_short) {
|
||||||
|
toAdd = new NRegister<short>(_address, _name);
|
||||||
|
}
|
||||||
|
if (_type == RegisterType.DT_ushort) {
|
||||||
|
toAdd = new NRegister<ushort>(_address, _name);
|
||||||
|
}
|
||||||
|
if (_type == RegisterType.DDT_int) {
|
||||||
|
toAdd = new NRegister<int>(_address, _name);
|
||||||
|
}
|
||||||
|
if (_type == RegisterType.DDT_uint) {
|
||||||
|
toAdd = new NRegister<uint>(_address, _name);
|
||||||
|
}
|
||||||
|
if (_type == RegisterType.DDT_float) {
|
||||||
|
toAdd = new NRegister<float>(_address, _name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (toAdd == null) {
|
||||||
|
toAdd = new BRegister(_address, _type, _name);
|
||||||
|
}
|
||||||
|
|
||||||
|
toAdd.collectionType = _colType;
|
||||||
|
Registers.Add(toAdd);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,7 +259,18 @@ 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));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void AddRegister (Type _colType, SpecialAddress _spAddress, RegisterType _type, string _name = null) {
|
||||||
|
|
||||||
|
var reg = new BRegister(_spAddress, _type, _name);
|
||||||
|
|
||||||
|
reg.collectionType = _colType;
|
||||||
|
|
||||||
|
//as bool registers
|
||||||
|
Registers.Add(reg);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -265,7 +291,7 @@ namespace MewtocolNet {
|
|||||||
/// <param name="_name">A naming definition for QOL, doesn't effect PLC and is optional</param>
|
/// <param name="_name">A naming definition for QOL, doesn't effect PLC and is optional</param>
|
||||||
/// <param name="_address">The address of the register in the PLCs memory</param>
|
/// <param name="_address">The address of the register in the PLCs memory</param>
|
||||||
/// <param name="_length">The length of the string (Can be ignored for other types)</param>
|
/// <param name="_length">The length of the string (Can be ignored for other types)</param>
|
||||||
public void AddRegister<T>(int _address, int _length = 1, string _name = null, bool _isBitwise = false) {
|
public void AddRegister<T>(int _address, int _length = 1, string _name = null) {
|
||||||
|
|
||||||
Type regType = typeof(T);
|
Type regType = typeof(T);
|
||||||
|
|
||||||
@@ -273,34 +299,84 @@ 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, _isBitwise));
|
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, _isBitwise));
|
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, Type _enumType = null) {
|
||||||
|
|
||||||
|
Type regType = typeof(T);
|
||||||
|
|
||||||
|
if (regType != typeof(string) && _length != 1) {
|
||||||
|
throw new NotSupportedException($"_lenght parameter only allowed for register of type string");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Registers.Any(x => x.MemoryAdress == _address) && _isBitwise) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Register reg = null;
|
||||||
|
|
||||||
|
if (regType == typeof(short)) {
|
||||||
|
reg = new NRegister<short>(_address, _name, _isBitwise);
|
||||||
|
} else if (regType == typeof(ushort)) {
|
||||||
|
reg = new NRegister<ushort>(_address, _name);
|
||||||
|
} else if (regType == typeof(int)) {
|
||||||
|
reg = new NRegister<int>(_address, _name, _isBitwise, _enumType);
|
||||||
|
} else if (regType == typeof(uint)) {
|
||||||
|
reg = new NRegister<uint>(_address, _name);
|
||||||
|
} else if (regType == typeof(float)) {
|
||||||
|
reg = new NRegister<float>(_address, _name);
|
||||||
|
} else if (regType == typeof(string)) {
|
||||||
|
reg = new SRegister(_address, _length, _name);
|
||||||
|
} else if (regType == typeof(TimeSpan)) {
|
||||||
|
reg = new NRegister<TimeSpan>(_address, _name);
|
||||||
|
} else if (regType == typeof(bool)) {
|
||||||
|
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 {
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@@ -313,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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -324,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);
|
||||||
}
|
}
|
||||||
@@ -340,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;
|
||||||
@@ -92,7 +92,7 @@ namespace MewtocolNet {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public int CycleTimeMs {
|
public int CycleTimeMs {
|
||||||
get { return cycleTimeMs; }
|
get { return cycleTimeMs; }
|
||||||
set {
|
private set {
|
||||||
cycleTimeMs = value;
|
cycleTimeMs = value;
|
||||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CycleTimeMs)));
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CycleTimeMs)));
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
@@ -234,43 +250,47 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
if (prop.PropertyType == typeof(bool) && cAttribute.AssignedBitIndex == -1) {
|
if (prop.PropertyType == typeof(bool) && cAttribute.AssignedBitIndex == -1) {
|
||||||
if (cAttribute.SpecialAddress == SpecialAddress.None) {
|
if (cAttribute.SpecialAddress == SpecialAddress.None) {
|
||||||
AddRegister(cAttribute.MemoryArea, cAttribute.RegisterType, _name: propName);
|
AddRegister(collection.GetType(), cAttribute.MemoryArea, cAttribute.RegisterType, _name: propName);
|
||||||
} else {
|
} else {
|
||||||
AddRegister(cAttribute.SpecialAddress, cAttribute.RegisterType, _name: propName);
|
AddRegister(collection.GetType(), cAttribute.SpecialAddress, cAttribute.RegisterType, _name: propName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prop.PropertyType == typeof(short)) {
|
if (prop.PropertyType == typeof(short)) {
|
||||||
AddRegister<short>(cAttribute.MemoryArea, _name: propName);
|
AddRegister<short>(collection.GetType(), cAttribute.MemoryArea, _name: propName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prop.PropertyType == typeof(ushort)) {
|
if (prop.PropertyType == typeof(ushort)) {
|
||||||
AddRegister<ushort>(cAttribute.MemoryArea, _name: propName);
|
AddRegister<ushort>(collection.GetType(), cAttribute.MemoryArea, _name: propName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prop.PropertyType == typeof(int)) {
|
if (prop.PropertyType == typeof(int)) {
|
||||||
AddRegister<int>(cAttribute.MemoryArea, _name: propName);
|
AddRegister<int>(collection.GetType(), cAttribute.MemoryArea, _name: propName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prop.PropertyType == typeof(uint)) {
|
if (prop.PropertyType == typeof(uint)) {
|
||||||
AddRegister<uint>(cAttribute.MemoryArea, _name: propName);
|
AddRegister<uint>(collection.GetType(), cAttribute.MemoryArea, _name: propName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prop.PropertyType == typeof(float)) {
|
if (prop.PropertyType == typeof(float)) {
|
||||||
AddRegister<float>(cAttribute.MemoryArea, _name: propName);
|
AddRegister<float>(collection.GetType(), cAttribute.MemoryArea, _name: propName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prop.PropertyType == typeof(string)) {
|
if (prop.PropertyType == typeof(string)) {
|
||||||
AddRegister<string>(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)) {
|
||||||
|
|
||||||
if (cAttribute.BitCount == BitCount.B16) {
|
if (cAttribute.BitCount == BitCount.B16) {
|
||||||
AddRegister<short>(cAttribute.MemoryArea, _name: propName, _isBitwise: true);
|
AddRegister<short>(collection.GetType(), cAttribute.MemoryArea, _name: propName, _isBitwise: true);
|
||||||
} else {
|
} else {
|
||||||
AddRegister<int>(cAttribute.MemoryArea, _name: propName, _isBitwise: true);
|
AddRegister<int>(collection.GetType(), cAttribute.MemoryArea, _name: propName, _isBitwise: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -278,16 +298,18 @@ 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) {
|
||||||
|
|
||||||
|
//var bitwiseCount = Registers.Count(x => x.Value.isUsedBitwise);
|
||||||
|
|
||||||
if (cAttribute.BitCount == BitCount.B16) {
|
if (cAttribute.BitCount == BitCount.B16) {
|
||||||
AddRegister<short>(cAttribute.MemoryArea, _name: propName, _isBitwise: true);
|
AddRegister<short>(collection.GetType(), cAttribute.MemoryArea, _name: $"Auto_Bitwise_DT{cAttribute.MemoryArea}", _isBitwise: true);
|
||||||
} else {
|
} else {
|
||||||
AddRegister<int>(cAttribute.MemoryArea, _name: propName, _isBitwise: true);
|
AddRegister<int>(collection.GetType(), cAttribute.MemoryArea, _name: $"Auto_Bitwise_DDT{cAttribute.MemoryArea}", _isBitwise: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prop.PropertyType == typeof(TimeSpan)) {
|
if (prop.PropertyType == typeof(TimeSpan)) {
|
||||||
AddRegister<TimeSpan>(cAttribute.MemoryArea, _name: propName);
|
AddRegister<TimeSpan>(collection.GetType(), cAttribute.MemoryArea, _name: propName);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -298,6 +320,40 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
RegisterChanged += (reg) => {
|
RegisterChanged += (reg) => {
|
||||||
|
|
||||||
|
//if the register is also used bitwise assign the boolean bit value to the according prop
|
||||||
|
if(reg.isUsedBitwise) {
|
||||||
|
|
||||||
|
for (int i = 0; i < props.Length; i++) {
|
||||||
|
|
||||||
|
var prop = props[i];
|
||||||
|
var bitWiseFound = prop.GetCustomAttributes(true)
|
||||||
|
.FirstOrDefault(y => y.GetType() == typeof(RegisterAttribute) && ((RegisterAttribute)y).MemoryArea == reg.MemoryAdress);
|
||||||
|
|
||||||
|
if(bitWiseFound != null && reg is NRegister<short> reg16) {
|
||||||
|
var casted = (RegisterAttribute)bitWiseFound;
|
||||||
|
var bitIndex = casted.AssignedBitIndex;
|
||||||
|
|
||||||
|
var bytes = BitConverter.GetBytes(reg16.Value);
|
||||||
|
BitArray bitAr = new BitArray(bytes);
|
||||||
|
prop.SetValue(collection, bitAr[bitIndex]);
|
||||||
|
collection.TriggerPropertyChanged(prop.Name);
|
||||||
|
|
||||||
|
} else if (bitWiseFound != null && reg is NRegister<int> reg32) {
|
||||||
|
var casted = (RegisterAttribute)bitWiseFound;
|
||||||
|
var bitIndex = casted.AssignedBitIndex;
|
||||||
|
|
||||||
|
var bytes = BitConverter.GetBytes(reg32.Value);
|
||||||
|
BitArray bitAr = new BitArray(bytes);
|
||||||
|
prop.SetValue(collection, bitAr[bitIndex]);
|
||||||
|
collection.TriggerPropertyChanged(prop.Name);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var foundToUpdate = props.FirstOrDefault(x => x.Name == reg.Name);
|
var foundToUpdate = props.FirstOrDefault(x => x.Name == reg.Name);
|
||||||
|
|
||||||
if (foundToUpdate != null) {
|
if (foundToUpdate != null) {
|
||||||
@@ -340,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)) {
|
||||||
@@ -349,27 +413,7 @@ namespace MewtocolNet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (foundToUpdate.PropertyType == typeof(bool) && registerAttr.AssignedBitIndex >= 0) {
|
if (foundToUpdate.PropertyType == typeof(BitArray)) {
|
||||||
|
|
||||||
//setting back bit registers to individual properties
|
|
||||||
if (reg is NRegister<short> shortReg) {
|
|
||||||
|
|
||||||
var bytes = BitConverter.GetBytes(shortReg.Value);
|
|
||||||
BitArray bitAr = new BitArray(bytes);
|
|
||||||
foundToUpdate.SetValue(collection, bitAr[registerAttr.AssignedBitIndex]);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (reg is NRegister<int> intReg) {
|
|
||||||
|
|
||||||
var bytes = BitConverter.GetBytes(intReg.Value);
|
|
||||||
BitArray bitAr = new BitArray(bytes);
|
|
||||||
foundToUpdate.SetValue(collection, bitAr[registerAttr.AssignedBitIndex]);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
} else if (foundToUpdate.PropertyType == typeof(BitArray)) {
|
|
||||||
|
|
||||||
//setting back bit registers
|
//setting back bit registers
|
||||||
if (reg is NRegister<short> shortReg) {
|
if (reg is NRegister<short> shortReg) {
|
||||||
@@ -502,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) {
|
||||||
|
|
||||||
@@ -519,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;
|
||||||
@@ -576,9 +619,12 @@ 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
|
||||||
|
try {
|
||||||
using (var sendStream = new MemoryStream(message)) {
|
using (var sendStream = new MemoryStream(message)) {
|
||||||
await sendStream.CopyToAsync(stream);
|
await sendStream.CopyToAsync(stream);
|
||||||
Logger.Log($"OUT MSG: {_blockString}", LogLevel.Critical, this);
|
Logger.Log($"OUT MSG: {_blockString}", LogLevel.Critical, this);
|
||||||
@@ -586,28 +632,44 @@ namespace MewtocolNet {
|
|||||||
ASCIIEncoding enc = new ASCIIEncoding();
|
ASCIIEncoding enc = new ASCIIEncoding();
|
||||||
string characters = enc.GetString(message);
|
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();
|
||||||
|
try {
|
||||||
byte[] responseBuffer = new byte[256];
|
byte[] responseBuffer = new byte[256];
|
||||||
do {
|
do {
|
||||||
int bytes = stream.Read(responseBuffer, 0, responseBuffer.Length);
|
int bytes = stream.Read(responseBuffer, 0, responseBuffer.Length);
|
||||||
response.Append(Encoding.UTF8.GetString(responseBuffer, 0, bytes));
|
response.Append(Encoding.UTF8.GetString(responseBuffer, 0, bytes));
|
||||||
}
|
}
|
||||||
while (stream.DataAvailable);
|
while (stream.DataAvailable);
|
||||||
|
} catch (IOException) {
|
||||||
|
Logger.Log($"Critical IO exception on receive", LogLevel.Critical, this);
|
||||||
|
return null;
|
||||||
|
} catch (SocketException) {
|
||||||
|
OnMajorSocketException();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
sw.Stop();
|
sw.Stop();
|
||||||
Logger.Log($"IN MSG ({(int)sw.Elapsed.TotalMilliseconds}ms): {_blockString}", LogLevel.Critical, this);
|
var curCycle = (int)sw.ElapsedMilliseconds;
|
||||||
|
if (Math.Abs(CycleTimeMs - curCycle) > 2) {
|
||||||
|
CycleTimeMs = curCycle;
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
IsConnected = false;
|
|
||||||
Disconnected?.Invoke();
|
|
||||||
}
|
|
||||||
|
|
||||||
KillPoller();
|
|
||||||
Logger.Log("The PLC connection was closed", LogLevel.Error, this);
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -616,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
|
||||||
|
|
||||||
|
|||||||
@@ -160,7 +160,7 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
var resultBool = result.Response.ParseRCSingleBit();
|
var resultBool = result.Response.ParseRCSingleBit();
|
||||||
if(resultBool != null) {
|
if(resultBool != null) {
|
||||||
_toRead.LastValue = resultBool.Value;
|
_toRead.SetValueFromPLC(resultBool.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
var finalRes = new BRegisterResult {
|
var finalRes = new BRegisterResult {
|
||||||
@@ -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");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,25 +216,25 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
var resultBytes = result.Response.ParseDTByteString(4).ReverseByteOrder();
|
var resultBytes = result.Response.ParseDTByteString(4).ReverseByteOrder();
|
||||||
var val = short.Parse(resultBytes, NumberStyles.HexNumber);
|
var val = short.Parse(resultBytes, NumberStyles.HexNumber);
|
||||||
(_toRead as NRegister<short>).LastValue = val;
|
_toRead.SetValueFromPLC(val);
|
||||||
|
|
||||||
} else if (numType == typeof(ushort)) {
|
} else if (numType == typeof(ushort)) {
|
||||||
|
|
||||||
var resultBytes = result.Response.ParseDTByteString(4).ReverseByteOrder();
|
var resultBytes = result.Response.ParseDTByteString(4).ReverseByteOrder();
|
||||||
var val = ushort.Parse(resultBytes, NumberStyles.HexNumber);
|
var val = ushort.Parse(resultBytes, NumberStyles.HexNumber);
|
||||||
(_toRead as NRegister<ushort>).LastValue = val;
|
_toRead.SetValueFromPLC(val);
|
||||||
|
|
||||||
} else if (numType == typeof(int)) {
|
} else if (numType == typeof(int)) {
|
||||||
|
|
||||||
var resultBytes = result.Response.ParseDTByteString(8).ReverseByteOrder();
|
var resultBytes = result.Response.ParseDTByteString(8).ReverseByteOrder();
|
||||||
var val = int.Parse(resultBytes, NumberStyles.HexNumber);
|
var val = int.Parse(resultBytes, NumberStyles.HexNumber);
|
||||||
(_toRead as NRegister<int>).LastValue = val;
|
_toRead.SetValueFromPLC(val);
|
||||||
|
|
||||||
} else if (numType == typeof(uint)) {
|
} else if (numType == typeof(uint)) {
|
||||||
|
|
||||||
var resultBytes = result.Response.ParseDTByteString(8).ReverseByteOrder();
|
var resultBytes = result.Response.ParseDTByteString(8).ReverseByteOrder();
|
||||||
var val = uint.Parse(resultBytes, NumberStyles.HexNumber);
|
var val = uint.Parse(resultBytes, NumberStyles.HexNumber);
|
||||||
(_toRead as NRegister<uint>).LastValue = val;
|
_toRead.SetValueFromPLC(val);
|
||||||
|
|
||||||
} else if (numType == typeof(float)) {
|
} else if (numType == typeof(float)) {
|
||||||
|
|
||||||
@@ -245,7 +245,7 @@ namespace MewtocolNet {
|
|||||||
byte[] floatVals = BitConverter.GetBytes(val);
|
byte[] floatVals = BitConverter.GetBytes(val);
|
||||||
float finalFloat = BitConverter.ToSingle(floatVals, 0);
|
float finalFloat = BitConverter.ToSingle(floatVals, 0);
|
||||||
|
|
||||||
(_toRead as NRegister<float>).LastValue = finalFloat;
|
_toRead.SetValueFromPLC(finalFloat);
|
||||||
|
|
||||||
} else if (numType == typeof(TimeSpan)) {
|
} else if (numType == typeof(TimeSpan)) {
|
||||||
|
|
||||||
@@ -256,7 +256,7 @@ namespace MewtocolNet {
|
|||||||
var ts = TimeSpan.FromMilliseconds(valMillis);
|
var ts = TimeSpan.FromMilliseconds(valMillis);
|
||||||
|
|
||||||
//minmax writable / readable value is 10ms
|
//minmax writable / readable value is 10ms
|
||||||
(_toRead as NRegister<TimeSpan>).LastValue = ts;
|
_toRead.SetValueFromPLC(ts);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ namespace MewtocolNet.Registers {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class BRegister : Register {
|
public class BRegister : Register {
|
||||||
|
|
||||||
internal RegisterType RegType { get; set; }
|
internal RegisterType RegType { get; private set; }
|
||||||
internal SpecialAddress SpecialAddress { get; set; }
|
internal SpecialAddress SpecialAddress { get; private set; }
|
||||||
|
|
||||||
public bool NeedValue;
|
public bool NeedValue;
|
||||||
public bool LastValue;
|
public bool LastValue;
|
||||||
@@ -18,13 +18,7 @@ namespace MewtocolNet.Registers {
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The value of the register
|
/// The value of the register
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Value {
|
public bool Value => LastValue;
|
||||||
get => LastValue;
|
|
||||||
set {
|
|
||||||
NeedValue = value;
|
|
||||||
TriggerChangedEvnt(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defines a register containing a number
|
/// Defines a register containing a number
|
||||||
@@ -35,8 +29,8 @@ namespace MewtocolNet.Registers {
|
|||||||
public BRegister (int _address, RegisterType _type = RegisterType.R, string _name = null) {
|
public BRegister (int _address, RegisterType _type = RegisterType.R, string _name = null) {
|
||||||
|
|
||||||
if (_address > 99999) throw new NotSupportedException("Memory adresses cant be greater than 99999");
|
if (_address > 99999) throw new NotSupportedException("Memory adresses cant be greater than 99999");
|
||||||
MemoryAdress = _address;
|
memoryAdress = _address;
|
||||||
Name = _name;
|
name = _name;
|
||||||
|
|
||||||
RegType = _type;
|
RegType = _type;
|
||||||
|
|
||||||
@@ -54,7 +48,7 @@ namespace MewtocolNet.Registers {
|
|||||||
throw new NotSupportedException("Special adress cant be none");
|
throw new NotSupportedException("Special adress cant be none");
|
||||||
|
|
||||||
SpecialAddress = _address;
|
SpecialAddress = _address;
|
||||||
Name = _name;
|
name = _name;
|
||||||
|
|
||||||
RegType = _type;
|
RegType = _type;
|
||||||
|
|
||||||
@@ -74,6 +68,11 @@ namespace MewtocolNet.Registers {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void SetValueFromPLC (bool val) {
|
||||||
|
LastValue = val;
|
||||||
|
TriggerChangedEvnt(this);
|
||||||
|
TriggerNotifyChange();
|
||||||
|
}
|
||||||
public override string ToString() {
|
public override string ToString() {
|
||||||
return $"Adress: {MemoryAdress} Val: {Value}";
|
return $"Adress: {MemoryAdress} Val: {Value}";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,48 +13,75 @@ namespace MewtocolNet.Registers {
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The value of the register
|
/// The value of the register
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public T Value {
|
public T Value => LastValue;
|
||||||
get => LastValue;
|
|
||||||
set {
|
|
||||||
NeedValue = value;
|
|
||||||
TriggerChangedEvnt(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 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");
|
if (_adress > 99999)
|
||||||
MemoryAdress = _adress;
|
throw new NotSupportedException("Memory adresses cant be greater than 99999");
|
||||||
Name = _name;
|
memoryAdress = _adress;
|
||||||
|
name = _name;
|
||||||
Type numType = typeof(T);
|
Type numType = typeof(T);
|
||||||
if (numType == typeof(short)) {
|
if (numType == typeof(short)) {
|
||||||
MemoryLength = 0;
|
memoryLength = 0;
|
||||||
} else if (numType == typeof(ushort)) {
|
} else if (numType == typeof(ushort)) {
|
||||||
MemoryLength = 0;
|
memoryLength = 0;
|
||||||
} else if (numType == typeof(int)) {
|
} else if (numType == typeof(int)) {
|
||||||
MemoryLength = 1;
|
memoryLength = 1;
|
||||||
} else if (numType == typeof(uint)) {
|
} else if (numType == typeof(uint)) {
|
||||||
MemoryLength = 1;
|
memoryLength = 1;
|
||||||
} else if (numType == typeof(float)) {
|
} else if (numType == typeof(float)) {
|
||||||
MemoryLength = 1;
|
memoryLength = 1;
|
||||||
} else if (numType == typeof(TimeSpan)) {
|
} else if (numType == typeof(TimeSpan)) {
|
||||||
MemoryLength = 1;
|
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;
|
||||||
|
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 {
|
} else {
|
||||||
throw new NotSupportedException($"The type {numType} is not allowed for Number Registers");
|
throw new NotSupportedException($"The type {numType} is not allowed for Number Registers");
|
||||||
}
|
}
|
||||||
|
|
||||||
isUsedBitwise = isBitwise;
|
isUsedBitwise = isBitwise;
|
||||||
|
enumType = _enumType;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void SetValueFromPLC (object val) {
|
||||||
|
LastValue = (T)val;
|
||||||
|
TriggerChangedEvnt(this);
|
||||||
|
TriggerNotifyChange();
|
||||||
|
}
|
||||||
|
|
||||||
public override string ToString() {
|
public override string ToString() {
|
||||||
return $"Adress: {MemoryAdress} Val: {Value}";
|
return $"Adress: {MemoryAdress} Val: {Value}";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,12 +17,63 @@ namespace MewtocolNet.Registers {
|
|||||||
/// Gets called whenever the value was changed
|
/// Gets called whenever the value was changed
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event Action<object> ValueChanged;
|
public event Action<object> ValueChanged;
|
||||||
|
/// <summary>
|
||||||
|
/// Triggers when a property on the register changes
|
||||||
|
/// </summary>
|
||||||
public event PropertyChangedEventHandler PropertyChanged;
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
|
|
||||||
public string Name { get; set; }
|
internal Type collectionType;
|
||||||
public int MemoryAdress { get; set; }
|
/// <summary>
|
||||||
public int MemoryLength { get; set; }
|
/// The type of collection the register is in or null of added manually
|
||||||
|
/// </summary>
|
||||||
|
public Type CollectionType => collectionType;
|
||||||
|
|
||||||
|
internal string name;
|
||||||
|
/// <summary>
|
||||||
|
/// The register name or null of not defined
|
||||||
|
/// </summary>
|
||||||
|
public string Name => name;
|
||||||
|
|
||||||
|
internal int memoryAdress;
|
||||||
|
/// <summary>
|
||||||
|
/// The registers memory adress if not a special register
|
||||||
|
/// </summary>
|
||||||
|
public int MemoryAdress => memoryAdress;
|
||||||
|
|
||||||
|
internal int memoryLength;
|
||||||
|
/// <summary>
|
||||||
|
/// The rgisters memory length
|
||||||
|
/// </summary>
|
||||||
|
public int MemoryLength => memoryLength;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The value of the register auto converted to a string
|
||||||
|
/// </summary>
|
||||||
|
public string StringValue => GetValueString();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The name the register would have in the PLC
|
||||||
|
/// </summary>
|
||||||
|
public string RegisterPLCName => GetRegisterPLCName();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The combined name with the holding register class type infront
|
||||||
|
/// </summary>
|
||||||
|
public string CombinedName => GetCombinedName();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The name of the class that contains this register or empty if it was added manually
|
||||||
|
/// </summary>
|
||||||
|
public string ContainerName => GetContainerName();
|
||||||
|
|
||||||
internal bool isUsedBitwise { get; set; }
|
internal bool isUsedBitwise { get; set; }
|
||||||
|
internal Type enumType { get; set; }
|
||||||
|
|
||||||
|
internal Register () {
|
||||||
|
ValueChanged += (obj) => {
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(StringValue)));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
public virtual string BuildMewtocolIdent() {
|
public virtual string BuildMewtocolIdent() {
|
||||||
|
|
||||||
@@ -60,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()}]" : "")}";
|
||||||
}
|
}
|
||||||
@@ -83,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.";
|
||||||
@@ -148,7 +210,23 @@ namespace MewtocolNet.Registers {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetRegisterPLCName () {
|
internal string GetCombinedName () {
|
||||||
|
|
||||||
|
return $"{(CollectionType != null ? $"{CollectionType.Name}." : "")}{Name ?? "Unnamed"}";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
internal string GetContainerName () {
|
||||||
|
|
||||||
|
return $"{(CollectionType != null ? $"{CollectionType.Name}" : "")}";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
internal string GetRegisterPLCName () {
|
||||||
|
|
||||||
|
if (this is BRegister bReg && bReg.SpecialAddress != SpecialAddress.None) {
|
||||||
|
return $"{GetRegisterString()}{bReg.SpecialAddress}";
|
||||||
|
}
|
||||||
|
|
||||||
return $"{GetRegisterString()}{MemoryAdress}";
|
return $"{GetRegisterString()}{MemoryAdress}";
|
||||||
|
|
||||||
|
|||||||
@@ -8,11 +8,8 @@ namespace MewtocolNet.Registers {
|
|||||||
public class SRegister : Register {
|
public class SRegister : Register {
|
||||||
|
|
||||||
private string lastVal = "";
|
private string lastVal = "";
|
||||||
public string Value {
|
|
||||||
|
|
||||||
get => lastVal;
|
public string Value => lastVal;
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public short ReservedSize { get; set; }
|
public short ReservedSize { get; set; }
|
||||||
|
|
||||||
@@ -22,8 +19,8 @@ namespace MewtocolNet.Registers {
|
|||||||
public SRegister(int _adress, int _reservedStringSize, string _name = null) {
|
public SRegister(int _adress, int _reservedStringSize, string _name = 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");
|
||||||
Name = _name;
|
name = _name;
|
||||||
MemoryAdress = _adress;
|
memoryAdress = _adress;
|
||||||
ReservedSize = (short)_reservedStringSize;
|
ReservedSize = (short)_reservedStringSize;
|
||||||
|
|
||||||
//calc mem length
|
//calc mem length
|
||||||
@@ -32,7 +29,7 @@ namespace MewtocolNet.Registers {
|
|||||||
wordsize++;
|
wordsize++;
|
||||||
}
|
}
|
||||||
|
|
||||||
MemoryLength = (int)Math.Round(wordsize + 1);
|
memoryLength = (int)Math.Round(wordsize + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString() {
|
public override string ToString() {
|
||||||
@@ -62,7 +59,7 @@ namespace MewtocolNet.Registers {
|
|||||||
return asciistring.ToString();
|
return asciistring.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetValueFromPLC (string val) {
|
internal void SetValueFromPLC (string val) {
|
||||||
lastVal = val;
|
lastVal = val;
|
||||||
TriggerChangedEvnt(this);
|
TriggerChangedEvnt(this);
|
||||||
TriggerNotifyChange();
|
TriggerNotifyChange();
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<PackageId>MewtocolNet</PackageId>
|
<PackageId>MewtocolNet</PackageId>
|
||||||
<Version>0.3.3</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