C # opc-client.net api

I am writing an opc client using the .NET API from the core of opc.

In the samples, I see only where the names of the elements are rigidly indicated as:

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

I want not to write the names of all objects with my hands. I want to load all the elements from the server, for example, into a tree. How can i do this?

+3
source share
5 answers

You can view the server with the following design:

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);

Tags are in the [i] .Name elements.

+6
source

XML. XML , , . , MSXML DOM. VB.NET XML VB.NET DOM. #.

+1

Well, I am not familiar with your opc client library, but you should be able to view server items. This is a common feature used by many standalone OPC clients.

0
source
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
0
source

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

Use opcdaauto.dll.Its free dll for OPC Foundation members

0
source

Source: https://habr.com/ru/post/1765107/


All Articles