Skip to content

Commit 7864915

Browse files
author
Sandoun
committed
Added auto prop sending
- updated readme
1 parent 62f4a48 commit 7864915

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

Examples/ExampleScenarios.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,19 @@ public async Task RunEnumsBitwiseAsync () {
164164

165165
await interf.ConnectAsync();
166166

167-
await interf.SetRegisterAsync(nameof(registers.StartCyclePLC), true);
167+
//use the async method to make sure the cycling is stopped
168+
await interf.SetRegisterAsync(nameof(registers.StartCyclePLC), false);
168169

169-
await Task.Delay(-1);
170+
await Task.Delay(5000);
171+
172+
//set the register without waiting for it async
173+
registers.StartCyclePLC = true;
174+
175+
await Task.Delay(5000);
176+
177+
registers.StartCyclePLC = false;
178+
179+
await Task.Delay(2000);
170180

171181
}
172182

MewtocolNet/Mewtocol/DynamicInterface.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading;
77
using System.Threading.Tasks;
88
using MewtocolNet.Logging;
9+
using MewtocolNet.RegisterAttributes;
910
using MewtocolNet.Registers;
1011

1112
namespace MewtocolNet {
@@ -194,6 +195,12 @@ internal void AttachPoller () {
194195

195196
}
196197

198+
internal void PropertyRegisterWasSet (string propName, object value) {
199+
200+
SetRegister(propName, value);
201+
202+
}
203+
197204
#endregion
198205

199206
#region Register Adding

MewtocolNet/Mewtocol/RegisterAttributes/RegisterCollectionBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public void TriggerPropertyChanged (string propertyName = null) {
3838
/// <summary>
3939
/// Use this on the setter method of a property to enable automatic property register writing
4040
/// </summary>
41-
public static void AutoSetter<T> (object value, ref T privateField) {
41+
public void AutoSetter<T> (object value, ref T privateField, [CallerMemberName] string propName = null) {
42+
43+
PLCInterface.PropertyRegisterWasSet(propName, value);
4244

4345
if(value is IRegister reg) {
4446

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,29 @@ await plc.ConnectAsync(
143143

144144
**Never set a register by setting the property, always use one of the provided methods**
145145

146+
Registers are stored in an underlying layer for automatic handling, each register has a unique name and address.
147+
148+
Classes that derive from `RegisterCollectionBase` reference these registers automatically using attributes.
149+
All the heavy lifting is done automatically for you, setting this up is described [here](https://github.com/WOmed/MewtocolNet/wiki/Attribute-handled-reading)
150+
151+
### Asynchronous
152+
153+
This operations awaits a task to make sure the register was actually set to your desired value before progressing
154+
155+
```C#
156+
//sets the register to false
157+
await plc.SetRegisterAsync(nameof(registers.TestBool1), false);
158+
159+
//set the current second to the PLCs TIME register
160+
await plc.SetRegisterAsync(nameof(registers.TestTime), TimeSpan.FromSeconds(DateTime.Now.Second));
161+
```
162+
146163
### Synchronous
147164

148165
Sets the register without feedback if it was set
149166

167+
You can use the method to set a register
168+
150169
```C#
151170
//inverts the boolean register
152171
plc.SetRegister(nameof(registers.TestBool1), !registers.TestBool1);
@@ -158,6 +177,13 @@ plc.SetRegister(nameof(registers.TestTime), TimeSpan.FromSeconds(DateTime.Now.Se
158177
plc.SetRegister(nameof(registers.TestString1), "Test");
159178
```
160179

180+
or write to a register in your `RegisterCollectionBase` directly (you need to attach a register collection to your interface beforehand)
181+
182+
```C#
183+
//inverts the boolean register
184+
registers.TestBool1 = true;
185+
```
186+
161187
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)
162188

163189
Adding registers to a manual list

0 commit comments

Comments
 (0)