Skip to content

Commit 09f4da5

Browse files
author
Felix Weiß
committed
Made registers use the IRegister interface
- cleanup and refactoring - fully implemented auto prop register generator unit tests #4 - added plc test program c30 fpx-h - fixed bitarray setback - cleaned up examples and added new ones with addition of attributes for later additions
1 parent 6ca8e9d commit 09f4da5

23 files changed

+1447
-780
lines changed

Examples/ExampleScenarios.cs

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
using MewtocolNet.Logging;
2+
using MewtocolNet;
3+
using System;
4+
using System.Reflection;
5+
using System.Threading.Tasks;
6+
using System.Collections;
7+
8+
namespace Examples;
9+
10+
public class ExampleScenarios {
11+
12+
public static bool MewtocolLoggerEnabled = false;
13+
14+
public void SetupLogger () {
15+
16+
//attaching the logger
17+
Logger.LogLevel = LogLevel.Verbose;
18+
Logger.OnNewLogMessage((date, msg) => {
19+
if(MewtocolLoggerEnabled)
20+
Console.WriteLine($"{date.ToString("HH:mm:ss")} {msg}");
21+
});
22+
23+
}
24+
25+
[Scenario("Permament connection with poller")]
26+
public async Task RunCyclicPollerAsync () {
27+
28+
Console.WriteLine("Starting poller scenario");
29+
30+
int runTime = 10000;
31+
int remainingTime = runTime;
32+
33+
//setting up a new PLC interface and register collection
34+
MewtocolInterface interf = new MewtocolInterface("192.168.115.210");
35+
TestRegisters registers = new TestRegisters();
36+
37+
//attaching the register collection and an automatic poller
38+
interf.WithRegisterCollection(registers).WithPoller();
39+
40+
await interf.ConnectAsync();
41+
42+
_ = Task.Factory.StartNew(async () => {
43+
44+
while (interf.IsConnected) {
45+
46+
//flip the bool register each tick and wait for it to be registered
47+
await interf.SetRegisterAsync(nameof(registers.TestBool1), !registers.TestBool1);
48+
49+
Console.Title = $"Polling Paused: {interf.PollingPaused}, " +
50+
$"Poller active: {interf.PollerActive}, " +
51+
$"Speed UP: {interf.BytesPerSecondUpstream} B/s, " +
52+
$"Speed DOWN: {interf.BytesPerSecondDownstream} B/s, " +
53+
$"Poll delay: {interf.PollerDelayMs} ms, " +
54+
$"Queued MSGs: {interf.QueuedMessages}";
55+
56+
Console.Clear();
57+
Console.WriteLine("Underlying registers on tick: \n");
58+
59+
foreach (var register in interf.Registers) {
60+
61+
Console.WriteLine($"{register.GetCombinedName()} / {register.GetRegisterPLCName()} - Value: {register.GetValueString()}");
62+
63+
}
64+
65+
Console.WriteLine($"{registers.TestBool1}");
66+
Console.WriteLine($"{registers.TestDuplicate}");
67+
68+
remainingTime -= 1000;
69+
70+
Console.WriteLine($"\nStopping in: {remainingTime}ms");
71+
72+
await Task.Delay(1000);
73+
74+
}
75+
76+
});
77+
78+
await Task.Delay(runTime);
79+
interf.Disconnect();
80+
81+
}
82+
83+
[Scenario("Dispose and disconnect connection")]
84+
public async Task RunDisposalAndDisconnectAsync () {
85+
86+
//automatic disposal
87+
using (var interf = new MewtocolInterface("192.168.115.210")) {
88+
89+
await interf.ConnectAsync();
90+
91+
if (interf.IsConnected) {
92+
93+
Console.WriteLine("Opened connection");
94+
95+
await Task.Delay(5000);
96+
97+
}
98+
99+
}
100+
101+
Console.WriteLine("Disposed, closed connection");
102+
103+
//manual close
104+
var interf2 = new MewtocolInterface("192.168.115.210");
105+
106+
await interf2.ConnectAsync();
107+
108+
if (interf2.IsConnected) {
109+
110+
Console.WriteLine("Opened connection");
111+
112+
await Task.Delay(5000);
113+
114+
}
115+
116+
interf2.Disconnect();
117+
118+
Console.WriteLine("Disconnected, closed connection");
119+
120+
}
121+
122+
[Scenario("Test auto enums and bitwise, needs the example program from MewtocolNet/PLC_Test")]
123+
public async Task RunEnumsBitwiseAsync () {
124+
125+
Console.WriteLine("Starting auto enums and bitwise");
126+
127+
//setting up a new PLC interface and register collection
128+
MewtocolInterface interf = new MewtocolInterface("192.168.115.210");
129+
TestRegistersEnumBitwise registers = new TestRegistersEnumBitwise();
130+
131+
//attaching the register collection and an automatic poller
132+
interf.WithRegisterCollection(registers).WithPoller();
133+
134+
registers.PropertyChanged += (s, e) => {
135+
136+
Console.Clear();
137+
138+
var props = registers.GetType().GetProperties();
139+
140+
foreach (var prop in props) {
141+
142+
var val = prop.GetValue(registers);
143+
string printVal = val?.ToString() ?? "null";
144+
145+
if (val is BitArray bitarr) {
146+
printVal = bitarr.ToBitString();
147+
}
148+
149+
Console.Write($"{prop.Name} - ");
150+
151+
if(printVal == "True") {
152+
Console.ForegroundColor = ConsoleColor.Green;
153+
}
154+
155+
Console.Write($"{printVal}");
156+
157+
Console.ResetColor();
158+
159+
Console.WriteLine();
160+
161+
}
162+
163+
};
164+
165+
await interf.ConnectAsync();
166+
167+
await interf.SetRegisterAsync(nameof(registers.StartCyclePLC), true);
168+
169+
await Task.Delay(-1);
170+
171+
}
172+
173+
}

0 commit comments

Comments
 (0)