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, EET
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, EET
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 579
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: 267

Currently Online:
11 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

hbrackel: 103

pramanj: 86

ibrahim: 70

kapsl: 57

gjevremovic: 49

TimK: 41

Fransua33: 39

fred: 38

Rainer Versteeg: 32

Thomas Reuther: 31

Member Stats:

Guest Posters: 0

Members: 1160

Moderators: 15

Admins: 1

Forum Stats:

Groups: 3

Forums: 15

Topics: 1048

Posts: 4416

Newest Members:

jonas.rahm, sophiekohler, zqecortney, adeletoscano947, forestgenders23, auroratrumper, ericmclain04, rblu, starlowery23194, dakotadalgety82

Moderators: Jouni Aro: 853, Otso Palonen: 32, Tuomas Hiltunen: 5, janimakela: 0, Pyry: 1, Terho: 0, Petri: 0, Bjarne Boström: 579, Heikki Tahvanainen: 402, Jukka Asikainen: 1, moldzh08: 0, Teppo Uimonen: 21, Markus Johansson: 24, Matti Siponen: 72, Lusetti: 0

Administrators: admin: 1