|
Please use our Code Center to obtain source-code and example projects/solutions:
http://codecenter.softwaretoolbox.com
|
|
|

|
|
|
|
|

|
|
OPC Browsing
Be sure to check out the OPC Browser Tutorial.
This page shows you how to:
- Browse available PC's
- Browse available OPC Servers (using the dialog and via code)
- Browse available OPC Groups/Branches (via code)
- Browse available OPC Tags (using the dialog and via code)
Browsing available PCs
'define a browser object Dim o As New
OPCUserObjects.OPCUserBrowseMachine
'run the dialog o.RunMe()
'show the result
MessageBox.Show(o.MachineName)
Browsing Available OPC Servers
Via Dialog Browser
'define our browser object Dim o As New
OPCUserObjects.OPCUserBrowseMachine
'specify the machine we will browse 'no value means local computer o
.MachineName = ""
'run the dialog o.RunMe()
'now to check the result
'if the serverclass is blank and the machine is blank 'then nothing was selected If ( _ o.ServerClass
.Equals(Text.Empty) _ And _ o.MachineName.Equals(Text.Empty) _ ) Then
'nothing selected MessageBox.Show("Dialog cancelled")
Else 'user picked... MessageBox.Show(o.ServerClass) End If
Via Code
The following code assumes that an empty for exists with a single listbox control in it. Place the following code in the Form_load event:
'
'display a message in the listbox to acknowledge that something is happening... ListBox1.Items.Add("please wait...") ListBox1.Update()
' 'now to do the opc server browsing Try '
'define ourselves an opc web client Dim objOWC As New OPCLabs.EasyOPCDANet.EasyDACOM
'
'this is the name of the computer we want to look at Dim strMachineName As String = ""
' 'now capture the browsed servers into a dictionary object Dim ServerElements As IDictionary =
objOWC.BrowseServers(strMachineName)
'
'now clear any messages/items in the listbox ListBox1.Items.Clear()
If Not ServerElements Is Nothing Then ' 'now iterate thru each element within the dictionary For Each key As Object In ServerElements.Keys
' 'now get the server element from the collection into a variable Dim server As
OPCLabs.EasyOPCDANet.IServerElementNet = ServerElements.Item(key)
'
'add the progid of the opc server into the listbox ListBox1.Items.Add(server.ProgID)
Next End If
Catch ex As Exception '
'display any exceptions in a messagebox MessageBox.Show("Error: " & ex.Message) End Try
Browsing Available OPC Groups/Branches
The following code assumes that an empty form exists with a single listbox control in it. Place the following code in the Form_load event:
Try 'lets create an instance of our OPC Web Client
'we will use the COM interface (not the webservice) Dim EasyDA As New OPCLabs.EasyOPCDANet.EasyDACOM
'
'now lets define a dictionary collection Dim BranchElements As System.Collections.IDictionary
'
'the name of the computer we want to browse on Dim strMachineName As String ""
' 'capture the results of the browseBranches function into the dictionary BranchElements = EasyDA
.BrowseBranches(strMachineName, "OPCLabs.KitServer.2", "")
'
'make sure our dictionary is not empty/null If Not BranchElements Is Nothing Then
'
'lets now iterate thru the elements within the dictionary For Each Key As Object In BranchElements.Keys
' 'define a branch object and set it to the object in the dictionary object
'at the position specified by the "key" variable, which happens 'to be the "name" of the branch.
Dim BranchElement As OPCLabs.EasyOPCDANet.INodeElementNet = BranchElements.Item(Key)
'
'if this is a branch, then add it to our listbox If BranchElement.IsBranch Then
ListBox1.Items.Add(BranchElement.ItemID) End If Next
End If
Catch ex As Exception '
'show any exception in a message box MessageBox.Show("Error: " & ex.Message) End Try
Browsing Available Tags
Via Dialog Browser
'create a tag browser object Dim objOWC As New
OPCUserObjects.OPCUserBrowseItem
'set the server we want to browse 'along with the machine it is on objOWC.MachineName = ""
objOWC.ServerClass = "OPCLabs.KitServer.2"
'run the dialog objOWC.RunMe()
'now to see what was selected If (objOWC.ItemID.Equals(Text.Empty)) Then
'nothing was selected, or the dialog was cancelled
MessageBox.Show("Nothing selected") Else
'the user picked something... MessageBox.Show(objOWC
.ItemID) End If
|
|

|
|
|
|

|
|
|