The following code can be used to see what tags are available on the local OPC server (it was tested as working with both MatrikonOPC and Kepware servers):
Private hostName As String = "localhost"
Private serverName As String = "Matrikon.OPC.Simulation.1" 'or "Kepware.KEPServerEX.V5"
Private urlstring As String = String.Format("opcda://{0}/{1}", hostName, serverName)
Private serv As New Opc.Da.Server(New OpcCom.Factory, New Opc.URL(urlstring))
Private filtersBranch As New Opc.Da.BrowseFilters With {.BrowseFilter = Opc.Da.browseFilter.branch}
Private filtersItem As New Opc.Da.BrowseFilters With {.BrowseFilter = Opc.Da.browseFilter.item}
Private pos As Opc.Da.BrowsePosition = Nothing
Private id As Opc.ItemIdentifier = Nothing
Private rootNodes, childrenNodes, childrenTags As Opc.Da.BrowseElement()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.lblOPCServer.Text = serverName
serv.Connect()
rootNodes = serv.Browse(id, filtersBranch, pos)
If rootNodes IsNot Nothing Then
For Each rootNode As Opc.Da.BrowseElement In rootNodes
Me.TreeView1.SelectedNode = Me.TreeView1.Nodes.Add(rootNode.Name)
If rootNode.HasChildren Then
id = New Opc.ItemIdentifier(rootNode.ItemPath, rootNode.ItemName)
childrenNodes = serv.Browse(id, filtersBranch, pos)
If childrenNodes IsNot Nothing Then
For Each childNode As Opc.Da.BrowseElement In childrenNodes
Me.TreeView1.SelectedNode = Me.TreeView1.SelectedNode.Nodes.Add(childNode.Name)
If childNode.HasChildren Then
id = New Opc.ItemIdentifier(childNode.ItemPath, childNode.ItemName)
childrenTags = serv.Browse(id, filtersItem, pos)
If childrenTags IsNot Nothing Then
For Each childTag As Opc.Da.BrowseElement In childrenTags
Me.TreeView1.SelectedNode.Nodes.Add(childTag.Name)
Next
Me.TreeView1.SelectedNode = Me.TreeView1.SelectedNode.Parent
End If
End If
Next
End If
End If
Next
End If
serv.Disconnect()
End Sub
In the AdvancedHMI project properties window, add a reference to both OpcNetApi.dll and OpcNetApi.Com.dll files found in the Support\OPC directory inside the AdvancedHMIDrivers project.
I guess that, with additional code, it would be possible to see the real time data as well but those tags would probably have to be active (subscribed to). There might be a possibility to just use OPC driver to achieve all these things (Archie would have to provide some input on that).
The attached pictures show how I tested it. There are 3 standard Windows Forms controls: button, label and treeview and the above code is reflective of those.
If you wish to browse more than one OPC server at Runtime, instead of using a label you should use a combobox control and set its Items collection similar to the attached picture (change only the server names if necessary) and then use the code below:
Private Sub MainForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Me.ComboBox1.SelectedIndex = 0
Me.Button1.Enabled = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.TreeView1.Nodes.Clear()
Dim hostName As String = "localhost"
Dim serverName As String = Me.ComboBox1.SelectedItem
Dim rootNodes, childrenNodes, childrenTags As Opc.Da.BrowseElement()
Dim urlstring As String = String.Format("opcda://{0}/{1}", hostName, serverName)
Dim serv As New Opc.Da.Server(New OpcCom.Factory, New Opc.URL(urlstring))
Dim filtersBranch As New Opc.Da.BrowseFilters With {.BrowseFilter = Opc.Da.browseFilter.branch}
Dim filtersItem As New Opc.Da.BrowseFilters With {.BrowseFilter = Opc.Da.browseFilter.item}
Dim pos As Opc.Da.BrowsePosition = Nothing
Dim id As Opc.ItemIdentifier = Nothing
serv.Connect()
rootNodes = serv.Browse(id, filtersBranch, pos)
If rootNodes IsNot Nothing Then
For Each rootNode As Opc.Da.BrowseElement In rootNodes
Me.TreeView1.SelectedNode = Me.TreeView1.Nodes.Add(rootNode.Name)
If rootNode.HasChildren Then
id = New Opc.ItemIdentifier(rootNode.ItemPath, rootNode.ItemName)
childrenNodes = serv.Browse(id, filtersBranch, pos)
If childrenNodes IsNot Nothing Then
For Each childNode As Opc.Da.BrowseElement In childrenNodes
Me.TreeView1.SelectedNode = Me.TreeView1.SelectedNode.Nodes.Add(childNode.Name)
If childNode.HasChildren Then
id = New Opc.ItemIdentifier(childNode.ItemPath, childNode.ItemName)
childrenTags = serv.Browse(id, filtersItem, pos)
If childrenTags IsNot Nothing Then
For Each childTag As Opc.Da.BrowseElement In childrenTags
Me.TreeView1.SelectedNode.Nodes.Add(childTag.Name)
Next
Me.TreeView1.SelectedNode = Me.TreeView1.SelectedNode.Parent
End If
End If
Next
End If
End If
Next
End If
serv.Disconnect()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If Me.ComboBox1.SelectedIndex <> 0 Then
Me.Button1.Enabled = True
Else
Me.Button1.Enabled = False
End If
End Sub