Avatar

Please consider registering
guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed Topic RSS sp_TopicIcon
OPC UA Client C#
June 1, 2020
9:20, EEST
Avatar
NilsonChalie
Member
Members
Forum Posts: 4
Member Since:
May 30, 2020
sp_UserOfflineSmall Offline

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?

June 1, 2020
11:06, EEST
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 1026
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

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.

Forum Timezone: Europe/Helsinki

Most Users Ever Online: 1919

Currently Online:
59 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: 734

Moderators: 7

Admins: 1

Forum Stats:

Groups: 3

Forums: 15

Topics: 1523

Posts: 6449

Newest Members:

christamcdowall, redaahern07571, nigelbdhmp, travistimmons, AnnelCib, dalenegettinger, howardkennerley, Thomassnism, biancacraft16, edgardo3518

Moderators: Jouni Aro: 1026, Pyry: 1, Petri: 0, Bjarne Boström: 1026, Jimmy Ni: 26, Matti Siponen: 346, Lusetti: 0

Administrators: admin: 1