Subscribe to OPC UA Node

2 views (last 30 days)
Oliver May
Oliver May on 29 Dec 2020
Commented: Srijith Vijay on 13 Mar 2024
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 Comment
Thomas Pursche
Thomas Pursche on 10 May 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

Sign in to comment.

Answers (2)

Nelson Igbokwe
Nelson Igbokwe on 25 Feb 2021
I have the exact same question

MathWorks Industrial Communication Toolbox Team
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 Comments
Gernot Reichl
Gernot Reichl on 12 Mar 2024
Edited: Gernot Reichl on 12 Mar 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
Srijith Vijay on 13 Mar 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!

Sign in to comment.

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!