Author Topic: OPC tag borwser  (Read 4125 times)

daveyh

  • Newbie
  • *
  • Posts: 23
    • View Profile
OPC tag borwser
« on: December 15, 2015, 03:10:07 PM »
Has anybody developed an app or know of any Visual basic or C code that browses an OPC server and shows the tag data in real time?

Thanks

xcore

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: OPC tag borwser
« Reply #1 on: May 05, 2016, 10:09:42 AM »
yes i have,

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: OPC tag borwser
« Reply #2 on: May 05, 2016, 04:57:29 PM »
xcore, could you share or provide more details?

daveyh, as a starting point you might take a look at these topics:

http://stackoverflow.com/questions/5077536/what-do-i-need-to-create-simple-c-sharp-app-which-uses-opc

http://mestaa.blogspot.ca/2010/11/opc-client-c-net-sample-rslinx-opc.html

I would be interested to learn more so anybody else can provide their input as well.
« Last Edit: May 05, 2016, 07:07:22 PM by Godra »

DougLyons

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
OPC tag borwser
« Reply #3 on: May 05, 2016, 11:30:04 PM »
For VB6 there is great source code available here:

https://info.kepware.com/visual-basic-source-code

This requires registration, but I have seen no ill effects from this.

Doug Lyons

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: OPC tag borwser
« Reply #4 on: May 08, 2016, 04:04:43 PM »
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):

Code: [Select]
    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:

Code: [Select]
    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
« Last Edit: October 25, 2016, 08:36:36 PM by Godra »