9:20, EEST
May 30, 2020
Hi,
I’m trying to change an OPC UA Console Client into a Winform App.
Here’s the Console code:
using Opc.Ua;
using Opc.Ua.Client;
using Opc.Ua.Configuration;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
namespace MyHomework
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Step 1 – Create application configuration and certificate.”);
var config = new ApplicationConfiguration()
{
ApplicationName = “MyHomework”,
ApplicationUri = Utils.Format(@”urn:{0}:MyHomework”, System.Net.Dns.GetHostName()),
ApplicationType = ApplicationType.Client,
SecurityConfiguration = new SecurityConfiguration
{
ApplicationCertificate = new CertificateIdentifier { StoreType = @”Directory”, StorePath = @”%CommonApplicationData%\OPC Foundation\CertificateStores\MachineDefault”, SubjectName = “MyHomework” },
TrustedIssuerCertificates = new CertificateTrustList { StoreType = @”Directory”, StorePath = @”%CommonApplicationData%\OPC Foundation\CertificateStores\UA Certificate Authorities” },
TrustedPeerCertificates = new CertificateTrustList { StoreType = @”Directory”, StorePath = @”%CommonApplicationData%\OPC Foundation\CertificateStores\UA Applications” },
RejectedCertificateStore = new CertificateTrustList { StoreType = @”Directory”, StorePath = @”%CommonApplicationData%\OPC Foundation\CertificateStores\RejectedCertificates” },
AutoAcceptUntrustedCertificates = true
},
TransportConfigurations = new TransportConfigurationCollection(),
TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 },
TraceConfiguration = new TraceConfiguration()
};
config.Validate(ApplicationType.Client).GetAwaiter().GetResult();
if (config.SecurityConfiguration.AutoAcceptUntrustedCertificates)
{
config.CertificateValidator.CertificateValidation += (s, e) => { e.Accept = (e.Error.StatusCode == StatusCodes.BadCertificateUntrusted); };
}
var application = new ApplicationInstance
{
ApplicationName = “MyHomework”,
ApplicationType = ApplicationType.Client,
ApplicationConfiguration = config
};
application.CheckApplicationInstanceCertificate(false, 2048).GetAwaiter().GetResult();
var selectedEndpoint = CoreClientUtils.SelectEndpoint(“opc.tcp://192.168.0.3:62546”, useSecurity: false, operationTimeout: 15000);
Console.WriteLine($”Step 2 – Create a session with your server: {selectedEndpoint.EndpointUrl} “);
using (var session = Session.Create(config, new ConfiguredEndpoint(null, selectedEndpoint, EndpointConfiguration.Create(config)), false, “”, 60000, null, null).GetAwaiter().GetResult())
{
Console.WriteLine(“Step 3 – Browse the server namespace.”);
ReferenceDescriptionCollection refs;
Byte[] cp;
session.Browse(null, null, ObjectIds.ObjectsFolder, 0u, BrowseDirection.Forward, ReferenceTypeIds.HierarchicalReferences, true, (uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method, out cp, out refs);
Console.WriteLine(“DisplayName: BrowseName, NodeClass”);
foreach (var rd in refs)
{
Console.WriteLine(“{0}: {1}, {2}”, rd.DisplayName, rd.BrowseName, rd.NodeClass);
ReferenceDescriptionCollection nextRefs;
byte[] nextCp;
session.Browse(null, null, ExpandedNodeId.ToNodeId(rd.NodeId, session.NamespaceUris), 0u, BrowseDirection.Forward, ReferenceTypeIds.HierarchicalReferences, true, (uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method, out nextCp, out nextRefs);
foreach (var nextRd in nextRefs)
{
Console.WriteLine(“+ {0}: {1}, {2}”, nextRd.DisplayName, nextRd.BrowseName, nextRd.NodeClass);
}
}
Console.WriteLine(“Step 4 – Create a subscription. Set a faster publishing interval if you wish.”);
var subscription = new Subscription(session.DefaultSubscription) { PublishingInterval = 1000 };
Console.WriteLine(“Step 5 – Add a list of items you wish to monitor to the subscription.”);
var list = new List { new MonitoredItem(subscription.DefaultItem) { DisplayName = “CurrentVariableValue”, StartNodeId = “ns=2;i=6” } };
list.ForEach(i => i.Notification += OnNotification);
subscription.AddItems(list);
Console.WriteLine(“Step 6 – Add the subscription to the session.”);
session.AddSubscription(subscription);
subscription.Create();
Console.WriteLine(“Press any key to remove subscription…”);
Console.ReadKey(true);
}
Console.WriteLine(“Press any key to exit…”);
Console.ReadKey(true);
}
private static void OnNotification(MonitoredItem item, MonitoredItemNotificationEventArgs e)
{
foreach (var value in item.DequeueValues())
{
Console.WriteLine(“{0}: {1}, {2}, {3}”, item.DisplayName, value.Value, value.SourceTimestamp, value.StatusCode);
}
}
}
}
When execute the program, it connects to an OPC UA Server, if the variable “CurrentVariableValue” changed, it go to OnNotification and print that value to console.
What I pretend to do is create a Label in Winform, and update label’s text to display variable value.
The problem is, when the console is running, winform still pop-up but I cannot interact with it (even cannot turn it off). When ending OPC UA connection (press any key twice in console), the winform works normally.
Can you suggest me some solution for that?
11:06, EEST
April 3, 2012
Hi,
Please note that this is not a general purpose programming help forum. We do not teach basic programing nor answer questions about it.
Note that we only answer questions related to our own products or to those that we resell depending on thing.
Based on the code that would seem to be related to the OPC Foundation’s .NET SDK, thus please ask the OPC Foundation about that. We basically have nothing to do with that SDK. We do resell our partner’s https://www.unified-automation.com/products/server-sdk/net-ua-server-sdk.html, which is different.
As a general tip, most UI-framworks are modeled as single-threaded and thus use a https://en.wikipedia.org/wiki/Event_dispatching_thread, thus you probably need to look for some general .NET tutorials online.
Most Users Ever Online: 1919
Currently Online:
18 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Heikki Tahvanainen: 402
hbrackel: 144
rocket science: 88
pramanj: 86
Francesco Zambon: 83
Ibrahim: 78
Sabari: 62
kapsl: 57
gjevremovic: 49
Xavier: 43
Member Stats:
Guest Posters: 0
Members: 730
Moderators: 7
Admins: 1
Forum Stats:
Groups: 3
Forums: 15
Topics: 1529
Posts: 6471
Newest Members:
rickykennion, PromotionToold, HypromeImpupe, toneylapham544, rondawolinski7, Marypof5711, roycedelargie91, kourtneyquisenbe, ellis87832073466, zkxwilliemaeModerators: Jouni Aro: 1026, Pyry: 1, Petri: 0, Bjarne Boström: 1032, Jimmy Ni: 26, Matti Siponen: 349, Lusetti: 0
Administrators: admin: 1