【问题标题】:How do I get a list of tags on a OPC server如何获取 OPC 服务器上的标签列表
【发布时间】:2014-08-07 05:36:04
【问题描述】:

我正在尝试从 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 标签吗? 仅供参考:我可以使用以下命令从标签中读取值:

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

所以这不是连接问题

【问题讨论】:

    标签: c# opc opc-da


    【解决方案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

    【讨论】:

    • 这种方法效果很好..但是性能呢..??当 opc 服务器监视工厂中的多个“组件”时,浏览所有分支不会减慢系统速度,对吧...??
    • 您的问题是关于如何获取标签列表 - 这就是浏览所做的 - 除了浏览之外别无他法。当然,广泛的浏览可能是资源密集型的,并且会产生后果,但接下来由您来处理 - 具体而言,通常您不会在每次想要阅读某些内容时浏览。
    猜你喜欢
    • 2015-02-03
    • 2022-12-09
    • 2013-08-16
    • 2011-05-12
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多