Skip to content

Commit

Permalink
Merge pull request #46 from ChrisPulman/FixWatchdog
Browse files Browse the repository at this point in the history
Fix for Watchdog
  • Loading branch information
ChrisPulman authored Jan 18, 2024
2 parents 7dbdba3 + 798a64e commit 4c9317b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.3.0",
"version": "1.3.1",
"publicReleaseRefSpec": [
"^refs/heads/master$",
"^refs/heads/main$"
Expand Down
2 changes: 1 addition & 1 deletion src/S7PlcRx/IRxS7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public interface IRxS7 : ICancelable
/// Gets the watch dog address.
/// </summary>
/// <value>The watch dog address.</value>
string WatchDogAddress { get; }
string? WatchDogAddress { get; }

/// <summary>
/// Gets or sets the watch dog value to write.
Expand Down
30 changes: 19 additions & 11 deletions src/S7PlcRx/RxS7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,16 @@ public class RxS7 : IRxS7
/// <param name="ip">The ip.</param>
/// <param name="rack">The rack.</param>
/// <param name="slot">The slot.</param>
/// <param name="watchDogAddress">The watch dog address.</param>
/// <param name="interval">The interval to observe on.</param>
/// <param name="watchDogAddress">The watch dog address, WatchDogAddress must be a DBW address.</param>
/// <param name="interval">The interval to observe on in milliseconds.</param>
/// <param name="watchDogValueToWrite">The watch dog value to write.</param>
/// <param name="watchDogInterval">The watch dog interval.</param>
/// <param name="watchDogInterval">The watch dog interval in seconds.</param>
public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAddress = null, double interval = 100, ushort watchDogValueToWrite = 4500, int watchDogInterval = 10)
{
PLCType = type;
IP = ip;
Rack = rack;
Slot = slot;
WatchDogAddress = watchDogAddress!;

// Create an observable socket
_socketRx = new(IP, type, rack, slot);
Expand All @@ -68,6 +67,17 @@ public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAdd

if (!string.IsNullOrWhiteSpace(watchDogAddress))
{
if (!watchDogAddress.Contains("DBW"))
{
throw new ArgumentException("WatchDogAddress must be a DBW address.", nameof(watchDogAddress));
}

WatchDogAddress = watchDogAddress;
if (watchDogInterval < 1)
{
throw new ArgumentOutOfRangeException(nameof(watchDogInterval), "WatchDogInterval must be greater than 0.");
}

WatchDogWritingTime = watchDogInterval;
WatchDogValueToWrite = watchDogValueToWrite;
_disposables.Add(WatchDogObservable().Subscribe());
Expand Down Expand Up @@ -185,7 +195,7 @@ public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAdd
/// Gets the watch dog address.
/// </summary>
/// <value>The watch dog address.</value>
public string WatchDogAddress { get; }
public string? WatchDogAddress { get; }

/// <summary>
/// Gets or sets the watch dog value to write.
Expand Down Expand Up @@ -1242,22 +1252,20 @@ private IObservable<Unit> WatchDogObservable() =>
{
// disable watchdog if not defined
obs.OnCompleted();
return Task.CompletedTask;
return Disposable.Empty;
}

// Setup the watchdog
var wd = new Tag("WatchDog", WatchDogAddress, WatchDogValueToWrite, typeof(ushort));
this.AddUpdateTagItem<ushort>("WatchDog", WatchDogAddress).SetTagPollIng(false);

AddUpdateTagItem(wd);
var tim = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(WatchDogWritingTime)).Retry().Subscribe(_ =>
{
if (IsConnectedValue)
{
wd.Value = WatchDogValueToWrite;
_pLCRequestSubject.OnNext(new PLCRequest(PLCRequestType.Write, wd));
Value("WatchDog", WatchDogValueToWrite);
if (ShowWatchDogWriting)
{
_status.OnNext($"{DateTime.Now} - Watch Dog writing {wd.Value} to {wd.Address}");
_status.OnNext($"{DateTime.Now} - WatchDog writing {WatchDogValueToWrite} to {WatchDogAddress}");
}
}
});
Expand Down

0 comments on commit 4c9317b

Please sign in to comment.