2

I have an OPC-DA Server that a SCADA software writes the variables and its values in it so I want to read them synchronously with using C#. I have already write my algorithm but I could not read the variables. The code creates a subscription or may be creates a group instance that writes the own variables and values in it but I do not want this. I need to just read the values from OPC server.

I have established a connection between OPC Server but I have not reach the variables which writes the variables into OPC Server.

Where is the problem, I cannot realise it. Could you suggest a solution about it?

My Code:

class OpcFunctions
{
    Opc.Da.Server Server = null;
    OpcCom.Factory Factory = new OpcCom.Factory();
    Opc.Da.Item[] Items;
    Opc.Da.Subscription Group;
    Opc.IRequest myReq;
    Opc.Da.WriteCompleteEventHandler WriteEventHandler;
    Opc.Da.ReadCompleteEventHandler ReadEventHandler;



    public void GetOpcServers(TreeView OpcServerTreeList, ListBox OpcConnectionUrlListBox)
    {
        try
        {
            OpcCom.ServerEnumerator myServerEnumerator = new OpcCom.ServerEnumerator();
            Opc.Server[] Servers = myServerEnumerator.GetAvailableServers(Opc.Specification.COM_DA_20);
            ListServers(Servers,OpcServerTreeList,OpcConnectionUrlListBox);
        }

        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void ListServers(Opc.Server[] OpcServerList , TreeView OpcServerTreeList, ListBox OpcConnectionUrlListBox)
    {
        try
        {
            OpcServerTreeList.Nodes.Clear();
            OpcConnectionUrlListBox.Items.Clear();

            foreach(Opc.Server myServer in OpcServerList)
            {
                TreeNode myTreeNode = new TreeNode(myServer.Name);
                myTreeNode.Nodes.Add(myServer.Url.HostName + ":" + myServer.Url.Path + ":" + myServer.Url.Port);
                myTreeNode.Nodes.Add(myServer.Url.ToString());
                myTreeNode.Nodes.Add(myServer.IsConnected.ToString());
                OpcServerTreeList.Nodes.Add(myTreeNode);
                OpcConnectionUrlListBox.Items.Add(myServer.Url.ToString());
            }
        }

        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    public bool ConnectOpcServer(string OpcUrl)
    {
        Opc.URL Url = new Opc.URL(OpcUrl);
        Server = new Opc.Da.Server(Factory, null);

        try
        {
            Server.Connect(Url, new Opc.ConnectData(new System.Net.NetworkCredential()));

            Opc.Da.SubscriptionState GroupState = new Opc.Da.SubscriptionState();
            GroupState.Name = "Group1";
            GroupState.Active = true;

            Group = (Opc.Da.Subscription)Server.CreateSubscription(GroupState);
            Group.DataChanged += new Opc.Da.DataChangedEventHandler(GroupDataChanged);

            Items = Group.AddItems(Items);

            ReadEventHandler = new Opc.Da.ReadCompleteEventHandler(ReadCompleteCallback);

            Group.Read(Group.Items, 123, ReadCompleteCallback, out myReq);
        }

        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
            return false;
        }

        return true;
    }

    void GroupDataChanged(object subscriptionHandle, object requestHandle, Opc.Da.ItemValueResult[] values)
    {
        uint order = 1;
        foreach (Opc.Da.ItemValueResult chitem in values)
        {
            myWriteLogList(order, chitem.Timestamp, chitem.ItemName, chitem.Value.ToString(), chitem.Quality.ToString());
            ++order;
        }
    }

    void myWriteLogList(uint order, DateTime timestamp, string name, string value, string signalquality)
    {
        SettingsUI.OpcExplorer.dataGridViewOpcExplorer.BeginInvoke((MethodInvoker)delegate
        {
            SettingsUI.OpcExplorer.dataGridViewOpcExplorer.Rows.Add(null,order,timestamp,name,value,signalquality);
        });
    }

    void ReadCompleteCallback(object clientHandle, Opc.Da.ItemValueResult[] results)
    {
        uint order = 1;
        foreach (Opc.Da.ItemValueResult readResult in results)
        {
            myWriteLogList(order, readResult.Timestamp, readResult.ItemName, readResult.Value.ToString(), readResult.Quality.ToString());
            ++order;
        }
    }
}
Mert Arisoy
  • 65
  • 2
  • 7
  • You have not described the nature of the problem - what you expect to happen when you run the code, and what has happened in reality. Also, please remove the "opc-ua" tag from the question, as the code is not related to OPC UA, but rather to OPC Data Access. – ZbynekZ Feb 01 '18 at 08:38
  • Actually "Group.Read(Group.Items, 123, ReadCompleteCallback, out myReq);" part cannot read the variables, but I expect to read them, Sorry for misunderstanding. – Mert Arisoy Feb 01 '18 at 08:57
  • Are you getting an exception, or no exception but also no callback? – ZbynekZ Feb 02 '18 at 05:44

2 Answers2

0

Your 'Items' is empty!

sample:

Opc.Da.Item[] items = new Opc.Da.Item[1];

items[0] = new Opc.Da.Item();

items[0].ItemName = "PlcGroup.Items.value";

Hsiang
  • 1
  • Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.[How to Answer](https://stackoverflow.com/help/how-to-answer) – Elletlar Nov 01 '18 at 09:43
0

Try read from server... ADD ITEMS AND READ

var result=Server.read(items); For(i=0;i<result.length;i++) { Console.writeln(result[i].value); }