From 635823a66f1194df8be1b08664d95ba1bc417bdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Wei=C3=9F?= <72068105+Sandoun@users.noreply.github.com> Date: Tue, 27 Sep 2022 09:52:35 +0200 Subject: [PATCH] Made poller delay thread safe - added property for currentlly queued messages - added (optional) progress to ReadByteRange --- Examples/Program.cs | 165 +++++++++++------- MewtocolNet/Mewtocol/DynamicInterface.cs | 2 +- MewtocolNet/Mewtocol/MewtocolInterface.cs | 19 +- .../Mewtocol/MewtocolInterfaceRequests.cs | 6 +- MewtocolNet/MewtocolNet.csproj | 2 +- 5 files changed, 126 insertions(+), 68 deletions(-) diff --git a/Examples/Program.cs b/Examples/Program.cs index 17de76e..b2bccbb 100644 --- a/Examples/Program.cs +++ b/Examples/Program.cs @@ -27,6 +27,8 @@ class Program { Console.ReadLine(); } + private static bool isProgressReadout = false; + static void Scenario1 () { Task.Factory.StartNew(async () => { @@ -46,77 +48,116 @@ class Program { _ = Task.Factory.StartNew(async () => { while (true) { - Console.Title = $"Polling Paused: {interf.PollingPaused}, Speed UP: {interf.BytesPerSecondUpstream} B/s, Speed DOWN: {interf.BytesPerSecondDownstream} B/s"; + if (isProgressReadout) continue; + Console.Title = $"Polling Paused: {interf.PollingPaused}, " + + $"Speed UP: {interf.BytesPerSecondUpstream} B/s, " + + $"Speed DOWN: {interf.BytesPerSecondDownstream} B/s, " + + $"Poll delay: {interf.PollerDelayMs} ms, " + + $"Queued MSGs: {interf.QueuedMessages}"; await Task.Delay(1000); } }); - await interf.ConnectAsync( - (plcinf) => { - - //reading a value from the register collection - Console.WriteLine($"BitValue is: {registers.BitValue}"); - Console.WriteLine($"TestEnum is: {registers.TestEnum}"); - - //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(); - Console.WriteLine("Paused poller"); - } else { - interf.ResumePolling(); - Console.WriteLine("Resumed poller"); - } - - } - - - }); - - } - ); + 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"); + } + + } + + + }); + + } + static void Scenario2 () { Logger.LogLevel = LogLevel.Critical; diff --git a/MewtocolNet/Mewtocol/DynamicInterface.cs b/MewtocolNet/Mewtocol/DynamicInterface.cs index 31f135f..178128b 100644 --- a/MewtocolNet/Mewtocol/DynamicInterface.cs +++ b/MewtocolNet/Mewtocol/DynamicInterface.cs @@ -168,7 +168,7 @@ namespace MewtocolNet { iteration++; - await Task.Delay(PollerDelayMs); + await Task.Delay(pollerDelayMs); } diff --git a/MewtocolNet/Mewtocol/MewtocolInterface.cs b/MewtocolNet/Mewtocol/MewtocolInterface.cs index 4855c68..cbf58c5 100644 --- a/MewtocolNet/Mewtocol/MewtocolInterface.cs +++ b/MewtocolNet/Mewtocol/MewtocolInterface.cs @@ -52,13 +52,24 @@ namespace MewtocolNet { set { connectTimeout = value; } } - private int pollerDelayMs = 0; + private volatile int pollerDelayMs = 0; /// /// Delay for each poller cycle in milliseconds, default = 0 /// public int PollerDelayMs { - get { return pollerDelayMs; } - set { pollerDelayMs = value; } + get => pollerDelayMs; + set { + pollerDelayMs = value; + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PollerDelayMs))); + } + } + + private volatile int queuedMessages; + /// + /// Currently queued Messages + /// + public int QueuedMessages { + get => queuedMessages; } /// @@ -721,7 +732,9 @@ namespace MewtocolNet { //send request try { + queuedMessages++; var response = await queue.Enqueue(() => SendSingleBlock(_msg)); + queuedMessages--; if (response == null) { return new CommandResult { diff --git a/MewtocolNet/Mewtocol/MewtocolInterfaceRequests.cs b/MewtocolNet/Mewtocol/MewtocolInterfaceRequests.cs index 3df69b5..9c69906 100644 --- a/MewtocolNet/Mewtocol/MewtocolInterfaceRequests.cs +++ b/MewtocolNet/Mewtocol/MewtocolInterfaceRequests.cs @@ -112,8 +112,9 @@ namespace MewtocolNet { /// /// Start adress /// Number of bytes to get + /// Gets invoked when the progress changes, contains the progress as a double /// A byte array or null of there was an error - public async Task ReadByteRange (int start, int count) { + public async Task ReadByteRange (int start, int count, Action onProgress = null) { var byteList = new List(); @@ -146,6 +147,9 @@ namespace MewtocolNet { } + if(onProgress != null) + onProgress((double)i / wordLength); + } return byteList.ToArray(); diff --git a/MewtocolNet/MewtocolNet.csproj b/MewtocolNet/MewtocolNet.csproj index dc1cd4e..b1c5b7b 100644 --- a/MewtocolNet/MewtocolNet.csproj +++ b/MewtocolNet/MewtocolNet.csproj @@ -2,7 +2,7 @@ netstandard2.0 MewtocolNet - 0.5.6 + 0.5.7 Felix Weiss Womed true