Difference between revisions of "How to subscribe to OPC UA data changes in FSharp"

From OPC Labs Knowledge Base
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Category:.NET]] [[Category:Console application]] [[Category:F-sharp]] [[Category:OPC UA]]
+
[[Category:.NET]] [[Category:Console application]] [[Category:Example]] [[Category:F-sharp]] [[Category:How to]] [[Category:OPC UA]]
 
<syntaxhighlight lang="fsharp">
 
<syntaxhighlight lang="fsharp">
 
// This example shows how to subscribe to changes of a single monitored item, and display the value of the item with each change
 
// This example shows how to subscribe to changes of a single monitored item, and display the value of the item with each change
Line 16: Line 16:
  
 
     Console.WriteLine("Subscribing...");
 
     Console.WriteLine("Subscribing...");
     // The callback is a delegate the displays the value
+
     // The callback is a delegate that displays the value
 
     let handle =  
 
     let handle =  
 
         easyUAClient.SubscribeDataChange(
 
         easyUAClient.SubscribeDataChange(

Latest revision as of 18:22, 8 February 2018

// 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.