?? opcbrowserclass.cls
字號:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "OPCBrowserClass"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' The OPCBrowserClass object houses all of the functionallity used
' to interact with an OPC server's tag browser. This OPCBrowserClass
' is used here largely to wrap the functions supplied by the Automation
' interface with error handlers. Unlike the OPCServerClass,
' OPCGroupClass, and OPCItemClass, the OPCBrowserClass simply provides
' consistancy in the of the error handling and rounds out the classes
' supplied by this example. As is the case with any object based
' design using this class module will allow you to modify the browser
' interface if needed. I have not wrapped all of the function supplied
' by the OPCBrowser object but adding others if needed is easy.
'
' In this example the frmAddItem form contains all of the real meat
' for actually browsing the tag space of an OPC Server. The main code
' is housed there due largely to the fact that most of what needs to be
' be done when browsing is the management of the Treeview and Listview
' controls used to display the tags.
Option Explicit
Option Base 1
' This is the actuial Automation Interface browser object this is
' wrapped by this class.
'
Dim OPCBrowserObject As OPCBrowser
' This function simply sets the OPCBrowse object that will be used by
' this instance of the OPCBrowserClass object. This is normally done
' by using the OPCServerClass function GetServerBrowseObject that
' should be used to nromally get this object class. You can however
' create an OPCBrowserClass object yourself if you have an OPCServer
' object upon which you can use the .CreateBrowser method and pass the.
' resulting OPCBrower object to this function.
'
Function SetBrowserObject(ByVal OPCBrowseObj As OPCBrowser)
Set OPCBrowserObject = OPCBrowseObj
End Function
' This function allows you to determine the format of the browse space
' available from the connected OPC Server. There are two possible
' types of browse space your application may encounter. There
' is Flat(OPCFlat = 2) and Hierarchial(OPCHierarchical = 1}. Most
' OPC Servers you will emcounter by default support a Hierarchial
' browse space. Hierarchial means that the tags or items available
' in the server will be organized into a tree like structure with
' branches and leafs. This organiztion is the same as what you
' see in your Window's Explorer where you have directories and files.
' In Hierarchial mode the your code must be able to move up and down
' the branches of the tree inorder to access the various tags found at
' each level in the tree.
'
' In a Flat browse space the server returns all of the items/tags
' at a single level. Just because the server returns all of the items
' at a single level doesn't mean the the item IDs for all of item will
' be simple references. The item IDs may still contain complex grouping
' information that your application may find useful.
'
' In this example the frmAddItem form contains all of the actual
' code that does the real work of moving around in the browse space
' of of the OPC Server.
'
Function GetBrowserOrganization(ByRef Organization As Long)
'Set error handling for OPC Function
On Error GoTo ShowOPCGetBrowserOrganizationError
Organization = OPCBrowserObject.Organization
GetBrowserOrganization = True
GoTo SkipOPCGetBrowserOrganizationError
ShowOPCGetBrowserOrganizationError:
Call DisplayOPC_COM_ErrorValue("GetBrowserOrganization", Err.Number)
GetBrowserOrganization = False
SkipOPCGetBrowserOrganizationError:
End Function
' The next two functions are used to move around in the browser space
' of the OPC Server. When browsing an OPC server you must tell the
' server where to go on it's browse tree. This is down using the
' following functions. By default you start at the root of the
' OPC Server's browse space. You then use the function ShowBranches
' and GetItemCount to determine if there are any other branches
' available from this point in the tree. If there are more branches
' available you can use the MoveDown function and specify a branch
' to move down to. Once you move down a level you will need to call the
' ShowBranches function again to see if there are branches at this new
' level of the OPC server browse space. These steps are repeated until
' you reach the end of a paticular brance. Additionally as you move to
' each branch including the root you will also be calling ShowLeafs
' to determine of there are any tags available at a given branch.
'
' In this exmaple I use only the MoveToRoot and MoveDown functions
' to browse the server. In the frmAddItem form you will see that
' I keep track of the branch structure as you navigate the browse
' space. This allows me to simply move to root and then do a series
' of MoveDown operations to get to the desired branch. The OPCBrowser
' object also supports a MoveUp and MoveTo operation. I don't use
' either in this example.
' The MoveToRoot function will return the OPC Server's browse position
' to the root of the browse space. This makes staying in sync with the
' the OPC Server's position in the browse space easy. See the
' frmAddItem form for more on the use of this function.
'
Function MoveToRoot()
'Set error handling for OPC Function
On Error GoTo ShowOPCMoveToRootError
OPCBrowserObject.MoveToRoot
MoveToRoot = True
GoTo SkipOPCMoveToRootError
ShowOPCMoveToRootError:
Call DisplayOPC_COM_ErrorValue("MoveToRoot", Err.Number)
MoveToRoot = False
SkipOPCMoveToRootError:
End Function
' The MoveDown function moves the OPC Server's browse position down
' to the branch specified by the string "Branch". When passing a
' branch name to this function you should try to use the branch name
' exactly as it was given to you by the ShwoBranches function. This
' will insure that you don't give the server an improper name and get
' the OPC Server's browse space out of sync with you own. See the
' frmAddItem form for more on the use of this function.
'
Function MoveDown(ByVal Branch As String)
'Set error handling for OPC Function
On Error GoTo ShowOPCMoveDownError
OPCBrowserObject.MoveDown (Branch)
MoveDown = True
GoTo SkipOPCMoveDownError
ShowOPCMoveDownError:
Call DisplayOPC_COM_ErrorValue("MoveDown", Err.Number)
MoveDown = False
SkipOPCMoveDownError:
End Function
' The MoveUp function move the OPC Server's browse position up
' one level from the current position. While you can use this
' function in conjunction with the MoveDown to navigate the
' browse space of teh OPC server. I suggest that you only use
' the MoveDown and MoveToRoot functions to traverse the browse
' space. You will see more in the frmAddItem form code.
'
Function MoveUp()
'Set error handling for OPC Function
On Error GoTo ShowOPCMoveUpError
OPCBrowserObject.MoveUp
MoveUp = True
GoTo SkipOPCMoveUpError
ShowOPCMoveUpError:
Call DisplayOPC_COM_ErrorValue("MoveUp", Err.Number)
MoveUp = False
SkipOPCMoveUpError:
End Function
' The GetItemCount function servers two purposes depending the
' function calls that proceed it. If the ShowBranches function
' is called prior to GetItemCount, the value returned here will
' be the number of branches available under the current browse
' position of the OPC server.
' If the ShowLeafs function is called prior to GetItemCount,
' the value returned here will be the number of leafs(Items/Tags)
' available at the current browse position of the OPC server.
'
Function GetItemCount()
'Set error handling for OPC Function
On Error GoTo ShowOPCGetItemCountError
GetItemCount = OPCBrowserObject.Count
GoTo SkipOPCGetItemCountError
ShowOPCGetItemCountError:
Call DisplayOPC_COM_ErrorValue("GetItemCount", Err.Number)
GetItemCount = 0
SkipOPCGetItemCountError:
End Function
' The ShowBranches function will cause the OPC server to load the
' the OPCBrowse object's item collection with any branches available
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -