How to subscribe to OPC UA data changes in FSharp
From OPC Labs Knowledge Base
(Redirected from How to subscribe to OPC UA data changes in F)
// This example shows how to subscribe to changes of a single monitored item, and display the value of the item with each change
// using a callback method that is provided as a function delegate.
module _EasyUAClient.SubscribeDataChange
open OpcLabs.EasyOpc.UA
open OpcLabs.EasyOpc.UA.OperationModel
open System
let CallbackFunction =
// Instantiate the client object
let easyUAClient = new EasyUAClient()
Console.WriteLine("Subscribing...");
// The callback is a delegate that displays the value
let handle =
easyUAClient.SubscribeDataChange(
new UAEndpointDescriptor("http://opcua.demo-this.com:51211/UA/SampleServer"), // or "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer"
new UANodeDescriptor("nsu=http://test.org/UA/Data/;i=10853"),
1000,
new EasyUADataChangeNotificationEventHandler(fun sender eventArgs -> Console.WriteLine(eventArgs.AttributeData.Value:obj)))
// Remark: Production code would check eventArgs.Exception before accessing eventArgs.AttributeData.
Console.WriteLine("Processing data change events for 10 seconds...")
System.Threading.Thread.Sleep(10 * 1000)
Console.WriteLine("Unsubscribing...")
easyUAClient.UnsubscribeAllMonitoredItems()
Console.WriteLine("Waiting for 2 seconds...")
System.Threading.Thread.Sleep(2 * 1000)
Usage:
_EasyUAClient.SubscribeDataChange.CallbackFunction
Ships as part of the UADocExamples project in the F# examples solution with QuickOPC since version 5.41. Use the example from the product for an up-to-date code.