Skip to content

Commit

Permalink
Added auto register reset to default typed value on disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandoun committed Oct 21, 2022
1 parent 2e35ed8 commit 43c7f72
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
19 changes: 17 additions & 2 deletions Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static void Scenario1 () {
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}");
});
Expand All @@ -50,6 +50,7 @@ static void Scenario1 () {
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, " +
Expand All @@ -58,7 +59,21 @@ static void Scenario1 () {
}
});

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);

}

});

Expand Down
11 changes: 11 additions & 0 deletions MewtocolNet/Mewtocol/DynamicInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ public partial class MewtocolInterface {
/// </summary>
public bool PollingPaused => pollerIsPaused;

/// <summary>
/// True if the poller is actvice (can be paused)
/// </summary>
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;

Expand All @@ -37,6 +43,8 @@ internal void KillPoller () {
pollerTaskRunning = false;
pollerTaskStopped = true;

ClearRegisterVals();

}

/// <summary>
Expand Down Expand Up @@ -80,6 +88,8 @@ internal void AttachPoller () {
if (pollerTaskRunning)
return;

pollerFirstCycle = true;

Task.Factory.StartNew(async () => {

Logger.Log("Poller is attaching", LogLevel.Info, this);
Expand Down Expand Up @@ -167,6 +177,7 @@ internal void AttachPoller () {
}

iteration++;
pollerFirstCycle = false;

await Task.Delay(pollerDelayMs);

Expand Down
11 changes: 11 additions & 0 deletions MewtocolNet/Mewtocol/MewtocolInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,17 @@ private void OnMajorSocketExceptionWhileConnected () {

}

private void ClearRegisterVals () {

for (int i = 0; i < Registers.Count; i++) {

var reg = Registers[i];
reg.ClearValue();

}

}

#endregion

#region Register Collection
Expand Down
32 changes: 32 additions & 0 deletions MewtocolNet/Mewtocol/Subregisters/Register.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,38 @@ internal void TriggerNotifyChange () {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
}

internal void ClearValue () {

if (enumType != null && this is NRegister<int> intEnumReg) {
intEnumReg.SetValueFromPLC((int)0);
}
if (this is NRegister<short> shortReg) {
shortReg.SetValueFromPLC((short)0);
}
if (this is NRegister<ushort> ushortReg) {
ushortReg.SetValueFromPLC((ushort)0);
}
if (this is NRegister<int> intReg) {
intReg.SetValueFromPLC((int)0);
}
if (this is NRegister<uint> uintReg) {
uintReg.SetValueFromPLC((uint)0);
}
if (this is NRegister<float> floatReg) {
floatReg.SetValueFromPLC((float)0);
}
if (this is NRegister<TimeSpan> tsReg) {
tsReg.SetValueFromPLC(TimeSpan.Zero);
}
if (this is BRegister boolReg) {
boolReg.SetValueFromPLC(false);
}
if (this is SRegister stringReg) {
stringReg.SetValueFromPLC(null);
}

}

/// <summary>
/// Gets the starting memory are either numeric or A,B,C,D etc for special areas like inputs
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion MewtocolNet/MewtocolNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Mewtocol.NET</PackageId>
<Version>0.6.1</Version>
<Version>0.6.2</Version>
<Authors>Felix Weiss</Authors>
<Company>Womed</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down

0 comments on commit 43c7f72

Please sign in to comment.