mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 11:11:23 +00:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a2f278dd1 | ||
|
|
19159ed183 | ||
|
|
635823a66f | ||
|
|
c7a6559f97 | ||
|
|
88a453355c | ||
|
|
6f8f891760 | ||
|
|
8fb8d4989d | ||
|
|
45a9fa0520 | ||
|
|
772f8b89a4 | ||
|
|
6c7c368b55 | ||
|
|
38f0f9f523 | ||
|
|
c2aecb387a | ||
|
|
0e1f5cd12b |
@@ -27,6 +27,8 @@ class Program {
|
|||||||
Console.ReadLine();
|
Console.ReadLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool isProgressReadout = false;
|
||||||
|
|
||||||
static void Scenario1 () {
|
static void Scenario1 () {
|
||||||
|
|
||||||
Task.Factory.StartNew(async () => {
|
Task.Factory.StartNew(async () => {
|
||||||
@@ -44,45 +46,113 @@ class Program {
|
|||||||
//attaching the register collection and an automatic poller
|
//attaching the register collection and an automatic poller
|
||||||
interf.WithRegisterCollection(registers).WithPoller();
|
interf.WithRegisterCollection(registers).WithPoller();
|
||||||
|
|
||||||
await interf.ConnectAsync(
|
_ = Task.Factory.StartNew(async () => {
|
||||||
(plcinf) => {
|
while (true) {
|
||||||
|
if (isProgressReadout) continue;
|
||||||
//reading a value from the register collection
|
Console.Title = $"Polling Paused: {interf.PollingPaused}, " +
|
||||||
Console.WriteLine($"BitValue is: {registers.BitValue}");
|
$"Speed UP: {interf.BytesPerSecondUpstream} B/s, " +
|
||||||
Console.WriteLine($"TestEnum is: {registers.TestEnum}");
|
$"Speed DOWN: {interf.BytesPerSecondDownstream} B/s, " +
|
||||||
|
$"Poll delay: {interf.PollerDelayMs} ms, " +
|
||||||
//writing a value to the registers
|
$"Queued MSGs: {interf.QueuedMessages}";
|
||||||
Task.Factory.StartNew(async () => {
|
await Task.Delay(1000);
|
||||||
|
|
||||||
//set plc to run mode if not already
|
|
||||||
await interf.SetOperationMode(OPMode.Run);
|
|
||||||
|
|
||||||
|
|
||||||
int startAdress = 10000;
|
|
||||||
int entryByteSize = 20 * 20;
|
|
||||||
|
|
||||||
var bytes = await interf.ReadByteRange(startAdress, entryByteSize);
|
|
||||||
Console.WriteLine($"Bytes: {string.Join('-', bytes)}");
|
|
||||||
|
|
||||||
await Task.Delay(2000);
|
|
||||||
|
|
||||||
await interf.SetRegisterAsync(nameof(registers.TestInt32), 100);
|
|
||||||
|
|
||||||
//adds 10 each time the plc connects to the PLCs INT regíster
|
|
||||||
interf.SetRegister(nameof(registers.TestInt16), (short)(registers.TestInt16 + 10));
|
|
||||||
//adds 1 each time the plc connects to the PLCs DINT regíster
|
|
||||||
interf.SetRegister(nameof(registers.TestInt32), (registers.TestInt32 + 1));
|
|
||||||
//adds 11.11 each time the plc connects to the PLCs REAL regíster
|
|
||||||
interf.SetRegister(nameof(registers.TestFloat32), (float)(registers.TestFloat32 + 11.11));
|
|
||||||
//writes 'Hello' to the PLCs string register
|
|
||||||
interf.SetRegister(nameof(registers.TestString2), "Hello");
|
|
||||||
//set the current second to the PLCs TIME register
|
|
||||||
interf.SetRegister(nameof(registers.TestTime), TimeSpan.FromSeconds(DateTime.Now.Second));
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
);
|
});
|
||||||
|
|
||||||
|
await interf.ConnectAsync((plcinf) => AfterConnect(interf, registers));
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void AfterConnect (MewtocolInterface interf, TestRegisters registers) {
|
||||||
|
|
||||||
|
//reading a value from the register collection
|
||||||
|
Console.WriteLine($"BitValue is: {registers.BitValue}");
|
||||||
|
Console.WriteLine($"TestEnum is: {registers.TestEnum}");
|
||||||
|
|
||||||
|
_ = Task.Factory.StartNew(async () => {
|
||||||
|
|
||||||
|
while(true) {
|
||||||
|
|
||||||
|
isProgressReadout = true;
|
||||||
|
|
||||||
|
await interf.ReadByteRange(1000, 2000, (p) => {
|
||||||
|
|
||||||
|
var totSteps = 10;
|
||||||
|
var cSteps = totSteps * p;
|
||||||
|
|
||||||
|
string progBar = "";
|
||||||
|
for (int i = 0; i < totSteps; i++) {
|
||||||
|
|
||||||
|
if(i < (int)cSteps) {
|
||||||
|
progBar += "⬛";
|
||||||
|
} else {
|
||||||
|
progBar += "⬜";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.Title = $"Prog read range: {(p * 100).ToString("N1")}% {progBar} Queued MSGs: {interf.QueuedMessages}";
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
isProgressReadout = false;
|
||||||
|
|
||||||
|
await Task.Delay(3000);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
//writing a value to the registers
|
||||||
|
_ = Task.Factory.StartNew(async () => {
|
||||||
|
|
||||||
|
//set plc to run mode if not already
|
||||||
|
await interf.SetOperationMode(OPMode.Run);
|
||||||
|
|
||||||
|
int startAdress = 10000;
|
||||||
|
int entryByteSize = 20 * 20;
|
||||||
|
|
||||||
|
var bytes = await interf.ReadByteRange(startAdress, entryByteSize);
|
||||||
|
Console.WriteLine($"Bytes: {string.Join('-', bytes)}");
|
||||||
|
|
||||||
|
await Task.Delay(2000);
|
||||||
|
|
||||||
|
await interf.SetRegisterAsync(nameof(registers.TestInt32), 100);
|
||||||
|
|
||||||
|
//adds 10 each time the plc connects to the PLCs INT regíster
|
||||||
|
interf.SetRegister(nameof(registers.TestInt16), (short)(registers.TestInt16 + 10));
|
||||||
|
//adds 1 each time the plc connects to the PLCs DINT regíster
|
||||||
|
interf.SetRegister(nameof(registers.TestInt32), (registers.TestInt32 + 1));
|
||||||
|
//adds 11.11 each time the plc connects to the PLCs REAL regíster
|
||||||
|
interf.SetRegister(nameof(registers.TestFloat32), (float)(registers.TestFloat32 + 11.11));
|
||||||
|
//writes 'Hello' to the PLCs string register
|
||||||
|
interf.SetRegister(nameof(registers.TestString2), "Hello");
|
||||||
|
//set the current second to the PLCs TIME register
|
||||||
|
interf.SetRegister(nameof(registers.TestTime), TimeSpan.FromSeconds(DateTime.Now.Second));
|
||||||
|
|
||||||
|
//test pausing poller
|
||||||
|
|
||||||
|
bool pollerPaused = false;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
|
||||||
|
await Task.Delay(5000);
|
||||||
|
|
||||||
|
pollerPaused = !pollerPaused;
|
||||||
|
|
||||||
|
if (pollerPaused) {
|
||||||
|
Console.WriteLine("Pausing poller");
|
||||||
|
await interf.PausePollingAsync();
|
||||||
|
//interf.PollerDelayMs += 10;
|
||||||
|
Console.WriteLine("Paused poller");
|
||||||
|
} else {
|
||||||
|
interf.ResumePolling();
|
||||||
|
Console.WriteLine("Resumed poller");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -97,14 +167,14 @@ class Program {
|
|||||||
|
|
||||||
Task.Factory.StartNew(async () => {
|
Task.Factory.StartNew(async () => {
|
||||||
|
|
||||||
using(var interf = new MewtocolInterface("10.237.191.3")) {
|
//automatic endpoint
|
||||||
|
using (var interf = new MewtocolInterface("10.237.191.3")) {
|
||||||
|
|
||||||
await interf.ConnectAsync();
|
await interf.ConnectAsync();
|
||||||
|
|
||||||
if(interf.IsConnected) {
|
if (interf.IsConnected) {
|
||||||
|
|
||||||
var plcInf = await interf.GetPLCInfoAsync();
|
await Task.Delay(5000);
|
||||||
Console.WriteLine(plcInf);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,15 +182,16 @@ class Program {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//manual endpoint
|
||||||
using (var interf = new MewtocolInterface("10.237.191.3")) {
|
using (var interf = new MewtocolInterface("10.237.191.3")) {
|
||||||
|
|
||||||
|
interf.HostEndpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("10.237.191.77"), 0);
|
||||||
|
|
||||||
await interf.ConnectAsync();
|
await interf.ConnectAsync();
|
||||||
|
|
||||||
if (interf.IsConnected) {
|
if(interf.IsConnected) {
|
||||||
|
|
||||||
var plcInf = await interf.GetPLCInfoAsync();
|
await Task.Delay(5000);
|
||||||
Console.WriteLine(plcInf);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -68,6 +68,8 @@ namespace Examples {
|
|||||||
[Register(50)]
|
[Register(50)]
|
||||||
public CurrentState TestEnum { get; private set; }
|
public CurrentState TestEnum { get; private set; }
|
||||||
|
|
||||||
|
[Register(100)]
|
||||||
|
public TimeSpan TsTest2 { get; private set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,28 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace MewtocolNet.Registers {
|
namespace MewtocolNet.Registers {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Contains information about the plc and its cpu
|
||||||
|
/// </summary>
|
||||||
public partial class CpuInfo {
|
public partial class CpuInfo {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The cpu type of the plc
|
||||||
|
/// </summary>
|
||||||
public CpuType Cputype { get; set; }
|
public CpuType Cputype { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Program capacity in 1K steps
|
||||||
|
/// </summary>
|
||||||
public int ProgramCapacity { get; set; }
|
public int ProgramCapacity { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Version of the cpu
|
||||||
|
/// </summary>
|
||||||
public string CpuVersion { get; set; }
|
public string CpuVersion { get; set; }
|
||||||
|
|
||||||
|
internal static CpuInfo BuildFromHexString (string _cpuType, string _cpuVersion, string _progCapacity) {
|
||||||
public static CpuInfo BuildFromHexString (string _cpuType, string _cpuVersion, string _progCapacity) {
|
|
||||||
|
|
||||||
CpuInfo retInf = new CpuInfo();
|
CpuInfo retInf = new CpuInfo();
|
||||||
|
|
||||||
@@ -47,8 +61,7 @@ namespace MewtocolNet.Registers {
|
|||||||
return retInf;
|
return retInf;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -14,15 +14,61 @@ namespace MewtocolNet {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class MewtocolInterface {
|
public partial class MewtocolInterface {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// True if the auto poller is currently paused
|
||||||
|
/// </summary>
|
||||||
|
public bool PollingPaused => pollerIsPaused;
|
||||||
|
|
||||||
internal event Action PolledCycle;
|
internal event Action PolledCycle;
|
||||||
internal bool ContinousReaderRunning;
|
|
||||||
|
internal volatile bool pollerTaskRunning;
|
||||||
|
internal volatile bool pollerTaskStopped;
|
||||||
|
internal volatile bool pollerIsPaused;
|
||||||
|
|
||||||
internal bool usePoller = false;
|
internal bool usePoller = false;
|
||||||
|
|
||||||
#region Register Polling
|
#region Register Polling
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Kills the poller completely
|
||||||
|
/// </summary>
|
||||||
internal void KillPoller () {
|
internal void KillPoller () {
|
||||||
|
|
||||||
ContinousReaderRunning = false;
|
pollerTaskRunning = false;
|
||||||
|
pollerTaskStopped = true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Pauses the polling and waits for the last message to be sent
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task PausePollingAsync () {
|
||||||
|
|
||||||
|
if (!pollerTaskRunning)
|
||||||
|
return;
|
||||||
|
|
||||||
|
pollerTaskRunning = false;
|
||||||
|
|
||||||
|
while (!pollerIsPaused) {
|
||||||
|
|
||||||
|
if (pollerIsPaused)
|
||||||
|
break;
|
||||||
|
|
||||||
|
await Task.Delay(10);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
pollerTaskRunning = false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Resumes the polling
|
||||||
|
/// </summary>
|
||||||
|
public void ResumePolling () {
|
||||||
|
|
||||||
|
pollerTaskRunning = true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,94 +77,107 @@ namespace MewtocolNet {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
internal void AttachPoller () {
|
internal void AttachPoller () {
|
||||||
|
|
||||||
if (ContinousReaderRunning)
|
if (pollerTaskRunning)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Task.Factory.StartNew(async () => {
|
Task.Factory.StartNew(async () => {
|
||||||
|
|
||||||
Logger.Log("Poller is attaching", LogLevel.Info, this);
|
Logger.Log("Poller is attaching", LogLevel.Info, this);
|
||||||
|
|
||||||
int it = 0;
|
int iteration = 0;
|
||||||
ContinousReaderRunning = true;
|
|
||||||
|
|
||||||
while (ContinousReaderRunning) {
|
pollerTaskStopped = false;
|
||||||
|
pollerTaskRunning = true;
|
||||||
|
pollerIsPaused = false;
|
||||||
|
|
||||||
|
while (!pollerTaskStopped) {
|
||||||
|
|
||||||
|
while (pollerTaskRunning) {
|
||||||
|
|
||||||
|
if (iteration >= Registers.Count + 1) {
|
||||||
|
iteration = 0;
|
||||||
|
//invoke cycle polled event
|
||||||
|
InvokePolledCycleDone();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (iteration >= Registers.Count) {
|
||||||
|
await GetPLCInfoAsync();
|
||||||
|
iteration++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var reg = Registers[iteration];
|
||||||
|
|
||||||
|
if (reg is NRegister<short> shortReg) {
|
||||||
|
var lastVal = shortReg.Value;
|
||||||
|
var readout = (await ReadNumRegister(shortReg)).Register.Value;
|
||||||
|
if (lastVal != readout) {
|
||||||
|
InvokeRegisterChanged(shortReg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (reg is NRegister<ushort> ushortReg) {
|
||||||
|
var lastVal = ushortReg.Value;
|
||||||
|
var readout = (await ReadNumRegister(ushortReg)).Register.Value;
|
||||||
|
if (lastVal != readout) {
|
||||||
|
InvokeRegisterChanged(ushortReg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (reg is NRegister<int> intReg) {
|
||||||
|
var lastVal = intReg.Value;
|
||||||
|
var readout = (await ReadNumRegister(intReg)).Register.Value;
|
||||||
|
if (lastVal != readout) {
|
||||||
|
InvokeRegisterChanged(intReg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (reg is NRegister<uint> uintReg) {
|
||||||
|
var lastVal = uintReg.Value;
|
||||||
|
var readout = (await ReadNumRegister(uintReg)).Register.Value;
|
||||||
|
if (lastVal != readout) {
|
||||||
|
InvokeRegisterChanged(uintReg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (reg is NRegister<float> floatReg) {
|
||||||
|
var lastVal = floatReg.Value;
|
||||||
|
var readout = (await ReadNumRegister(floatReg)).Register.Value;
|
||||||
|
if (lastVal != readout) {
|
||||||
|
InvokeRegisterChanged(floatReg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (reg is NRegister<TimeSpan> tsReg) {
|
||||||
|
var lastVal = tsReg.Value;
|
||||||
|
var readout = (await ReadNumRegister(tsReg)).Register.Value;
|
||||||
|
if (lastVal != readout) {
|
||||||
|
InvokeRegisterChanged(tsReg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (reg is BRegister boolReg) {
|
||||||
|
var lastVal = boolReg.Value;
|
||||||
|
var readout = (await ReadBoolRegister(boolReg)).Register.Value;
|
||||||
|
if (lastVal != readout) {
|
||||||
|
InvokeRegisterChanged(boolReg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (reg is SRegister stringReg) {
|
||||||
|
var lastVal = stringReg.Value;
|
||||||
|
var readout = (await ReadStringRegister(stringReg)).Register.Value;
|
||||||
|
if (lastVal != readout) {
|
||||||
|
InvokeRegisterChanged(stringReg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
iteration++;
|
||||||
|
|
||||||
|
await Task.Delay(pollerDelayMs);
|
||||||
|
|
||||||
if (it >= Registers.Count + 1) {
|
|
||||||
it = 0;
|
|
||||||
//invoke cycle polled event
|
|
||||||
InvokePolledCycleDone();
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (it >= Registers.Count) {
|
pollerIsPaused = !pollerTaskRunning;
|
||||||
await GetPLCInfoAsync();
|
|
||||||
it++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var reg = Registers[it];
|
|
||||||
|
|
||||||
if (reg is NRegister<short> shortReg) {
|
|
||||||
var lastVal = shortReg.Value;
|
|
||||||
var readout = (await ReadNumRegister(shortReg)).Register.Value;
|
|
||||||
if (lastVal != readout) {
|
|
||||||
InvokeRegisterChanged(shortReg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (reg is NRegister<ushort> ushortReg) {
|
|
||||||
var lastVal = ushortReg.Value;
|
|
||||||
var readout = (await ReadNumRegister(ushortReg)).Register.Value;
|
|
||||||
if (lastVal != readout) {
|
|
||||||
InvokeRegisterChanged(ushortReg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (reg is NRegister<int> intReg) {
|
|
||||||
var lastVal = intReg.Value;
|
|
||||||
var readout = (await ReadNumRegister(intReg)).Register.Value;
|
|
||||||
if (lastVal != readout) {
|
|
||||||
InvokeRegisterChanged(intReg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (reg is NRegister<uint> uintReg) {
|
|
||||||
var lastVal = uintReg.Value;
|
|
||||||
var readout = (await ReadNumRegister(uintReg)).Register.Value;
|
|
||||||
if (lastVal != readout) {
|
|
||||||
InvokeRegisterChanged(uintReg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (reg is NRegister<float> floatReg) {
|
|
||||||
var lastVal = floatReg.Value;
|
|
||||||
var readout = (await ReadNumRegister(floatReg)).Register.Value;
|
|
||||||
if (lastVal != readout) {
|
|
||||||
InvokeRegisterChanged(floatReg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (reg is NRegister<TimeSpan> tsReg) {
|
|
||||||
var lastVal = tsReg.Value;
|
|
||||||
var readout = (await ReadNumRegister(tsReg)).Register.Value;
|
|
||||||
if (lastVal != readout) {
|
|
||||||
InvokeRegisterChanged(tsReg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (reg is BRegister boolReg) {
|
|
||||||
var lastVal = boolReg.Value;
|
|
||||||
var readout = (await ReadBoolRegister(boolReg)).Register.Value;
|
|
||||||
if (lastVal != readout) {
|
|
||||||
InvokeRegisterChanged(boolReg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (reg is SRegister stringReg) {
|
|
||||||
var lastVal = stringReg.Value;
|
|
||||||
var readout = (await ReadStringRegister(stringReg)).Register.Value;
|
|
||||||
if (lastVal != readout) {
|
|
||||||
InvokeRegisterChanged(stringReg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
it++;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pollerIsPaused = false;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ namespace MewtocolNet {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public event PropertyChangedEventHandler PropertyChanged;
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
|
|
||||||
private int connectTimeout = 1000;
|
private int connectTimeout = 3000;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The initial connection timeout in milliseconds
|
/// The initial connection timeout in milliseconds
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -52,6 +52,31 @@ namespace MewtocolNet {
|
|||||||
set { connectTimeout = value; }
|
set { connectTimeout = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private volatile int pollerDelayMs = 0;
|
||||||
|
/// <summary>
|
||||||
|
/// Delay for each poller cycle in milliseconds, default = 0
|
||||||
|
/// </summary>
|
||||||
|
public int PollerDelayMs {
|
||||||
|
get => pollerDelayMs;
|
||||||
|
set {
|
||||||
|
pollerDelayMs = value;
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PollerDelayMs)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private volatile int queuedMessages;
|
||||||
|
/// <summary>
|
||||||
|
/// Currently queued Messages
|
||||||
|
/// </summary>
|
||||||
|
public int QueuedMessages {
|
||||||
|
get => queuedMessages;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The host ip endpoint, leave it null to use an automatic interface
|
||||||
|
/// </summary>
|
||||||
|
public IPEndPoint HostEndpoint { get; set; }
|
||||||
|
|
||||||
private bool isConnected;
|
private bool isConnected;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The current connection state of the interface
|
/// The current connection state of the interface
|
||||||
@@ -96,6 +121,9 @@ namespace MewtocolNet {
|
|||||||
private int stationNumber;
|
private int stationNumber;
|
||||||
private int cycleTimeMs = 25;
|
private int cycleTimeMs = 25;
|
||||||
|
|
||||||
|
private int bytesTotalCountedUpstream = 0;
|
||||||
|
private int bytesTotalCountedDownstream = 0;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The current IP of the PLC connection
|
/// The current IP of the PLC connection
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -120,6 +148,30 @@ namespace MewtocolNet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int bytesPerSecondUpstream = 0;
|
||||||
|
/// <summary>
|
||||||
|
/// The current transmission speed in bytes per second
|
||||||
|
/// </summary>
|
||||||
|
public int BytesPerSecondUpstream {
|
||||||
|
get { return bytesPerSecondUpstream; }
|
||||||
|
private set {
|
||||||
|
bytesPerSecondUpstream = value;
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BytesPerSecondUpstream)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int bytesPerSecondDownstream = 0;
|
||||||
|
/// <summary>
|
||||||
|
/// The current transmission speed in bytes per second
|
||||||
|
/// </summary>
|
||||||
|
public int BytesPerSecondDownstream {
|
||||||
|
get { return bytesPerSecondDownstream; }
|
||||||
|
private set {
|
||||||
|
bytesPerSecondDownstream = value;
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BytesPerSecondDownstream)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal NetworkStream stream;
|
internal NetworkStream stream;
|
||||||
internal TcpClient client;
|
internal TcpClient client;
|
||||||
internal readonly SerialQueue queue = new SerialQueue();
|
internal readonly SerialQueue queue = new SerialQueue();
|
||||||
@@ -127,6 +179,9 @@ namespace MewtocolNet {
|
|||||||
internal int SendExceptionsInRow = 0;
|
internal int SendExceptionsInRow = 0;
|
||||||
internal bool ImportantTaskRunning = false;
|
internal bool ImportantTaskRunning = false;
|
||||||
|
|
||||||
|
private Stopwatch speedStopwatchUpstr;
|
||||||
|
private Stopwatch speedStopwatchDownstr;
|
||||||
|
|
||||||
#region Initialization
|
#region Initialization
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -271,24 +326,41 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
client = new TcpClient() {
|
if(HostEndpoint != null) {
|
||||||
ReceiveBufferSize = RecBufferSize,
|
|
||||||
NoDelay = false,
|
client = new TcpClient(HostEndpoint) {
|
||||||
ExclusiveAddressUse = true,
|
ReceiveBufferSize = RecBufferSize,
|
||||||
};
|
NoDelay = false,
|
||||||
|
};
|
||||||
|
var ep = (IPEndPoint)client.Client.LocalEndPoint;
|
||||||
|
Logger.Log($"Connecting [MAN] endpoint: {ep.Address}:{ep.Port}", LogLevel.Verbose, this);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
client = new TcpClient() {
|
||||||
|
ReceiveBufferSize = RecBufferSize,
|
||||||
|
NoDelay = false,
|
||||||
|
ExclusiveAddressUse = true,
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
var result = client.BeginConnect(targetIP, port, null, null);
|
var result = client.BeginConnect(targetIP, port, null, null);
|
||||||
var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(ConnectTimeout));
|
var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(ConnectTimeout));
|
||||||
|
|
||||||
if(!success) {
|
if(!success || !client.Connected) {
|
||||||
OnMajorSocketExceptionWhileConnecting();
|
OnMajorSocketExceptionWhileConnecting();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(HostEndpoint == null) {
|
||||||
|
var ep = (IPEndPoint)client.Client.LocalEndPoint;
|
||||||
|
Logger.Log($"Connecting [AUTO] endpoint: {ep.Address.MapToIPv4()}:{ep.Port}", LogLevel.Verbose, this);
|
||||||
|
}
|
||||||
|
|
||||||
stream = client.GetStream();
|
stream = client.GetStream();
|
||||||
stream.ReadTimeout = 1000;
|
stream.ReadTimeout = 1000;
|
||||||
|
|
||||||
Console.WriteLine($"Connected {client.Connected}");
|
|
||||||
await Task.CompletedTask;
|
await Task.CompletedTask;
|
||||||
|
|
||||||
} catch (SocketException) {
|
} catch (SocketException) {
|
||||||
@@ -660,8 +732,13 @@ namespace MewtocolNet {
|
|||||||
//send request
|
//send request
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
queuedMessages++;
|
||||||
|
|
||||||
var response = await queue.Enqueue(() => SendSingleBlock(_msg));
|
var response = await queue.Enqueue(() => SendSingleBlock(_msg));
|
||||||
|
|
||||||
|
if (queuedMessages > 0)
|
||||||
|
queuedMessages--;
|
||||||
|
|
||||||
if (response == null) {
|
if (response == null) {
|
||||||
return new CommandResult {
|
return new CommandResult {
|
||||||
Success = false,
|
Success = false,
|
||||||
@@ -707,12 +784,23 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
if (client == null || !client.Connected ) {
|
if (client == null || !client.Connected ) {
|
||||||
await ConnectTCP();
|
await ConnectTCP();
|
||||||
if (!client.Connected)
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (client == null || !client.Connected)
|
||||||
|
return null;
|
||||||
|
|
||||||
var message = _blockString.ToHexASCIIBytes();
|
var message = _blockString.ToHexASCIIBytes();
|
||||||
|
|
||||||
|
//time measuring
|
||||||
|
if(speedStopwatchUpstr == null) {
|
||||||
|
speedStopwatchUpstr = Stopwatch.StartNew();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(speedStopwatchUpstr.Elapsed.TotalSeconds >= 1) {
|
||||||
|
speedStopwatchUpstr.Restart();
|
||||||
|
bytesTotalCountedUpstream = 0;
|
||||||
|
}
|
||||||
|
|
||||||
//send request
|
//send request
|
||||||
using (var sendStream = new MemoryStream(message)) {
|
using (var sendStream = new MemoryStream(message)) {
|
||||||
await sendStream.CopyToAsync(stream);
|
await sendStream.CopyToAsync(stream);
|
||||||
@@ -720,6 +808,13 @@ namespace MewtocolNet {
|
|||||||
Logger.Log($"--> OUT MSG: {_blockString}", LogLevel.Critical, this);
|
Logger.Log($"--> OUT MSG: {_blockString}", LogLevel.Critical, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//calc upstream speed
|
||||||
|
bytesTotalCountedUpstream += message.Length;
|
||||||
|
|
||||||
|
var perSecUpstream = (double)((bytesTotalCountedUpstream / speedStopwatchUpstr.Elapsed.TotalMilliseconds) * 1000);
|
||||||
|
if (perSecUpstream <= 10000)
|
||||||
|
BytesPerSecondUpstream = (int)Math.Round(perSecUpstream, MidpointRounding.AwayFromZero);
|
||||||
|
|
||||||
//await result
|
//await result
|
||||||
StringBuilder response = new StringBuilder();
|
StringBuilder response = new StringBuilder();
|
||||||
try {
|
try {
|
||||||
@@ -732,6 +827,17 @@ namespace MewtocolNet {
|
|||||||
while (!endLineCode && !startMsgCode) {
|
while (!endLineCode && !startMsgCode) {
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
|
||||||
|
//time measuring
|
||||||
|
if (speedStopwatchDownstr == null) {
|
||||||
|
speedStopwatchDownstr = Stopwatch.StartNew();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (speedStopwatchDownstr.Elapsed.TotalSeconds >= 1) {
|
||||||
|
speedStopwatchDownstr.Restart();
|
||||||
|
bytesTotalCountedDownstream = 0;
|
||||||
|
}
|
||||||
|
|
||||||
int bytes = await stream.ReadAsync(responseBuffer, 0, responseBuffer.Length);
|
int bytes = await stream.ReadAsync(responseBuffer, 0, responseBuffer.Length);
|
||||||
|
|
||||||
endLineCode = responseBuffer.Any(x => x == 0x0D);
|
endLineCode = responseBuffer.Any(x => x == 0x0D);
|
||||||
@@ -746,7 +852,7 @@ namespace MewtocolNet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException) {
|
} catch (IOException) {
|
||||||
Logger.Log($"Critical IO exception on receive", LogLevel.Critical, this);
|
OnMajorSocketExceptionWhileConnected();
|
||||||
return null;
|
return null;
|
||||||
} catch (SocketException) {
|
} catch (SocketException) {
|
||||||
OnMajorSocketExceptionWhileConnected();
|
OnMajorSocketExceptionWhileConnected();
|
||||||
@@ -754,8 +860,18 @@ namespace MewtocolNet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(!string.IsNullOrEmpty(response.ToString())) {
|
if(!string.IsNullOrEmpty(response.ToString())) {
|
||||||
|
|
||||||
Logger.Log($"<-- IN MSG: {response}", LogLevel.Critical, this);
|
Logger.Log($"<-- IN MSG: {response}", LogLevel.Critical, this);
|
||||||
|
|
||||||
|
bytesTotalCountedDownstream += Encoding.ASCII.GetByteCount(response.ToString());
|
||||||
|
|
||||||
|
var perSecDownstream = (double)((bytesTotalCountedDownstream / speedStopwatchDownstr.Elapsed.TotalMilliseconds) * 1000);
|
||||||
|
|
||||||
|
if(perSecUpstream <= 10000)
|
||||||
|
BytesPerSecondDownstream = (int)Math.Round(perSecUpstream, MidpointRounding.AwayFromZero);
|
||||||
|
|
||||||
return response.ToString();
|
return response.ToString();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -112,8 +112,9 @@ namespace MewtocolNet {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="start">Start adress</param>
|
/// <param name="start">Start adress</param>
|
||||||
/// <param name="count">Number of bytes to get</param>
|
/// <param name="count">Number of bytes to get</param>
|
||||||
|
/// <param name="onProgress">Gets invoked when the progress changes, contains the progress as a double</param>
|
||||||
/// <returns>A byte array or null of there was an error</returns>
|
/// <returns>A byte array or null of there was an error</returns>
|
||||||
public async Task<byte[]> ReadByteRange (int start, int count) {
|
public async Task<byte[]> ReadByteRange (int start, int count, Action<double> onProgress = null) {
|
||||||
|
|
||||||
var byteList = new List<byte>();
|
var byteList = new List<byte>();
|
||||||
|
|
||||||
@@ -146,6 +147,9 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(onProgress != null)
|
||||||
|
onProgress((double)i / wordLength);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return byteList.ToArray();
|
return byteList.ToArray();
|
||||||
@@ -190,6 +194,7 @@ namespace MewtocolNet {
|
|||||||
/// Writes to the given bool register on the PLC
|
/// Writes to the given bool register on the PLC
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="_toWrite">The register to write to</param>
|
/// <param name="_toWrite">The register to write to</param>
|
||||||
|
/// <param name="value">The value to write</param>
|
||||||
/// <returns>The success state of the write operation</returns>
|
/// <returns>The success state of the write operation</returns>
|
||||||
public async Task<bool> WriteBoolRegister (BRegister _toWrite, bool value) {
|
public async Task<bool> WriteBoolRegister (BRegister _toWrite, bool value) {
|
||||||
|
|
||||||
@@ -294,7 +299,7 @@ namespace MewtocolNet {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">Type of number (short, ushort, int, uint, float)</typeparam>
|
/// <typeparam name="T">Type of number (short, ushort, int, uint, float)</typeparam>
|
||||||
/// <param name="_toWrite">The register to write</param>
|
/// <param name="_toWrite">The register to write</param>
|
||||||
/// <param name="_stationNumber">Station number to access</param>
|
/// <param name="_value">The value to write</param>
|
||||||
/// <returns>The success state of the write operation</returns>
|
/// <returns>The success state of the write operation</returns>
|
||||||
public async Task<bool> WriteNumRegister<T> (NRegister<T> _toWrite, T _value) {
|
public async Task<bool> WriteNumRegister<T> (NRegister<T> _toWrite, T _value) {
|
||||||
|
|
||||||
|
|||||||
@@ -6,13 +6,38 @@ namespace MewtocolNet.Registers {
|
|||||||
/// All modes
|
/// All modes
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class PLCMode {
|
public class PLCMode {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// PLC is running
|
||||||
|
/// </summary>
|
||||||
public bool RunMode { get; set; }
|
public bool RunMode { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// PLC is in test
|
||||||
|
/// </summary>
|
||||||
public bool TestRunMode { get; set; }
|
public bool TestRunMode { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// BreakExcecuting
|
||||||
|
/// </summary>
|
||||||
public bool BreakExcecuting { get; set; }
|
public bool BreakExcecuting { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// BreakValid
|
||||||
|
/// </summary>
|
||||||
public bool BreakValid { get; set; }
|
public bool BreakValid { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// PLC output is enabled
|
||||||
|
/// </summary>
|
||||||
public bool OutputEnabled { get; set; }
|
public bool OutputEnabled { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// PLC runs step per step
|
||||||
|
/// </summary>
|
||||||
public bool StepRunMode { get; set; }
|
public bool StepRunMode { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Message executing
|
||||||
|
/// </summary>
|
||||||
public bool MessageExecuting { get; set; }
|
public bool MessageExecuting { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// PLC is in remote mode
|
||||||
|
/// </summary>
|
||||||
public bool RemoteMode { get; set; }
|
public bool RemoteMode { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -6,22 +6,39 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace MewtocolNet.RegisterAttributes {
|
namespace MewtocolNet.RegisterAttributes {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The size of the bitwise register
|
||||||
|
/// </summary>
|
||||||
public enum BitCount {
|
public enum BitCount {
|
||||||
|
/// <summary>
|
||||||
|
/// 16 bit
|
||||||
|
/// </summary>
|
||||||
B16,
|
B16,
|
||||||
|
/// <summary>
|
||||||
|
/// 32 bit
|
||||||
|
/// </summary>
|
||||||
B32
|
B32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Defines the behavior of a register property
|
||||||
|
/// </summary>
|
||||||
[AttributeUsage(AttributeTargets.Property)]
|
[AttributeUsage(AttributeTargets.Property)]
|
||||||
public class RegisterAttribute : Attribute {
|
public class RegisterAttribute : Attribute {
|
||||||
|
|
||||||
public int MemoryArea;
|
internal int MemoryArea;
|
||||||
public int StringLength;
|
internal int StringLength;
|
||||||
public RegisterType RegisterType;
|
internal RegisterType RegisterType;
|
||||||
public SpecialAddress SpecialAddress = SpecialAddress.None;
|
internal SpecialAddress SpecialAddress = SpecialAddress.None;
|
||||||
public BitCount BitCount;
|
internal BitCount BitCount;
|
||||||
public int AssignedBitIndex = -1;
|
internal int AssignedBitIndex = -1;
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attribute for string type or numeric registers
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="memoryArea">The area in the plcs memory</param>
|
||||||
|
/// <param name="stringLength">The max string length in the plc</param>
|
||||||
public RegisterAttribute (int memoryArea, int stringLength = 1) {
|
public RegisterAttribute (int memoryArea, int stringLength = 1) {
|
||||||
|
|
||||||
MemoryArea = memoryArea;
|
MemoryArea = memoryArea;
|
||||||
@@ -29,6 +46,11 @@ namespace MewtocolNet.RegisterAttributes {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attribute for boolean registers
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="memoryArea">The area in the plcs memory</param>
|
||||||
|
/// <param name="type">The type of boolean register</param>
|
||||||
public RegisterAttribute (int memoryArea, RegisterType type) {
|
public RegisterAttribute (int memoryArea, RegisterType type) {
|
||||||
|
|
||||||
if (type.ToString().StartsWith("DT"))
|
if (type.ToString().StartsWith("DT"))
|
||||||
@@ -40,6 +62,11 @@ namespace MewtocolNet.RegisterAttributes {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attribute for boolean registers
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="spAdress">The special area in the plcs memory</param>
|
||||||
|
/// <param name="type">The type of boolean register</param>
|
||||||
public RegisterAttribute (RegisterType type, SpecialAddress spAdress) {
|
public RegisterAttribute (RegisterType type, SpecialAddress spAdress) {
|
||||||
|
|
||||||
if (type.ToString().StartsWith("DT"))
|
if (type.ToString().StartsWith("DT"))
|
||||||
@@ -50,7 +77,11 @@ namespace MewtocolNet.RegisterAttributes {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attribute to read numeric registers as bitwise
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="memoryArea">The area in the plcs memory</param>
|
||||||
|
/// <param name="bitcount">The number of bits to parse</param>
|
||||||
public RegisterAttribute (int memoryArea, BitCount bitcount) {
|
public RegisterAttribute (int memoryArea, BitCount bitcount) {
|
||||||
|
|
||||||
MemoryArea = memoryArea;
|
MemoryArea = memoryArea;
|
||||||
@@ -59,6 +90,12 @@ namespace MewtocolNet.RegisterAttributes {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attribute to read numeric registers as bitwise
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="memoryArea">The area in the plcs memory</param>
|
||||||
|
/// <param name="bitcount">The number of bits to parse</param>
|
||||||
|
/// <param name="assignBit">The index of the bit that gets linked to the bool</param>
|
||||||
public RegisterAttribute (int memoryArea, uint assignBit, BitCount bitcount) {
|
public RegisterAttribute (int memoryArea, uint assignBit, BitCount bitcount) {
|
||||||
|
|
||||||
if(assignBit > 15 && bitcount == BitCount.B16) {
|
if(assignBit > 15 && bitcount == BitCount.B16) {
|
||||||
|
|||||||
@@ -34,8 +34,17 @@ namespace MewtocolNet.RegisterAttributes {
|
|||||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets called when the register collection base was linked to its parent mewtocol interface
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="plc">The parent interface</param>
|
||||||
public virtual void OnInterfaceLinked (MewtocolInterface plc) { }
|
public virtual void OnInterfaceLinked (MewtocolInterface plc) { }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets called when the register collection base was linked to its parent mewtocol interface
|
||||||
|
/// and the plc connection is established
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="plc">The parent interface</param>
|
||||||
public virtual void OnInterfaceLinkedAndOnline (MewtocolInterface plc) { }
|
public virtual void OnInterfaceLinkedAndOnline (MewtocolInterface plc) { }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,7 @@ namespace MewtocolNet.Registers {
|
|||||||
internal RegisterType RegType { get; private set; }
|
internal RegisterType RegType { get; private set; }
|
||||||
internal SpecialAddress SpecialAddress { get; private set; }
|
internal SpecialAddress SpecialAddress { get; private set; }
|
||||||
|
|
||||||
public bool NeedValue;
|
internal bool LastValue;
|
||||||
public bool LastValue;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The value of the register
|
/// The value of the register
|
||||||
@@ -54,6 +53,9 @@ namespace MewtocolNet.Registers {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Builds the register area name
|
||||||
|
/// </summary>
|
||||||
public override string BuildMewtocolIdent () {
|
public override string BuildMewtocolIdent () {
|
||||||
|
|
||||||
//build area code from register type
|
//build area code from register type
|
||||||
@@ -73,9 +75,7 @@ namespace MewtocolNet.Registers {
|
|||||||
TriggerChangedEvnt(this);
|
TriggerChangedEvnt(this);
|
||||||
TriggerNotifyChange();
|
TriggerNotifyChange();
|
||||||
}
|
}
|
||||||
public override string ToString() {
|
|
||||||
return $"Adress: {MemoryAdress} Val: {Value}";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,20 @@
|
|||||||
namespace MewtocolNet.Registers {
|
namespace MewtocolNet.Registers {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Result for a boolean register
|
||||||
|
/// </summary>
|
||||||
public class BRegisterResult {
|
public class BRegisterResult {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The command result
|
||||||
|
/// </summary>
|
||||||
public CommandResult Result { get; set; }
|
public CommandResult Result { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The used register
|
||||||
|
/// </summary>
|
||||||
public BRegister Register { get; set; }
|
public BRegister Register { get; set; }
|
||||||
|
|
||||||
public override string ToString() {
|
|
||||||
string errmsg = Result.Success ? "" : $", Error [{Result.ErrorDescription}]";
|
|
||||||
return $"Result [{Result.Success}], Register [{Register.ToString()}]{errmsg}";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ namespace MewtocolNet.Registers {
|
|||||||
/// <typeparam name="T">The type of the numeric value</typeparam>
|
/// <typeparam name="T">The type of the numeric value</typeparam>
|
||||||
public class NRegister<T> : Register {
|
public class NRegister<T> : Register {
|
||||||
|
|
||||||
public T NeedValue;
|
internal T LastValue;
|
||||||
public T LastValue;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The value of the register
|
/// The value of the register
|
||||||
@@ -78,10 +77,6 @@ namespace MewtocolNet.Registers {
|
|||||||
TriggerNotifyChange();
|
TriggerNotifyChange();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString() {
|
|
||||||
return $"Adress: {MemoryAdress} Val: {Value}";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,13 +6,16 @@ namespace MewtocolNet.Registers {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The type of the numeric value</typeparam>
|
/// <typeparam name="T">The type of the numeric value</typeparam>
|
||||||
public class NRegisterResult<T> {
|
public class NRegisterResult<T> {
|
||||||
public CommandResult Result { get; set; }
|
|
||||||
public NRegister<T> Register { get; set; }
|
|
||||||
|
|
||||||
public override string ToString() {
|
/// <summary>
|
||||||
string errmsg = Result.Success ? "" : $", Error [{Result.ErrorDescription}]";
|
/// Command result
|
||||||
return $"Result [{Result.Success}], Register [{Register.ToString()}]{errmsg}";
|
/// </summary>
|
||||||
}
|
public CommandResult Result { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The used register
|
||||||
|
/// </summary>
|
||||||
|
public NRegister<T> Register { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Trys to get the value of there is one
|
/// Trys to get the value of there is one
|
||||||
|
|||||||
@@ -75,6 +75,9 @@ namespace MewtocolNet.Registers {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Builds the register area name
|
||||||
|
/// </summary>
|
||||||
public virtual string BuildMewtocolIdent() {
|
public virtual string BuildMewtocolIdent() {
|
||||||
|
|
||||||
StringBuilder asciistring = new StringBuilder("D");
|
StringBuilder asciistring = new StringBuilder("D");
|
||||||
@@ -112,9 +115,14 @@ namespace MewtocolNet.Registers {
|
|||||||
public string GetValueString () {
|
public string GetValueString () {
|
||||||
|
|
||||||
if (enumType != null && this is NRegister<int> intEnumReg) {
|
if (enumType != null && this is NRegister<int> intEnumReg) {
|
||||||
|
|
||||||
var dict = new Dictionary<int, string>();
|
var dict = new Dictionary<int, string>();
|
||||||
|
|
||||||
foreach (var name in Enum.GetNames(enumType)) {
|
foreach (var name in Enum.GetNames(enumType)) {
|
||||||
dict.Add((int)Enum.Parse(enumType, name), name);
|
int enumKey = (int)Enum.Parse(enumType, name);
|
||||||
|
if(!dict.ContainsKey(enumKey)) {
|
||||||
|
dict.Add(enumKey, name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(dict.ContainsKey(intEnumReg.Value)) {
|
if(dict.ContainsKey(intEnumReg.Value)) {
|
||||||
@@ -122,6 +130,7 @@ namespace MewtocolNet.Registers {
|
|||||||
} else {
|
} else {
|
||||||
return $"{intEnumReg.Value} (Missing Enum)";
|
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()}]" : "")}";
|
||||||
@@ -178,6 +187,9 @@ namespace MewtocolNet.Registers {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the register dataarea string DT for 16bit and DDT for 32 bit types
|
||||||
|
/// </summary>
|
||||||
public string GetRegisterString () {
|
public string GetRegisterString () {
|
||||||
|
|
||||||
if (this is NRegister<short> shortReg) {
|
if (this is NRegister<short> shortReg) {
|
||||||
|
|||||||
@@ -9,9 +9,12 @@ namespace MewtocolNet.Registers {
|
|||||||
|
|
||||||
private string lastVal = "";
|
private string lastVal = "";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The current value of the register
|
||||||
|
/// </summary>
|
||||||
public string Value => lastVal;
|
public string Value => lastVal;
|
||||||
|
|
||||||
public short ReservedSize { get; set; }
|
internal short ReservedSize { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defines a register containing a string
|
/// Defines a register containing a string
|
||||||
@@ -32,10 +35,9 @@ namespace MewtocolNet.Registers {
|
|||||||
memoryLength = (int)Math.Round(wordsize + 1);
|
memoryLength = (int)Math.Round(wordsize + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString() {
|
/// <summary>
|
||||||
return $"Adress: {MemoryAdress} Val: {Value}";
|
/// Builds the register identifier for the mewotocol protocol
|
||||||
}
|
/// </summary>
|
||||||
|
|
||||||
public override string BuildMewtocolIdent() {
|
public override string BuildMewtocolIdent() {
|
||||||
|
|
||||||
StringBuilder asciistring = new StringBuilder("D");
|
StringBuilder asciistring = new StringBuilder("D");
|
||||||
@@ -65,7 +67,6 @@ namespace MewtocolNet.Registers {
|
|||||||
TriggerNotifyChange();
|
TriggerNotifyChange();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,19 @@
|
|||||||
namespace MewtocolNet.Registers {
|
namespace MewtocolNet.Registers {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The results of a string register operation
|
||||||
|
/// </summary>
|
||||||
public class SRegisterResult {
|
public class SRegisterResult {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The command result
|
||||||
|
/// </summary>
|
||||||
public CommandResult Result { get; set; }
|
public CommandResult Result { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// The register definition used
|
||||||
|
/// </summary>
|
||||||
public SRegister Register { get; set; }
|
public SRegister Register { get; set; }
|
||||||
|
|
||||||
public override string ToString() {
|
|
||||||
string errmsg = Result.Success ? "" : $", Error [{Result.ErrorDescription}]";
|
|
||||||
return $"Result [{Result.Success}], Register [{Register.ToString()}]{errmsg}";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<PackageId>MewtocolNet</PackageId>
|
<PackageId>MewtocolNet</PackageId>
|
||||||
<Version>0.5.0</Version>
|
<Version>0.6.0</Version>
|
||||||
<Authors>Felix Weiss</Authors>
|
<Authors>Felix Weiss</Authors>
|
||||||
<Company>Womed</Company>
|
<Company>Womed</Company>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
|
|||||||
@@ -8,47 +8,6 @@ namespace MewtocolNet.Queue {
|
|||||||
readonly object _locker = new object();
|
readonly object _locker = new object();
|
||||||
readonly WeakReference<Task> _lastTask = new WeakReference<Task>(null);
|
readonly WeakReference<Task> _lastTask = new WeakReference<Task>(null);
|
||||||
|
|
||||||
internal Task Enqueue (Action action) {
|
|
||||||
return Enqueue<bool>(() => {
|
|
||||||
action();
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
internal Task<T> Enqueue<T> (Func<T> function) {
|
|
||||||
lock (_locker) {
|
|
||||||
Task lastTask;
|
|
||||||
Task<T> resultTask;
|
|
||||||
|
|
||||||
if (_lastTask.TryGetTarget(out lastTask)) {
|
|
||||||
resultTask = lastTask.ContinueWith(_ => function(), TaskContinuationOptions.ExecuteSynchronously);
|
|
||||||
} else {
|
|
||||||
resultTask = Task.Run(function);
|
|
||||||
}
|
|
||||||
|
|
||||||
_lastTask.SetTarget(resultTask);
|
|
||||||
|
|
||||||
return resultTask;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal Task Enqueue (Func<Task> asyncAction) {
|
|
||||||
lock (_locker) {
|
|
||||||
Task lastTask;
|
|
||||||
Task resultTask;
|
|
||||||
|
|
||||||
if (_lastTask.TryGetTarget(out lastTask)) {
|
|
||||||
resultTask = lastTask.ContinueWith(_ => asyncAction(), TaskContinuationOptions.ExecuteSynchronously).Unwrap();
|
|
||||||
} else {
|
|
||||||
resultTask = Task.Run(asyncAction);
|
|
||||||
}
|
|
||||||
|
|
||||||
_lastTask.SetTarget(resultTask);
|
|
||||||
|
|
||||||
return resultTask;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal Task<T> Enqueue<T> (Func<Task<T>> asyncFunction) {
|
internal Task<T> Enqueue<T> (Func<Task<T>> asyncFunction) {
|
||||||
lock (_locker) {
|
lock (_locker) {
|
||||||
Task lastTask;
|
Task lastTask;
|
||||||
|
|||||||
@@ -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.5.0" />
|
<PackageReference Include="MewtocolNet" Version="0.5.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