RobinFischer / OPC-UA_Unity

Unity Project of an OPC UA Sample Client. Designed to be used with a Microsoft HoloLens
MIT License
17 stars 10 forks source link

OPC client not working #1

Open blackbeard opened 6 years ago

blackbeard commented 6 years ago

i have adapted your code to run Opc client from unity to siemens DP opc server, using the same script as OPC ua input field manager. here is the code.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using OpcUa_Client_Hda; using System;

public class OpcUaInputFieldManager : MonoBehaviour {

public string endpointField;
public string Subscription1Field;   
public int subsTimeField;
OpcUaClient Client;

public string status;
public string Subscription1_status;

private int delayCnt;

private string EndpointURL;

private string SubscriptionId1;

private int PublishInterval;

private string variable1_name;
private int variable1_index;

public string TutorialOutput;

// Use this for initialization
public void Start()
{
    Debug.Log("Starting client");
    //login procedure values
    EndpointURL = "opc.tcp://Unity:55103";
    //Username = "Name";
    //Password = "Password";

    //Variable subscription values
    SubscriptionId1 = "ns=5;i=-1";
    variable1_name = "variable1";

    PublishInterval = 500;

    showValues();

    Client = new OpcUaClient();
    if (Client.setEndpoint(EndpointURL))
    {
        if (Client.connect())
        {
            onConnect();
            //subscribing to given variable
            variable1_index = Client.addItemToSubscribe(variable1_name, SubscriptionId1);
            if (Client.addSubscription(PublishInterval))
                Subscription1_status = "green";
            else
                Subscription1_status = "red";

            Debug.Log("Connected in the start");
        }
        else
        {
            Debug.Log("Failed to Connect");
        }
    }
    else
    {
        Debug.Log("Setpoint not reachable");
    }
}

// Update is called once per frame
void Update()
{
    delayCnt++;
    if (delayCnt == 10)
    {
        //get Variable
        if (OpcUaClient.m_list_subscribed[variable1_index].m_status)
        {
            TutorialOutput = OpcUaClient.m_list_subscribed[variable1_index].m_value;
        }
        delayCnt = 0;
    }
}

public void onReset()
{
    Debug.Log("Input field onReset");
    showValues();
}

public void onConnect()
{
    Debug.Log("Input field onConnect");
    Client.disconnect();

    if (Client.setEndpoint(endpointField))
    {
        Debug.Log("Setpoint ok");

        EndpointURL = endpointField;

        if(Client.connect())
        {
            Debug.Log("Connect ok");

            SubscriptionId1 = Subscription1Field;

            PublishInterval = subsTimeField;
           if(Client.isConnected)
            {
                status = "Conection Succeded";
            }

        }
        if (false);
        else
        {
            Debug.Log("Connect Wrong!!!!");
            showValues();
            status = "Conection Failed";
        }
    }
    else
    {
        Debug.Log("Endpoint not reachable!!!!");
        status = "Endpoint not reachable!";
    }

}

public void onDisconnect()
{
    Debug.Log("input field onDisconnect");
    Client.disconnect();
    status = "";
}

public void showValues()
{
    endpointField = EndpointURL;
    Subscription1Field = SubscriptionId1;  
    subsTimeField = PublishInterval;
    status = "";
}

public void resetErrorCode()
{
    Debug.Log("Reset TRIGGERED!!!");

}

public void resetStart()
{
    Debug.Log("ResetStart TRIGGERED!!!");

}

}

in the debugging i checked that its not making isConnected flag to true.

please help.

RobinFischer commented 6 years ago

Sorry for the late response. This client is only designed to work with the UA Sample Server by the OPC Foundation: https://github.com/OPCFoundation/UA-.NETStandard

To connect it to your siemens DP opc server you probably have to adapt the DLL we are using. (e.g. to make it set the isConnected flag to true)

We intended to make instructions on how to setup the project soon. Maybe that will help you.