Subscribe to OPC UA Node
    3 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
Hi, 
An attractive feature of OPC UA is the ability to subscribe to a node and receieve updates using events rather than polling. 
Follwoing the examples in the OPC Toolbox documentation I can connect to the server and pull a values from nodes but i can't seem to find any functions for subscribing to a node and plot values in real-time?
Is this feature not available in Matlab or do i need to invoke another function from a different package or toolkit?
Many thanks, 
Ollie
1 Kommentar
  Thomas Pursche
 am 10 Mai 2022
				Hi Ollie,
I am also working a lot with the OPC UA communication. It seems that at the moment there is no option to subscribe to groups or nodes within the OPC UA server like in OPC DA. When you have a look in the opc.ua Node class description https://de.mathworks.com/help/releases/R2018b/opc/ug/opc.ua.node-class.html?s_tid=answers_rc2-2_p5_MLT you see there is only polling inplemented at the moment.
I am working currently on the option to use a C# assembly to include the missing features.
Best regards,
Thomas
Antworten (2)
  MathWorks Industrial Communication Toolbox Team
    
 am 25 Sep. 2023
        Starting R2023b, you can now subscribe to data change events in OPC UA nodes using the "subscribe" function in the Industrial Communication Toolbox.
Here is an example of how to use this feature:
2 Kommentare
  Gernot Reichl
 am 12 Mär. 2024
				
      Bearbeitet: Gernot Reichl
 am 12 Mär. 2024
  
			Hello ICTb Team!
Is there a possibility to get the data (values, quality, timestamp)? I find the data in the notification object, but I can't get it out of the callback functions. Callback functions do not have outputs. Is there a way to hook the data to the subscription object?
function mycallback(subObj,notification,userdata)
    % Get the nodes that have data change and the corresponding data.
    % notification is an object containing the data values received from the
    % server and node names, in response to the subscription request.
    nodes = [notification(:).Node]; % Array of structures with fields: Name, Namespace, Identifier.
    data = [notification(:).Data];  % Array of structures with fields: Value, Quality, Timestamp.
end
thank you
  Srijith Vijay
    
 am 13 Mär. 2024
				Hi Gernot,
In MATLAB, callback functions are typically used to respond to events and generally doesn't involve returning outputs as with standard function calls. However, there are ways to acheive similar outcomes as returning a value from a callback based on the structure of your application.
- You can use a global variable to store data in the notification object, but this is generally discouraged due to potential issues with code readability and maintainability.
 - The preferred approach is to use object oriented programming to store the node value from notification object into a class property. For example, here is how you would retreive the latest values from the callback function into a class propery "NodeValues" that is a dictionary:
 
classdef OPCClass < handle
    properties
        OPCObj          (1,1) opc.ua.Client
        SubscriptionObj (1,1)
        NodeValues      (1,1) dictionary
    end
    methods
        function this = OPCClass(discoveryURL)
            % Constructor - creates an OPC UA Client and connects to the
            % server.
            this.OPCObj = opcua(discoveryURL);
            connect(this.OPCObj);
        end
        function subscribeToNodes(this)
            % Get nodes to subscribe
            nodes = browseNamespace(this.OPCObj);
            % Create subscription
            if ~isempty(nodes)
                this.SubscriptionObj = subscribe(this.OPCObj,nodes,@this.DataChangedFcn);
            end
        end
    end
    methods(Access = private)
        function DataChangedFcn(this,subscriptionObj,notification)
            % Get nodes with data change and corresponding value in these
            % nodes.
            nodes = [notification(:).Node];
            data = [notification(:).Data];
            % Store node and value in class property.
            this.NodeValues(nodes(:).Name)=[data(:).Value];
        end
    end
end
Hope this helps!
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!