From 43c7f72ac41a8296a808f79ad33d6c262bb7fe16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Wei=C3=9F?= <72068105+Sandoun@users.noreply.github.com> Date: Fri, 21 Oct 2022 11:56:58 +0200 Subject: [PATCH] Added auto register reset to default typed value on disconnect --- Examples/Program.cs | 19 +++++++++-- MewtocolNet/Mewtocol/DynamicInterface.cs | 11 +++++++ MewtocolNet/Mewtocol/MewtocolInterface.cs | 11 +++++++ MewtocolNet/Mewtocol/Subregisters/Register.cs | 32 +++++++++++++++++++ MewtocolNet/MewtocolNet.csproj | 2 +- 5 files changed, 72 insertions(+), 3 deletions(-) diff --git a/Examples/Program.cs b/Examples/Program.cs index b2bccbb..0327436 100644 --- a/Examples/Program.cs +++ b/Examples/Program.cs @@ -34,7 +34,7 @@ class Program { Task.Factory.StartNew(async () => { //attaching the logger - Logger.LogLevel = LogLevel.Critical; + Logger.LogLevel = LogLevel.Verbose; Logger.OnNewLogMessage((date, msg) => { Console.WriteLine($"{date.ToString("HH:mm:ss")} {msg}"); }); @@ -50,6 +50,7 @@ class Program { while (true) { if (isProgressReadout) continue; Console.Title = $"Polling Paused: {interf.PollingPaused}, " + + $"Poller active: {interf.PollerActive}, " + $"Speed UP: {interf.BytesPerSecondUpstream} B/s, " + $"Speed DOWN: {interf.BytesPerSecondDownstream} B/s, " + $"Poll delay: {interf.PollerDelayMs} ms, " + @@ -58,7 +59,21 @@ class Program { } }); - await interf.ConnectAsync((plcinf) => AfterConnect(interf, registers)); + //await interf.ConnectAsync((plcinf) => AfterConnect(interf, registers)); + + bool flip = false; + while(true) { + + if(!flip) { + await interf.ConnectAsync(); + } else { + interf.Disconnect(); + } + + flip = !flip; + await Task.Delay(5000); + + } }); diff --git a/MewtocolNet/Mewtocol/DynamicInterface.cs b/MewtocolNet/Mewtocol/DynamicInterface.cs index 178128b..0162b94 100644 --- a/MewtocolNet/Mewtocol/DynamicInterface.cs +++ b/MewtocolNet/Mewtocol/DynamicInterface.cs @@ -19,11 +19,17 @@ namespace MewtocolNet { /// public bool PollingPaused => pollerIsPaused; + /// + /// True if the poller is actvice (can be paused) + /// + public bool PollerActive => !pollerTaskStopped; + internal event Action PolledCycle; internal volatile bool pollerTaskRunning; internal volatile bool pollerTaskStopped; internal volatile bool pollerIsPaused; + internal volatile bool pollerFirstCycle = false; internal bool usePoller = false; @@ -37,6 +43,8 @@ namespace MewtocolNet { pollerTaskRunning = false; pollerTaskStopped = true; + ClearRegisterVals(); + } /// @@ -80,6 +88,8 @@ namespace MewtocolNet { if (pollerTaskRunning) return; + pollerFirstCycle = true; + Task.Factory.StartNew(async () => { Logger.Log("Poller is attaching", LogLevel.Info, this); @@ -167,6 +177,7 @@ namespace MewtocolNet { } iteration++; + pollerFirstCycle = false; await Task.Delay(pollerDelayMs); diff --git a/MewtocolNet/Mewtocol/MewtocolInterface.cs b/MewtocolNet/Mewtocol/MewtocolInterface.cs index 543ddf4..3fcf273 100644 --- a/MewtocolNet/Mewtocol/MewtocolInterface.cs +++ b/MewtocolNet/Mewtocol/MewtocolInterface.cs @@ -395,6 +395,17 @@ namespace MewtocolNet { } + private void ClearRegisterVals () { + + for (int i = 0; i < Registers.Count; i++) { + + var reg = Registers[i]; + reg.ClearValue(); + + } + + } + #endregion #region Register Collection diff --git a/MewtocolNet/Mewtocol/Subregisters/Register.cs b/MewtocolNet/Mewtocol/Subregisters/Register.cs index 8d46524..2af8c46 100644 --- a/MewtocolNet/Mewtocol/Subregisters/Register.cs +++ b/MewtocolNet/Mewtocol/Subregisters/Register.cs @@ -94,6 +94,38 @@ namespace MewtocolNet.Registers { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value")); } + internal void ClearValue () { + + if (enumType != null && this is NRegister intEnumReg) { + intEnumReg.SetValueFromPLC((int)0); + } + if (this is NRegister shortReg) { + shortReg.SetValueFromPLC((short)0); + } + if (this is NRegister ushortReg) { + ushortReg.SetValueFromPLC((ushort)0); + } + if (this is NRegister intReg) { + intReg.SetValueFromPLC((int)0); + } + if (this is NRegister uintReg) { + uintReg.SetValueFromPLC((uint)0); + } + if (this is NRegister floatReg) { + floatReg.SetValueFromPLC((float)0); + } + if (this is NRegister tsReg) { + tsReg.SetValueFromPLC(TimeSpan.Zero); + } + if (this is BRegister boolReg) { + boolReg.SetValueFromPLC(false); + } + if (this is SRegister stringReg) { + stringReg.SetValueFromPLC(null); + } + + } + /// /// Gets the starting memory are either numeric or A,B,C,D etc for special areas like inputs /// diff --git a/MewtocolNet/MewtocolNet.csproj b/MewtocolNet/MewtocolNet.csproj index 900a621..d0a3792 100644 --- a/MewtocolNet/MewtocolNet.csproj +++ b/MewtocolNet/MewtocolNet.csproj @@ -2,7 +2,7 @@ netstandard2.0 Mewtocol.NET - 0.6.1 + 0.6.2 Felix Weiss Womed true