Added auto prop sending

- updated readme
This commit is contained in:
Sandoun
2023-06-15 20:49:14 +02:00
parent 62f4a48bf9
commit 7864915967
4 changed files with 48 additions and 3 deletions

View File

@@ -164,9 +164,19 @@ public class ExampleScenarios {
await interf.ConnectAsync(); 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);
} }

View File

@@ -6,6 +6,7 @@ using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MewtocolNet.Logging; using MewtocolNet.Logging;
using MewtocolNet.RegisterAttributes;
using MewtocolNet.Registers; using MewtocolNet.Registers;
namespace MewtocolNet { namespace MewtocolNet {
@@ -194,6 +195,12 @@ namespace MewtocolNet {
} }
internal void PropertyRegisterWasSet (string propName, object value) {
SetRegister(propName, value);
}
#endregion #endregion
#region Register Adding #region Register Adding

View File

@@ -38,7 +38,9 @@ namespace MewtocolNet.RegisterAttributes
/// <summary> /// <summary>
/// Use this on the setter method of a property to enable automatic property register writing /// Use this on the setter method of a property to enable automatic property register writing
/// </summary> /// </summary>
public static void AutoSetter<T> (object value, ref T privateField) { public void AutoSetter<T> (object value, ref T privateField, [CallerMemberName] string propName = null) {
PLCInterface.PropertyRegisterWasSet(propName, value);
if(value is IRegister reg) { if(value is IRegister reg) {

View File

@@ -143,10 +143,29 @@ await plc.ConnectAsync(
⚠ **Never set a register by setting the property, always use one of the provided methods** ⚠ **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 ### Synchronous
Sets the register without feedback if it was set Sets the register without feedback if it was set
You can use the method to set a register
```C# ```C#
//inverts the boolean register //inverts the boolean register
plc.SetRegister(nameof(registers.TestBool1), !registers.TestBool1); 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"); 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) 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 Adding registers to a manual list