我如何获得一个OPC服务器上的标签列表(How do I get a list of tags on

2019-10-20 15:33发布

我试图从OPC服务器获取标签的列表中,我使用从QuickOPC的EasyDaClient。 我想要做的就是这

try
{
    //create the OPC Client object
    OpcLabs.EasyOpc.DataAccess.EasyDAClient easyDAClient1 = new OpcLabs.EasyOpc.DataAccess.EasyDAClient();

    //ServerDescriptor Declration
        OpcLabs.EasyOpc.ServerDescriptor sd = new OpcLabs.EasyOpc.ServerDescriptor();
sd.MachineName = "W7VM";
sd.ServerClass = "OPC.Siemens.XML.1";

    OpcLabs.EasyOpc.DataAccess.DANodeElementCollection allTags = easyDAClient1.BrowseLeaves(sd);

    foreach (OpcLabs.EasyOpc.DataAccess.DANodeElement tag in allTags)
    {
        //if the value is a branch add it to the listbox.
        if (tag.IsLeaf)
        {
            //add the fully qualified item id
            listBox1.Items.Add(tag.ItemId.ToString());
        }
    }


}
catch (Exception ex)
{
    MessageBox.Show("Error: " + ex.Message.ToString());
}

我总是从BrowseLeaves方法0元,我不知道什么是我的Channel_1.Device_1,这样我可以用其他的过载。 我是新来这个能有人解释我的OPC标签如何上市? FYI:我可以阅读使用标记的值:

easyDAClient1.ReadItem(MachineNameTextBox.Text,serverClassTextBox.Text,itemIdTextBox.Text);

因此,它不是一个Conenction问题

Answer 1:

您在根级别有浏览叶子,有没有,这就是为什么你得到一个空的集合。

你能做什么? 有几个选项有:

1)如果你想在根目录开始浏览并获得了叶级,你需要考虑的树枝也是如此。 使用BrowseBranches方法,或(甚至更好)使用BrowseNodes,它返回无论是树枝和树叶。 当你得到一个分支节点,您可能会决定进一步浏览到它(您可以使用.IsBranch测试)。

2)如果你想获得的叶子和知道哪个分支他们在,你可以在分支名传递给BrowseLeaves方法作为附加参数。 然而,这可能不是你的情况,我可以猜到表格,您说“我不知道什么是我的Channel_1.Device_1”,这恐怕是分支ID,你不“知道”的前期。

下面是递归浏览一个完整的例子:

// BrowseAndReadValues: Console application that recursively browses and displays the nodes in the OPC address space, and 
// attempts to read and display values of all OPC items it finds.

using System.Diagnostics;
using JetBrains.Annotations;
using OpcLabs.EasyOpc;
using OpcLabs.EasyOpc.DataAccess;
using System;

namespace BrowseAndReadValues
{
    class Program
    {
        const string ServerClass = "OPCLabs.KitServer.2";

        [NotNull]
        static readonly EasyDAClient Client = new EasyDAClient();

        static void BrowseAndReadFromNode([NotNull] string parentItemId)
        {
            // Obtain all node elements under parentItemId
            var nodeFilter = new DANodeFilter();    // no filtering whatsoever
            DANodeElementCollection nodeElementCollection = Client.BrowseNodes("", ServerClass, parentItemId, nodeFilter);
            // Remark: that BrowseNodes(...) may also throw OpcException; a production code should contain handling for it, here 
            // omitted for brevity.

            foreach (DANodeElement nodeElement in nodeElementCollection)
            {
                Debug.Assert(nodeElement != null);

                // If the node is a leaf, it might be possible to read from it
                if (nodeElement.IsLeaf)
                {
                    // Determine what the display - either the value read, or exception message in case of failure.
                    string display;
                    try
                    {
                        object value = Client.ReadItemValue("", ServerClass, nodeElement);
                        display = String.Format("{0}", value);
                    }
                    catch (OpcException exception)
                    {
                        display = String.Format("** {0} **", exception.GetBaseException().Message);
                    }

                    Console.WriteLine("{0} -> {1}", nodeElement.ItemId, display);
                }
                // If the node is not a leaf, just display its itemId
                else
                    Console.WriteLine("{0}", nodeElement.ItemId);

                // If the node is a branch, browse recursively into it.
                if (nodeElement.IsBranch &&
                    (nodeElement.ItemId != "SimulateEvents") /* this branch is too big for the purpose of this example */)
                    BrowseAndReadFromNode(nodeElement);
            }
        }

        static void Main()
        {
            Console.WriteLine("Browsing and reading values...");
            // Set timeout to only wait 1 second - default would be 1 minute to wait for good quality that may never come.
            Client.InstanceParameters.Timeouts.ReadItem = 1000;

            // Do the actual browsing and reading, starting from root of OPC address space (denoted by empty string for itemId)
            BrowseAndReadFromNode("");

            Console.WriteLine("Press Enter to continue...");
            Console.ReadLine();
        }
    }
}

技术支持: http://www.opclabs.com/forum/index



文章来源: How do I get a list of tags on a OPC server
标签: c# opc