How to subscribe to OPC UA data changes in FSharp

From OPC Labs Knowledge Base
Jump to navigation Jump to search
// 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()

    // The callback is a delegate that displays the value
    let handle = 
            new UAEndpointDescriptor(""), // or "opc.tcp://"
            new UANodeDescriptor("nsu=;i=10853"),
            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("Waiting for 2 seconds...")
    System.Threading.Thread.Sleep(2 * 1000)



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.