gpt4 book ai didi

.net - 使用 opc ua .net 库在 Unity3d 中创建一个非常简单的 OPC 客户端

转载 作者:行者123 更新时间:2023-12-02 14:37:41 25 4
gpt4 key购买 nike

我在尝试实现一个简单的 OPC 时遇到这些错误使用 Unity3D 的 .Net 客户端。这些错误出现在 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

以及其他 49 个类似错误。

Unity3D 向我显示此错误:

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 使用脚本运行时版本 .Net 3.5 我将其更改为 .NET 4.6,以便能够使用 OPC Library

这是我的代码:

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

该代码移植自 .Net Core example到 Unity3d。我该如何解决这些错误?

最佳答案

文档位于GitHub OPC Unified Architecture .NET Standard假设最低要求是 .NET Standard 2.0。
据Unity论坛What .NET stack for UWP/HoloLens?只要开发人员在播放器设置中选择 .NET 作为脚本后端,就只能使用 .NET Standard 1.4 版。

我没有测试这个特定的库,但我认为当您切换到 IL2CPP 时它应该可以工作,因为它支持 2.0。

从 2018.2 版本开始,Unity 将 .NET 标记为已弃用,因此我认为他们将来不会做出很大的努力来改变这种情况,而是会专注于 IL2CPP。
遗憾的是,我喜欢 .NET 的短构建周期

关于.net - 使用 opc ua .net 库在 Unity3d 中创建一个非常简单的 OPC 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53109110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com