开发者

C# opc-client .net api

I'm writing opc client, using .NET API from opc foundation.

In samples I only see, where item's names are hardcoded like:

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

What I want, is not to write names of all items by my hands. I want to load all items from server, into tree for example. How can I do it开发者_如何转开发?


You can browse the server with the following construct:

using Opc.Da;
using Server=Opc.Da.Server;
using Factory=OpcCom.Factory;

string urlstring = string.Format("opcda://{0}/{1}/{{{2}}}", _hostName, _serverName, serverid);
Server s = new Server(new Factory(), new URL(urlstring));
s.Connect();
ItemIdentifier itemId = null;
BrowsePosition position;
BrowseFilters filters = new BrowseFilters() {BrowseFilter = browseFilter.item};
BrowseElement[] elements = s.Browse(itemId, filters, out position);

The tags are in elements[i].Name.


You load the items from XML file. You create XML file once with as many items as you want, then you only edit it when needed. To parse it, you can use MSXML DOM, for example. There is a parser in VB.NET Parsing XML file in VB.NET using DOM. But you can can search for implementation in C#.


Well, I'm not familiar with your opc client library, but you should be able to browse the servers items. This is a common feature used by many standalone OPC-Clients.


Public Function Browse(ByRef node As TreeNode, Optional id As Opc.ItemIdentifier = Nothing) As Integer
    Try
        Dim clone As Opc.Da.Server = your_connected_server
        Dim filters As New Opc.Da.BrowseFilters
        filters.BrowseFilter = Opc.Da.browseFilter.all
        Dim pos As Opc.Da.BrowsePosition = Nothing
        Dim elements() As Opc.Da.BrowseElement = clone.Browse(id, filters, pos)
        If (elements IsNot Nothing) Then
            For Each element As Opc.Da.BrowseElement In elements
                Console.WriteLine(element.ItemName)
                AddBrowseElement(node, element)
                If (element.HasChildren = True) Then
                    id = New Opc.ItemIdentifier(element.ItemPath, element.ItemName)
                    Browse(node.Nodes.Item(node.Nodes.Count - 1), id)
                End If
            Next
        End If
        Return 0
    Catch ex As Exception
        RaiseEvent OnException(GetCurrentMethod, ex)
        Return -1
    End Try
End Function

Private Sub AddBrowseElement(ByRef parent As TreeNode, element As Opc.Da.BrowseElement)
    Dim node As TreeNode = New TreeNode(element.Name)
    node.Text = element.Name
    node.Tag = element

    ' add properties
    If (element.Properties IsNot Nothing) Then
        For Each [property] As Opc.Da.ItemProperty In element.Properties
            AddItemProperty(node, [property])
        Next
    End If
    ' add to parent.
    parent.Nodes.Add(node)
End Sub


Use XML or NLOG or log4NET file to load the tag items.

Use opcdaauto.dll.Its a free dll for OPC Foundation Members

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