10/30/2018
I tried for a long time to make a simple Client in Unity3d. I am not able to solve all errors. I hope someone can give me some hints.
Unity3d uses the Scripting Runtime Version: .NET 4.6
My Code which is a modification of the NetCoreConsoleClient:
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using Opc.Ua; using Opc.Ua.Client; using Opc.Ua.Configuration; public class main : MonoBehaviour { private async void Start() { Console.WriteLine("Step 1 - Create a config."); var config = new ApplicationConfiguration() { ApplicationName = "test-opc", ApplicationType = ApplicationType.Client, SecurityConfiguration = new SecurityConfiguration { ApplicationCertificate = new CertificateIdentifier() }, TransportConfigurations = new TransportConfigurationCollection(), TransportQuotas = new TransportQuotas { OperationTimeout = 15000 }, ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 } }; await config.Validate(ApplicationType.Client); if (config.SecurityConfiguration.AutoAcceptUntrustedCertificates) { config.CertificateValidator.CertificateValidation += (s, e) => { e.Accept = (e.Error.StatusCode == StatusCodes.BadCertificateUntrusted); }; } Console.WriteLine("Step 2 - Create a session with your server."); using (var session = await Session.Create(config, new ConfiguredEndpoint(null, new EndpointDescription("opc.tcp://localhost:4841")), true, "", 60000, null, null)) { 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(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("+ " + 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<MonitoredItem> { new MonitoredItem(subscription.DefaultItem) { DisplayName = "aaatime", StartNodeId = "i=10004" } }; 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("Finished client initialization"); } } 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); } } }
I am using the dll from the newst Release from Github. I got a lot of errors like:
Severity Code Description Project File Line Suppression State
Error CS0012 The type 'Enum' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. OPCProjekt C:\Users\HideThePainHarold\Documents\OPCProjekt\Assets\main.cs 17 Active
or:
Severity Code Description Project File Line Suppression State
Error CS0012 The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. OPCProjekt C:\Users\jaha\Documents\OPCProjekt\Assets\main.cs 68 Active
and so on.. (50 errors)
My other approach would be to use the old OPC library which is running on .NET 3.5 (which is the standard Version of Unity3D) as mentioned here. But where can I find Version 1.02.334.6 of the OPC.ua to download?
It would be soo kind, if someone could help me out.
1 Guest(s)