Online Forums
Technical support is provided through Support Forums below. Anybody can view them; you need to Register/Login to our site (see links in upper right corner) in order to Post questions. You do not have to be a licensed user of our product.
Please read Rules for forum posts before reporting your issue or asking a question. OPC Labs team is actively monitoring the forums, and replies as soon as possible. Various technical information can also be found in our Knowledge Base. For your convenience, we have also assembled a Frequently Asked Questions page.
Do not use the Contact page for technical issues.
- Forum
- Discussions
- QuickOPC-UA in .NET
- Reading, Writing, Subscriptions
- Monitoring all variables in a DB
Monitoring all variables in a DB
// This example shows how to subscribe to changes of all data variables under a specified object in OPC UA address space.
using System;
using System.Linq;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.AddressSpace;
using OpcLabs.EasyOpc.UA.OperationModel;
namespace UADocExamples._EasyUAClient
{
partial class SubscribeMultipleMonitoredItems
{
public static void AllInObject()
{
UAEndpointDescriptor endpointDescriptor =
"http://opcua.demo-this.com:51211/UA/SampleServer"; // or "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer"
// Instantiate the client object and hook events
var client = new EasyUAClient();
client.DataChangeNotification += client_DataChangeNotification_AllInObject;
// Obtain variables under "Scalar" node
Console.WriteLine("Browsing...");
UANodeElementCollection nodeElementCollection = client.BrowseDataVariables(endpointDescriptor,
"nsu=http://test.org/UA/Data/;ns=2;i=10787");
// Create array with monitored item arguments
EasyUAMonitoredItemArguments[] monitoredItemArgumentsArray = nodeElementCollection
.Select(element => new EasyUAMonitoredItemArguments(null, endpointDescriptor, element))
.ToArray();
Console.WriteLine("Subscribing...");
client.SubscribeMultipleMonitoredItems(monitoredItemArgumentsArray);
Console.WriteLine("Processing monitored item changed events for 20 seconds...");
System.Threading.Thread.Sleep(20 * 1000);
Console.WriteLine("Unsubscribing...");
client.UnsubscribeAllMonitoredItems();
Console.WriteLine("Waiting for 5 seconds...");
System.Threading.Thread.Sleep(5 * 1000);
}
static void client_DataChangeNotification_AllInObject(object sender, EasyUADataChangeNotificationEventArgs e)
{
// Display value
// Remark: Production code would check e.Exception before accessing e.AttributeData.
Console.WriteLine("{0}: {1}", e.Arguments.NodeDescriptor, e.AttributeData.Value);
}
}
}
Best regards
Please Log in or Create an account to join the conversation.
I was just curious because for my example I might have 1000 variables I want to monitor in a single DB. With your method I would have to enter 1000 lines of code one for each variable. The other would take maybe 6 lines of code to accomplish by browsing for the nodes then using the foreach loop to create each entry.
Also if the DB is modified I will have to modify my code to include the new variable. With a foreach if a new variable is added it will dynamically be included.
Anyways just curious if it was possible I really like your product.
Code Example:
public Subscription AddSubscription(string Startnodeid, string SubDisplayName, int PubInt, uint KeepAlive, uint LifeCount, uint MaxNotifyPerPublish, int SamplingInt, List<string> mylist)
{
Subscription subscription = new Subscription(m_Session.DefaultSubscription)
{
DisplayName = SubDisplayName,
PublishingEnabled = true,
PublishingInterval = PubInt, // in milliseconds.
KeepAliveCount = KeepAlive, // 10*UaRefreshRate = 5s if UaRefreshRate = 500
LifetimeCount = LifeCount, // UaRefreshRate*100 = 50s if UaRefreshRate = 500;
MaxNotificationsPerPublish = MaxNotifyPerPublish,
};
foreach (var myrows in mylist)
{
//OPCMonitoredNodeID = myrows[0].ToString();
//Step 5 - Add a list of items you wish to monitor to the subscription.
var BatchInputlist = new List<MonitoredItem>
{
new MonitoredItem(subscription.DefaultItem)
{
DisplayName = "Astec Monitoring",
StartNodeId = myrows,
AttributeId = Attributes.Value,
MonitoringMode = MonitoringMode.Reporting,
SamplingInterval = 250
}
};
subscription.AddItems(BatchInputlist);
m_Session.AddSubscription(subscription);
}
//Step 6 - Add the subscription to the session.
subscription.Create();
//m_Session.Notification += new NotificationEventHandler(Session_Notification);
//NodeId mynode = new NodeId(@"ns=3;s=""Inputs_FB_DB"".""Input1""");
//DataValue dvread = session.ReadValue(mynode);
//g_Notification = dvread.ToString();
//NodeId mynode = new NodeId(@"ns=3;s=""Inputs_FB_DB"".""Input1""");
//MessageBox.Show(mynode.ToString());
//DataValue dvread = session.ReadValue(mynode);
//MessageBox.Show(@"Input1 = " + dvread.ToString());
return subscription;
}
Thanks,
Please Log in or Create an account to join the conversation.
thank you for your interest in our products.
1. I do not quite understand this part: "How can I ... without creating a monitored item for each variable.". What's wrong with creating a monitored item for each variable? It is not even possible to subscribe to multiple variables without doing that; when you write " In the OPC Stack you can create a write list and create a subscription to all the variables", you are, in principle, referring to doing the same thing.
2. To your code: each element of UANodeElementCollection is a UANodeElement, and it has an implciti conversion to UANodeDescriptor. The line with call to the EasyUAMonitoredItemArguments constructor can therefore be written much easier, simply pass in the 'Nodes' variable as the 3rd argument).
Best regards
Please Log in or Create an account to join the conversation.
For Example:
UANodeElementCollection MyNodes = MyClient.BrowseDataNodes("opc.tcp://10.42.10.80:4840", @"ns=3;s=""OPCUA_PC_PLC"""); <---- loop through the Siemens DB and return all nodes then do a foreach and create my subscriptions.
foreach (UANodeElement Nodes in MyNodes)
{
//MessageBox.Show(@"ns=3;s=""OPCUA_PC_PLC"".""" + Nodes + "" + " " + @"ns=3;s=""OPCUA_PC_PLC"".""EnableScale1""");
MyClient.SubscribeMultipleMonitoredItems(new[]
{
new EasyUAMonitoredItemArguments(null,"opc.tcp://10.42.10.80:4840", @"ns=3;s=""OPCUA_PC_PLC"".""" + Nodes + "")
});
}
Thanks,
Please Log in or Create an account to join the conversation.
- Forum
- Discussions
- QuickOPC-UA in .NET
- Reading, Writing, Subscriptions
- Monitoring all variables in a DB