Last active
January 3, 2024 11:27
-
-
Save MuhammadSulaiman001/104c3e1d5cf5b30ef7d87cbbb0b06b2a to your computer and use it in GitHub Desktop.
Update ui-bound variables via modern-reactive Twincat libs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using TwinCAT.Ads; | |
using TwinCAT.Ads.Reactive; | |
namespace Project.CncModule.Utils; | |
public static class AdsExtensions | |
{ | |
private static readonly HashSet<IDisposable> AdsDisposables = new(); | |
/// must be called on App Exit to avoid memory leaks.. | |
public static void DisposeAllSubscription(this AdsClient adsClient) | |
{ | |
foreach (var disposable in AdsDisposables) disposable.Dispose(); | |
} | |
/// 1. at some point (app startup) you must have been done a similar initialization: <para/> | |
/// public static AdsClient adsClient = new(); <para/> | |
/// adsClient.Connect(AmsNetId.Local, PortNo); <para/> | |
/// 2. subscribing to twincat variable + update the ui bound to PropXPox variable: <para/> | |
/// adsClient.AddSubscriber<![CDATA[<float>]]>("GVL.stHMIRead.fXPos",xPos => PropXPos = xPos); | |
public static void AddSubscriber<T>(this AdsClient adsClient, | |
string instancePath, | |
Action<T> onValueChanged, | |
bool notifyImmediately = true) | |
{ | |
// https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_adsnetref/7312590987.html | |
AdsDisposables.Add( | |
adsClient | |
.WhenNotification<T>(instancePath, | |
notifyImmediately | |
? NotificationSettings.ImmediatelyOnChange | |
: NotificationSettings.Default) | |
.Subscribe(onValueChanged)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment