Create a very simple OPC client in Unity3d with opc ua .net library

泄露秘密 提交于 2020-06-27 10:08:32

问题


I am getting these errors while trying to implement a simple OPC client in .Net with Unity3D.These errors are in Visual Studio:

Severity    Code    Description Project File    Line    Suppression State
Error   CS0012  The type 'X509CertificateValidator' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.ServiceModel.Primitives, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Test    C:\Users\hidethepain\Documents\opc\Assets\main.cs   27  

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'.    Test, Test.Player   C:\Users\hidethepain\Documents\opc\Assets\main.cs   18  Active

Severity    Code    Description Project File    Line    Suppression State
Error   CS0012  The type 'Task<>' 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'.  Test, Test.Player   C:\Users\hidethepain\Documents\opc\Assets\main.cs   31  Active

And other 49 similar errors.

Unity3D shows me this error:

Assets/main.cs(67,40): error CS0012: The type `System.Object' is defined in an assembly that is not referenced. Consider adding a reference to assembly `netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'

Unity3D uses the Scripting Runtime Version .Net 3.5 I changed it to .NET 4.6 to be able to use the OPC Library.

Here is my Code:

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);
        }
    }
}

The Code is ported from the .Net Core example to Unity3d. How could I solve these errors?


回答1:


The docs at GitHub OPC Unified Architecture .NET Standard say the minmum requirement is .NET Standard 2.0.
According to the Unity Forum What .NET stack for UWP/HoloLens? developers are restricted to .NET Standard version 1.4 as long as they select .NET as Scripting Backend in Player Settings.

I didn't test this specific library but I think it should work when you switch to IL2CPP as this supports 2.0.

Starting with version 2018.2 Unity marks .NET as deprecated so I think they will not make strong efforts in the future to change the situation but will focus on IL2CPP instead.
What a pity, I like the short build cycles with .NET



来源:https://stackoverflow.com/questions/53109110/create-a-very-simple-opc-client-in-unity3d-with-opc-ua-net-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!