Skip to content

Commit

Permalink
add sync read function for beckhoff
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarresi committed May 19, 2020
1 parent 028961e commit 30bbf20
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion TFU002/TFU002.Logic/GatewayNotificationsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Serilog;
using Sharp7.Rx.Enums;
using Sharp7.Rx.Interfaces;
using TFU002.Interfaces.Extensions;
using TwinCAT.Ads;
using TwinCAT.Ads.Reactive;
using TwinCAT.TypeSystem;
Expand Down Expand Up @@ -66,9 +67,11 @@ public static IDisposable GetTypedBeckhoffNotification(this AdsClient beckhoff,
{
if (type == typeof(byte[]))
{
return beckhoff.WhenNotification<byte[]>(symbol.InstancePath, new NotificationSettings(AdsTransMode.Cyclic, 1000, 1000) )
return Observable.Timer(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1))
.Select(_ => beckhoff.ReadVariable<byte[]>(symbol))
.Do(value => Log.Logger.Debug($"Writing {symbol.InstancePath} to {address}: {ByteToString(value)}"))
.SelectMany(value => plc.Write(address, value))
.Retry()
.Subscribe();
}

Expand Down Expand Up @@ -139,5 +142,18 @@ private static async Task<Unit> Write<T>(this IPlc plc, string address, T value)
}
return Unit.Default;
}

private static T ReadVariable<T>(this AdsClient beckhoff, ISymbol symbol)
{
try
{
return (T) beckhoff.ReadValue(symbol);
}
catch (Exception e)
{
Log.Logger.Error(e, $"Error while reading symbol {symbol.InstancePath}");
throw;
}
}
}
}

0 comments on commit 30bbf20

Please sign in to comment.