From 7864915967efc1a2bf772b0f1be37d4cea5a6bdc Mon Sep 17 00:00:00 2001 From: Sandoun Date: Thu, 15 Jun 2023 20:49:14 +0200 Subject: [PATCH] Added auto prop sending - updated readme --- Examples/ExampleScenarios.cs | 14 ++++++++-- MewtocolNet/Mewtocol/DynamicInterface.cs | 7 +++++ .../RegisterCollectionBase.cs | 4 ++- README.md | 26 +++++++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/Examples/ExampleScenarios.cs b/Examples/ExampleScenarios.cs index 428d212..a0ccdf9 100644 --- a/Examples/ExampleScenarios.cs +++ b/Examples/ExampleScenarios.cs @@ -164,9 +164,19 @@ public class ExampleScenarios { await interf.ConnectAsync(); - await interf.SetRegisterAsync(nameof(registers.StartCyclePLC), true); + //use the async method to make sure the cycling is stopped + await interf.SetRegisterAsync(nameof(registers.StartCyclePLC), false); - await Task.Delay(-1); + await Task.Delay(5000); + + //set the register without waiting for it async + registers.StartCyclePLC = true; + + await Task.Delay(5000); + + registers.StartCyclePLC = false; + + await Task.Delay(2000); } diff --git a/MewtocolNet/Mewtocol/DynamicInterface.cs b/MewtocolNet/Mewtocol/DynamicInterface.cs index a4556a3..1fe5054 100644 --- a/MewtocolNet/Mewtocol/DynamicInterface.cs +++ b/MewtocolNet/Mewtocol/DynamicInterface.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using MewtocolNet.Logging; +using MewtocolNet.RegisterAttributes; using MewtocolNet.Registers; namespace MewtocolNet { @@ -194,6 +195,12 @@ namespace MewtocolNet { } + internal void PropertyRegisterWasSet (string propName, object value) { + + SetRegister(propName, value); + + } + #endregion #region Register Adding diff --git a/MewtocolNet/Mewtocol/RegisterAttributes/RegisterCollectionBase.cs b/MewtocolNet/Mewtocol/RegisterAttributes/RegisterCollectionBase.cs index 955a846..4b5f2a5 100644 --- a/MewtocolNet/Mewtocol/RegisterAttributes/RegisterCollectionBase.cs +++ b/MewtocolNet/Mewtocol/RegisterAttributes/RegisterCollectionBase.cs @@ -38,7 +38,9 @@ namespace MewtocolNet.RegisterAttributes /// /// Use this on the setter method of a property to enable automatic property register writing /// - public static void AutoSetter (object value, ref T privateField) { + public void AutoSetter (object value, ref T privateField, [CallerMemberName] string propName = null) { + + PLCInterface.PropertyRegisterWasSet(propName, value); if(value is IRegister reg) { diff --git a/README.md b/README.md index b739e17..b8ef1ae 100644 --- a/README.md +++ b/README.md @@ -143,10 +143,29 @@ await plc.ConnectAsync( ⚠ **Never set a register by setting the property, always use one of the provided methods** +Registers are stored in an underlying layer for automatic handling, each register has a unique name and address. + +Classes that derive from `RegisterCollectionBase` reference these registers automatically using attributes. +All the heavy lifting is done automatically for you, setting this up is described [here](https://github.com/WOmed/MewtocolNet/wiki/Attribute-handled-reading) + +### Asynchronous + +This operations awaits a task to make sure the register was actually set to your desired value before progressing + +```C# +//sets the register to false +await plc.SetRegisterAsync(nameof(registers.TestBool1), false); + +//set the current second to the PLCs TIME register +await plc.SetRegisterAsync(nameof(registers.TestTime), TimeSpan.FromSeconds(DateTime.Now.Second)); +``` + ### Synchronous Sets the register without feedback if it was set +You can use the method to set a register + ```C# //inverts the boolean register plc.SetRegister(nameof(registers.TestBool1), !registers.TestBool1); @@ -158,6 +177,13 @@ plc.SetRegister(nameof(registers.TestTime), TimeSpan.FromSeconds(DateTime.Now.Se plc.SetRegister(nameof(registers.TestString1), "Test"); ``` +or write to a register in your `RegisterCollectionBase` directly (you need to attach a register collection to your interface beforehand) + +```C# +//inverts the boolean register +registers.TestBool1 = true; +``` + You can also set a register by calling its name directly (Must be either in an attached register collection or added to the list manually) Adding registers to a manual list