Simple OPC Client in Unity3d|OPC UA Implementation: Stacks, Tools, and Samples|Forum|OPC Foundation

Avatar
Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
Lost password?
sp_Feed sp_PrintTopic sp_TopicIcon
Simple OPC Client in Unity3d
Avatar
Hidethepain Harold
New Member
Members
Forum Posts: 1
Member Since:
10/30/2018
sp_UserOfflineSmall Offline
1
11/01/2018 - 13:08
sp_Permalink sp_Print

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. 

Avatar
Paul Hunkar
Cleveland, Ohio, USA
Moderator
Members

Moderators-Specifications

Moderators-Companion

Moderators-Implementation

Moderators-Certification

Moderators-COM
Forum Posts: 112
Member Since:
02/24/2014
sp_UserOfflineSmall Offline
2
11/05/2018 - 11:06
sp_Permalink sp_Print

Coding related questions should be posted to Github

version 1.02.334.6 is a very old version, we have released 1.04 and are maintaining 1.03

Paul

Paul Hunkar - DSInteroperability

Forum Timezone: America/Phoenix
Most Users Ever Online: 510
Currently Online:
Guest(s) 18
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Forum Stats:
Groups: 2
Forums: 10
Topics: 1446
Posts: 4891