⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 opcbrowserclass.cls

📁 KEPware Automation DLL installation instructions: To use the KEPware Automation DLL in your Visua
💻 CLS
📖 第 1 页 / 共 2 页
字号:
' below the current browse position.  You can then determine if there
' are any branches by calling the the GetItemCount function.  If there
' are branches available they can be retrieved using the GetItemName
' function.  As described above the OPCBrowse object only keeps track
' of one collection at a time.  If you call the ShowBranches function
' you should immediately call the GetItemCount function then get all of
' the branch names in the collection using the GetItemName function.
'
' If you call ShowBranches then call ShowLeafs, the OPCBrowse object
' will load it's internal collection first with the brances then
' overwrite the branch collection with the Leafs collection thus
' losing the content and count of branches.
'
Function ShowBranches()
'Set error handling for OPC Function
    On Error GoTo ShowOPCShowBranchesError
    
    OPCBrowserObject.ShowBranches
    ShowBranches = True
    GoTo SkipOPCShowBranchesError
    
ShowOPCShowBranchesError:
    Call DisplayOPC_COM_ErrorValue("ShowBranches", Err.Number)
    ShowBranches = False
SkipOPCShowBranchesError:

End Function

' The ShowLeafs function will cause the OPC server to load the
' the OPCBrowse object's item collection with any leafs(item/tags)
' available at the current browse position.  You can then determine
' if there are any leafs by calling the the GetItemCount function.
' If there are leafs available they can be retrieved using the
' GetItemName function.  As described above the OPCBrowse object only
' keeps track of one collection at a time.  If you call the ShowLeafs
' function you should immediately call the GetItemCount function then
' get all of the leaf names in the collection using the GetItemName
' function.
'
' If you call ShowLeafs then call ShowBranches, the OPCBrowse object
' will load it's internal collection first with the leafs then
' overwrite the leaf collection with the branch collection thus
' losing the content and count of leafs.
'
Function ShowLeafs(Optional ByVal Mode As Variant)
'Set error handling for OPC Function
    On Error GoTo ShowOPCShowLeafsError
    
    OPCBrowserObject.ShowLeafs Mode
    ShowLeafs = True
    GoTo SkipOPCShowLeafsError
    
ShowOPCShowLeafsError:
    Call DisplayOPC_COM_ErrorValue("ShowLeafs", Err.Number)
    ShowLeafs = False
SkipOPCShowLeafsError:

End Function

' The GetItemName function returns the name of the item in the
' OPCBrowse object's collection specified by the "ItemSpecifier".
' The OPCBrowse object's collection is loaded when you call either
' ShowBranches or ShowLeafs.  For the same reasons described in those
' functions you must retrieve all of the item names in a given
' collection before changing the collection.
' The "ItemSpecifier" should not exceed the value returned by the
' GetItemCount function.
'
Function GetItemName(ByRef ItemName As String, ByVal ItemSpecifier As Variant)
'Set error handling for OPC Function
    On Error GoTo ShowOPCGetItemNameError
    
    ItemName = OPCBrowserObject.Item(ItemSpecifier)
    GetItemName = True
    GoTo SkipOPCGetItemNameError
    
ShowOPCGetItemNameError:
    Call DisplayOPC_COM_ErrorValue("GetItemName", Err.Number)
    GetItemName = False
SkipOPCGetItemNameError:

End Function

' The three following functions allow you to filter the amount of
' data retuned in by either the ShowBranches or ShowLeafs functions.
' In this example I don't allow the branches to be filtered only
' leafs(items/tags).
' Filtering the number of leafs retuned in the OPCBroswe objects
' collection can make find a specific itme in the server far easier.
' This becomes apparent quickly in servers with large tag counts.
' Using these three function you can reduce the number of tags
' by filtering the tag's name, Datatype, or Access method.

