Holelens2 refuses to connect to a OPC.UA server

57 Views Asked by At

I am currently working on a Unity project for my university, where we are tasked with creating an app for the Hololens2 to present CO2 data collected from a 3D printer via an OPC.UA server.

I've created an OPC client in C# using the Opc.Ua library and the code works perfectly fine in the Unity player. However, when running it on the Hololens2, I encounter the following error:

Session not connected!
UnityEngine.DebugLogHandler:Internal_Log_Injected(LogType, LogOption, String, IntPtr)
UnityEngine.DebugLogHandler:Internal_Log(LogType, LogOption, String, Object)
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
UnityEngine.Logger:Log(LogType, Object)
UnityEngine.Debug:LogError(Object)
OpcClient:read()

Ausnahme ausgelöst bei 0x00007FF8DF6B5E5C in Test project.exe: Microsoft C++-Ausnahme: Il2CppExceptionWrapper bei Speicherort 0x0000002B47DFCF40.
Connection failed:  The type initializer for 'Opc.Ua.ServiceMessageContext' threw an exception.
UnityEngine.DebugLogHandler:Internal_Log_Injected(LogType, LogOption, String, IntPtr)
UnityEngine.DebugLogHandler:Internal_Log(LogType, LogOption, String, Object)
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
UnityEngine.Logger:Log(LogType, Object)
UnityEngine.Debug:LogError(Object)
OpcClient:connect()
OpcClient:read()

I've included the relevant parts of my code below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Opc.Ua.Client;
using Opc.Ua.Configuration;
using Opc.Ua;
public class OpcClient : MonoBehaviour
{
    string url = @"opc.tcp://192.168.0.172:4840";
    private Session session;
    private ApplicationConfiguration app_configuration = new ApplicationConfiguration();
    public Dictionary<string, string> values = new Dictionary<string, string>();
    private void connect()
    {
        try
        {
            app_configuration = new ApplicationConfiguration()
            {
                ApplicationName = "Unity OPC UA Client",
                ApplicationUri = url,
                ApplicationType = ApplicationType.Client,

                TransportConfigurations = new TransportConfigurationCollection(),
                TransportQuotas = new TransportQuotas { OperationTimeout = 10000 },
                ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 50000 },
                TraceConfiguration = new TraceConfiguration()
            };
            EndpointDescription endpointDescription = CoreClientUtils.SelectEndpoint(url, false);
            session = Session.Create(app_configuration, new ConfiguredEndpoint(null, endpointDescription, EndpointConfiguration.Create(app_configuration)), false, "", 10000, null, null).GetAwaiter().GetResult();
        }
        catch (System.Exception e)
        {
            Debug.LogError("Connection failed:  " + e.Message);
        }
    }
    void read()
    {
        if (session == null || !session.Connected)
        {
            Debug.LogError("Session not connected!");
            connect();
            return;
        }
        try
        {

            DataValue turn = session.ReadValue(NodeId.Parse("ns=2;i=2"));
            addValue("turn", turn.Value.ToString());
            Debug.Log("TURN: " + turn.ToString());
            DataValue timestamp = session.ReadValue(NodeId.Parse("ns=2;i=3"));
            addValue("timestamp", timestamp.Value.ToString());
            Debug.Log("TIMESTAMP: " + timestamp.ToString());
            DataValue totalInputWH = session.ReadValue(NodeId.Parse("ns=2;i=4"));
            addValue("totalInputWH", totalInputWH.Value.ToString());
            Debug.Log("TOTALINPUT in WH: " + totalInputWH.ToString());
            DataValue totalPowerW = session.ReadValue(NodeId.Parse("ns=2;i=5"));
            addValue("totalPowerW", totalPowerW.Value.ToString());
            Debug.Log("TOTALPOWER in W: " + totalPowerW.ToString());
            DataValue powerHeatbedW = session.ReadValue(NodeId.Parse("ns=2;i=6"));
            addValue("powerHeatbedW", powerHeatbedW.Value.ToString());
            Debug.Log("POWERHEATBED in W: " + powerHeatbedW.ToString());
            DataValue powerHotendW = session.ReadValue(NodeId.Parse("ns=2;i=7"));
            addValue("powerHotendW", powerHotendW.Value.ToString());
            Debug.Log("POWERHOTEND in W: " + powerHotendW.ToString());
            DataValue currentWork = session.ReadValue(NodeId.Parse("ns=2;i=10"));
            addValue("currentWork", currentWork.Value.ToString());
            Debug.Log("CURRENTWORK in W: " + currentWork.ToString());
            DataValue currentTurn = session.ReadValue(NodeId.Parse("ns=2;i=11"));
            addValue("currentTurn", currentWork.Value.ToString());
            Debug.Log("CURRENTURN: " + currentWork.ToString());
            DataValue currentCo2 = session.ReadValue(NodeId.Parse("ns=2;i=12"));
            addValue("currentCo2", currentCo2.Value.ToString());
            Debug.Log("CURRENCO2: " + currentCo2.ToString());
        }
        catch (System.Exception e)
        {
            Debug.Log("Read failed: " + e.Message);
            session.Close();
        }
    }
    private void addValue(string key, string value)
    {
        if (values.ContainsKey(key))
        {
            values[key] = value;
        }
        else
        {
            values.Add(key, value);
        }
    }
    // Start is called before the first frame update

    public string getValue(string key)
    {
        if (values.ContainsKey(key))
        {
            return values[key];
        }
        else
        {
            return "Key not found!";
        }
    }

    void Start()
    {
        connect();
        //call read every 1 seconds
        InvokeRepeating("read", 2.0f, 1.0f);
    }
}

I've also verified that the Package.appxmanifest includes the necessary capabilities:

  <Capabilities>
    <Capability Name="internetClient" />
    <Capability Name="internetClientServer" />
    <Capability Name="privateNetworkClientServer" />
    <DeviceCapability Name="webcam" />
    <DeviceCapability Name="microphone" />
  </Capabilities>

Additional Information:

Unity version: 2023.1.20f

OPC.UA library version: 1.5.373.121

Does anybody have any idea what is going on? I am Losing my mind!

0

There are 0 best solutions below