diff --git a/Examples/Program.cs b/Examples/Program.cs index 0575d77..17de76e 100644 --- a/Examples/Program.cs +++ b/Examples/Program.cs @@ -32,7 +32,7 @@ static void Scenario1 () { Task.Factory.StartNew(async () => { //attaching the logger - Logger.LogLevel = LogLevel.Verbose; + Logger.LogLevel = LogLevel.Critical; Logger.OnNewLogMessage((date, msg) => { Console.WriteLine($"{date.ToString("HH:mm:ss")} {msg}"); }); @@ -44,6 +44,13 @@ static void Scenario1 () { //attaching the register collection and an automatic poller interf.WithRegisterCollection(registers).WithPoller(); + _ = Task.Factory.StartNew(async () => { + while (true) { + Console.Title = $"Polling Paused: {interf.PollingPaused}, Speed UP: {interf.BytesPerSecondUpstream} B/s, Speed DOWN: {interf.BytesPerSecondDownstream} B/s"; + await Task.Delay(1000); + } + }); + await interf.ConnectAsync( (plcinf) => { @@ -79,14 +86,28 @@ await interf.ConnectAsync( //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) { - Console.WriteLine($"Speed UP: {interf.BytesPerSecondUpstream} B/s"); - Console.WriteLine($"Speed DOWN: {interf.BytesPerSecondDownstream} B/s"); + 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 Task.Delay(1000); } + }); } diff --git a/MewtocolNet/Mewtocol/DynamicInterface.cs b/MewtocolNet/Mewtocol/DynamicInterface.cs index 8f702f7..31f135f 100644 --- a/MewtocolNet/Mewtocol/DynamicInterface.cs +++ b/MewtocolNet/Mewtocol/DynamicInterface.cs @@ -14,15 +14,61 @@ namespace MewtocolNet { /// public partial class MewtocolInterface { + /// + /// True if the auto poller is currently paused + /// + public bool PollingPaused => pollerIsPaused; + internal event Action PolledCycle; - internal bool ContinousReaderRunning; + + internal volatile bool pollerTaskRunning; + internal volatile bool pollerTaskStopped; + internal volatile bool pollerIsPaused; + internal bool usePoller = false; #region Register Polling + /// + /// Kills the poller completely + /// internal void KillPoller () { - ContinousReaderRunning = false; + pollerTaskRunning = false; + pollerTaskStopped = true; + + } + + /// + /// Pauses the polling and waits for the last message to be sent + /// + /// + public async Task PausePollingAsync () { + + if (!pollerTaskRunning) + return; + + pollerTaskRunning = false; + + while (!pollerIsPaused) { + + if (pollerIsPaused) + break; + + await Task.Delay(10); + + } + + pollerTaskRunning = false; + + } + + /// + /// Resumes the polling + /// + public void ResumePolling () { + + pollerTaskRunning = true; } @@ -31,96 +77,107 @@ internal void KillPoller () { /// internal void AttachPoller () { - if (ContinousReaderRunning) + if (pollerTaskRunning) return; Task.Factory.StartNew(async () => { Logger.Log("Poller is attaching", LogLevel.Info, this); - int it = 0; - ContinousReaderRunning = true; + int iteration = 0; - while (ContinousReaderRunning) { + pollerTaskStopped = false; + pollerTaskRunning = true; + pollerIsPaused = false; - if (it >= Registers.Count + 1) { - it = 0; - //invoke cycle polled event - InvokePolledCycleDone(); - continue; - } + while (!pollerTaskStopped) { - if (it >= Registers.Count) { - await GetPLCInfoAsync(); - it++; - continue; - } + while (pollerTaskRunning) { - var reg = Registers[it]; + if (iteration >= Registers.Count + 1) { + iteration = 0; + //invoke cycle polled event + InvokePolledCycleDone(); + continue; + } - if (reg is NRegister shortReg) { - var lastVal = shortReg.Value; - var readout = (await ReadNumRegister(shortReg)).Register.Value; - if (lastVal != readout) { - InvokeRegisterChanged(shortReg); + if (iteration >= Registers.Count) { + await GetPLCInfoAsync(); + iteration++; + continue; } - } - if (reg is NRegister ushortReg) { - var lastVal = ushortReg.Value; - var readout = (await ReadNumRegister(ushortReg)).Register.Value; - if (lastVal != readout) { - InvokeRegisterChanged(ushortReg); + + var reg = Registers[iteration]; + + if (reg is NRegister shortReg) { + var lastVal = shortReg.Value; + var readout = (await ReadNumRegister(shortReg)).Register.Value; + if (lastVal != readout) { + InvokeRegisterChanged(shortReg); + } } - } - if (reg is NRegister intReg) { - var lastVal = intReg.Value; - var readout = (await ReadNumRegister(intReg)).Register.Value; - if (lastVal != readout) { - InvokeRegisterChanged(intReg); + if (reg is NRegister ushortReg) { + var lastVal = ushortReg.Value; + var readout = (await ReadNumRegister(ushortReg)).Register.Value; + if (lastVal != readout) { + InvokeRegisterChanged(ushortReg); + } } - } - if (reg is NRegister uintReg) { - var lastVal = uintReg.Value; - var readout = (await ReadNumRegister(uintReg)).Register.Value; - if (lastVal != readout) { - InvokeRegisterChanged(uintReg); + if (reg is NRegister intReg) { + var lastVal = intReg.Value; + var readout = (await ReadNumRegister(intReg)).Register.Value; + if (lastVal != readout) { + InvokeRegisterChanged(intReg); + } } - } - if (reg is NRegister floatReg) { - var lastVal = floatReg.Value; - var readout = (await ReadNumRegister(floatReg)).Register.Value; - if (lastVal != readout) { - InvokeRegisterChanged(floatReg); + if (reg is NRegister uintReg) { + var lastVal = uintReg.Value; + var readout = (await ReadNumRegister(uintReg)).Register.Value; + if (lastVal != readout) { + InvokeRegisterChanged(uintReg); + } } - } - if (reg is NRegister tsReg) { - var lastVal = tsReg.Value; - var readout = (await ReadNumRegister(tsReg)).Register.Value; - if (lastVal != readout) { - InvokeRegisterChanged(tsReg); + if (reg is NRegister floatReg) { + var lastVal = floatReg.Value; + var readout = (await ReadNumRegister(floatReg)).Register.Value; + if (lastVal != readout) { + InvokeRegisterChanged(floatReg); + } } - } - if (reg is BRegister boolReg) { - var lastVal = boolReg.Value; - var readout = (await ReadBoolRegister(boolReg)).Register.Value; - if (lastVal != readout) { - InvokeRegisterChanged(boolReg); + if (reg is NRegister tsReg) { + var lastVal = tsReg.Value; + var readout = (await ReadNumRegister(tsReg)).Register.Value; + if (lastVal != readout) { + InvokeRegisterChanged(tsReg); + } } - } - if (reg is SRegister stringReg) { - var lastVal = stringReg.Value; - var readout = (await ReadStringRegister(stringReg)).Register.Value; - if (lastVal != readout) { - InvokeRegisterChanged(stringReg); + 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++; - it++; + await Task.Delay(PollerDelayMs); - await Task.Delay(PollerDelayMs); + } + + pollerIsPaused = !pollerTaskRunning; } + pollerIsPaused = false; + }); } diff --git a/MewtocolNet/MewtocolNet.csproj b/MewtocolNet/MewtocolNet.csproj index b116755..dc1cd4e 100644 --- a/MewtocolNet/MewtocolNet.csproj +++ b/MewtocolNet/MewtocolNet.csproj @@ -2,7 +2,7 @@ netstandard2.0 MewtocolNet - 0.5.5 + 0.5.6 Felix Weiss Womed true