' The SetFilter function allows you to specify a filter to be
' used on the acutal name of a leaf(item/tag).  By the default there
' is no filter set. This can be specified by either calling this
' funciton with an empty string "", or by calling this function with
' a string of "*".
'
' To reduce the number of items returned in the ShowLeafs function you
' can use wildcard characters such as *, ?, and name fragments such as
' "Position_*".  Using this example, the SowLeafs function would return
' all items that started with the string "Position_".
'
Function SetFilter(ByVal Filter As String)
'Set error handling for OPC Function
    On Error GoTo ShowOPCSetFilterError
    
    OPCBrowserObject.Filter = Filter
    SetFilter = True
    GoTo SkipOPCSetFilterError
    
ShowOPCSetFilterError:
    Call DisplayOPC_COM_ErrorValue("SetFilter", Err.Number)
    SetFilter = False
SkipOPCSetFilterError:
   
End Function

' In addition to filtering on the name of an item you can also filter
' based on the data type of an item.  If you want to see only itme that
' are defined as a data type of 16 bit signed integer (VT_I2) you can
' use this function set the filter data type to short (VT_I2).
'
Function SetDataTypeFilter(ByVal DataType As Integer)
'Set error handling for OPC Function
    On Error GoTo ShowOPCSetDataTypeFilterError
    
    OPCBrowserObject.DataType = DataType
    SetDataTypeFilter = True
    GoTo SkipOPCSetDataTypeFilterError
    
ShowOPCSetDataTypeFilterError:
    Call DisplayOPC_COM_ErrorValue("SetDataTypeFilter", Err.Number)
    SetDataTypeFilter = False
SkipOPCSetDataTypeFilterError:
   
End Function

' The SetAccessFilter function like the SetDataTypeFilter function
' allows you to filter the number of items retuned based on the Access
' rights of the item.  The Access rights in this cases are the item's
' Read or Write access.
'
Function SetAccessFilter(ByVal Access As Long)
'Set error handling for OPC Function
    On Error GoTo ShowOPCSetAccessFilterError
    
    OPCBrowserObject.AccessRights = Access
    SetAccessFilter = True
    GoTo SkipOPCSetAccessFilterError
    
ShowOPCSetAccessFilterError:
    Call DisplayOPC_COM_ErrorValue("SetAccessFilter", Err.Number)
    SetAccessFilter = False
SkipOPCSetAccessFilterError:
   
End Function

' Once you have browsed to a particular branch and possibly filter the
' list of items down to the few items of interest you still need to
' get the desired item's ItemID string.  The GetItemID function allows
' you to get the item ID of the "Leaf" parameter.  The leaf passed
' to this function must be available from the current browse
' position of the OPC Server.
'
' In many cases the GetItemID will return a string that closely
' matches the branch organization of the OPC server being browsed.
' You should not count on this however.  The format of the branches
' and leafs presented by the OPC server via it's browse space
' may be soley for benifit of the human user.  The ItemID returned
' may not match this branch.leaf format.
'
Function GetItemID(ByVal Leaf As String)
'Set error handling for OPC Function
    On Error GoTo ShowOPCGetItemIDError
    
    GetItemID = OPCBrowserObject.GetItemID(Leaf)
    GoTo SkipOPCGetItemIDError
    
ShowOPCGetItemIDError:
    Call DisplayOPC_COM_ErrorValue("GetItemID", Err.Number)
    GetItemID = ""
SkipOPCGetItemIDError:

End Function


' Handles displaying any OPC/COM/VB errors that are caught by the exception handler
Sub DisplayOPC_COM_ErrorValue(OPC_Function As String, ErrorCode As Long)
    Dim Response
    Dim ErrorDisplay As String
    ErrorDisplay = "The OPC function '" + OPC_Function + "' has returned an error of " + Str(ErrorCode) + " or Hex 0x" + Hex(ErrorCode)
    Response = MsgBox(ErrorDisplay, vbOKOnly, "OPC Function Error")
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -