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

📄 opcgroupclass.cls

📁 VB开发OPC Client的教程和源码
💻 CLS
📖 第 1 页 / 共 3 页
字号:
    On Error GoTo ShowOPCGetGroupUdateRateError
    
    UpdateRate = OPCGroupObj.UpdateRate
    GetGroupUpdateRate = True
    GoTo SkipGetGroupUdateRateError

ShowOPCGetGroupUdateRateError:
    Call DisplayOPC_COM_ErrorValue("Get Group Update Rate", Err.Number)
    GetGroupUpdateRate = False
SkipGetGroupUdateRateError:
End Function

' This function allows you to set the default data type for the addition
' of items to a group. When adding an item to an OPC group the item's data
' type can be set using the default data type accessed here.  You will see
' that items are added to the OPCGroupClass object the default data type
' is set for each item that is added.
'
Function SetOPCItemsDefaultDataType(ByVal DataType As Integer)
    'Set error handling for OPC Function
    On Error GoTo ShowOPCItemsDefaultDataTypeError
    
    OPCGroupObj.OPCItems.DefaultRequestedDataType = DataType
    SetOPCItemsDefaultDataType = True
    GoTo SkipOPCItemsDefaultDataTypeError

ShowOPCItemsDefaultDataTypeError:
    Call DisplayOPC_COM_ErrorValue("Set OPC Item Default Data Type", Err.Number)
    SetOPCItemsDefaultDataType = False
SkipOPCItemsDefaultDataTypeError:
End Function

' This function gets the default item data type of a group
'
Function GetOPCItemsDefaultDataType(ByRef DataType As Integer)
    'Set error handling for OPC Function
    On Error GoTo ShowOPCGetItemsDefaultDataTypeError
    
    DataType = OPCGroupObj.OPCItems.DefaultRequestedDataType
    GetOPCItemsDefaultDataType = True
    GoTo SkipOPCGetItemsDefaultDataTypeError

ShowOPCGetItemsDefaultDataTypeError:
    Call DisplayOPC_COM_ErrorValue("Get OPC Item Default Data Type", Err.Number)
    GetOPCItemsDefaultDataType = False
SkipOPCGetItemsDefaultDataTypeError:
End Function

' This function allows you to set the default active state of items
' being added to the group.  If you want to add item to an OPC group
' but do not want the server to start accessing those values immediately
' you can change the default active state for the items to inactive.
' As you add items to the group they will be added in an inactive state.
' Once the items have been added to the group you can enable them for
' reading by using the 'SetItemActiveState' function found in the
' OPCItemClass.
'
Function SetOPCItemsDefaultActive(ByVal ActiveState As Integer)
    'Set error handling for OPC Function
    On Error GoTo ShowOPCItemsDefaultActiveError
    
    OPCGroupObj.OPCItems.DefaultIsActive = ActiveState
    SetOPCItemsDefaultActive = True
    GoTo SkipOPCItemsDefaultActiveError

ShowOPCItemsDefaultActiveError:
    Call DisplayOPC_COM_ErrorValue("Set OPC Item Default Active State", Err.Number)
    SetOPCItemsDefaultActive = False
SkipOPCItemsDefaultActiveError:
End Function

' This function gets the default active state for items added to this
' group.
Function GetOPCItemsDefaultActive(ByRef ActiveState As Integer)
    'Set error handling for OPC Function
    On Error GoTo ShowOPCGetItemsDefaultActiveError
    
    ActiveState = OPCGroupObj.OPCItems.DefaultIsActive
    GetOPCItemsDefaultActive = True
    GoTo SkipOPCGetItemsDefaultActiveError

ShowOPCGetItemsDefaultActiveError:
    Call DisplayOPC_COM_ErrorValue("Get OPC Item Default Active State", Err.Number)
    GetOPCItemsDefaultActive = False
SkipOPCGetItemsDefaultActiveError:
End Function


' This function handles adding an item to the group and
' establishing the item interface.  When adding an item you
' can preset some of the item's parameters using the functions
' 'SetOPCItemsDefaultActive' and 'SetOPCItemsDefaultDataType'.
' These are set before adding the item. Once the item has been
' successfully added you can change these same settings on the
' fly using the properties on the resulting OPCItemClass object.
' The OPCItemID parameter is a fully qualified item name within the
' desired OPC server.  If this string is incorrect the server will
' return an error.  The DataType is the desired data type for this
' item.  The server may accept or reject this data type.  The
' ActiveState parameter allows you to make this item active or inactive
' from the momment it is added to the group.
'
' The ItemKey is string that will be returned to the VB application.
' This string can be used as a key for this item in a collection.
'
Function AddOPCItem(ByVal OPCItemID As String, ByVal DataType As Integer, ByVal ActiveState As Integer, ByRef ItemKey As String)
    
   'Set error handling for OPC Function
    On Error GoTo ShowOPCItemAddError
    
    Dim ItemToAdd As New OPCItemClass
    Dim NewItem As OPCItem
    Dim ItemIndex As Integer
    Dim TmpOPCItemID As String
              
    ' Establish the initial default conditions for new items added to this
    ' group.
    SetOPCItemsDefaultActive (ActiveState)
    SetOPCItemsDefaultDataType (DataType)
    TmpOPCItemID = OPCItemID
    
    ' Search the existing items and find the next available index number
    ItemIndex = FindNextItemNumber
        
    ' The group is added to the server using the group name that is derived
    ' when the group is created.  This is the same key name used in the tree
    ' view
    Set NewItem = OPCGroupObj.OPCItems.AddItem(OPCItemID, ItemIndex)
        
    If Not NewItem Is Nothing Then
        ItemToAdd.SetOPCItem NewItem, TmpOPCItemID, ItemIndex, DataType
       
        ' Although the OPCGroups (ServerGroups) object is already a collection we need to have our
        ' own class wrapper to contain the properties and event for each group we
        ' intend to add to the server.
        ItemKey = "Item" + Str(ItemIndex)
        With OPCGroupItems
            .Add ItemToAdd, Str(ItemIndex)
        End With
    Else
        AddOPCItem = False ' Signify a failure of this function
        Exit Function
    End If
        
    AddOPCItem = True
    
    GoTo SkipAddItemError

ShowOPCItemAddError:
    Call DisplayOPC_COM_ErrorValue("Add Item", Err.Number)
    AddOPCItem = False
SkipAddItemError:
    
End Function


' This sub handles removing an item from the OPC group.  The item
' is removed from the OPCItems object which is the collection
' managed by the automation interface.  Then the item is removed from
' the OPCGroupItems collection which is managed by the OPCGroupClass
'
Function RemoveOPCItem(ItemKey As String, ByRef Error As Long)
    'Set error handling for OPC Function
    On Error GoTo ShowOPCItemRemoveError
            
    Dim OPCItemToRemove As OPCItemClass
    Dim ServerHandles(1) As Long
    Dim Errors() As Long
    Dim Count As Long
    
    ' First the item is removed from the actual OPCItems collection managed by
    ' the automation interface
    Set OPCItemToRemove = OPCGroupItems.Item(Mid(ItemKey, InStr(ItemKey, " ")))
    ServerHandles(1) = OPCItemToRemove.GetItemServerHandle
    Count = 1
    OPCGroupObj.OPCItems.Remove Count, ServerHandles, Errors
    
    If Errors(1) <> 0 Then
        Error = Err.Number = Errors(1)
        GoTo ShowOPCItemRemoveError
    End If
    
    ' Now we remove it from the OPCGroupItems collection
    ' to allow any final processing on the item to be done
    OPCGroupItems.Remove (Mid(ItemKey, InStr(ItemKey, " ")))
        
    RemoveOPCItem = True
    GoTo SkipRemoveItemError

ShowOPCItemRemoveError:
    Call DisplayOPC_COM_ErrorValue("Remove Item", Err.Number)
    RemoveOPCItem = False
SkipRemoveItemError:
End Function


' This function attempts to keep the item index number
' from continuosly growing by finding an item number that
' may have been deleted and returning those first before
' going to the next item index number. The end result of
' this function it that the item number will never grow
' beyond actual number of items.
'
Private Function FindNextItemNumber()
    ' In this function an error state menas that a vacant
    ' item number has been found.
    On Error GoTo FoundNextItemNumber
    
    Dim i As Integer
    Dim ItemNum As Integer
    
    ItemNum = 1
    
    With OPCGroupItems
        ' This loop tries to access each item in the OPCGroupItems
        ' collection by it's Key.  If an item can not be pulled from the
        ' collection that means the item had been deleted and it's
        ' item number is available.
        For i = 1 To .Count
            ItemNum = i
            .Item (Str(i))
        Next i
        
        ' If we get here all of the item numbers in the collection
        ' exist and a next available item number should be returned.
        If .Count <> 0 Then
            FindNextItemNumber = i
        Else
            FindNextItemNumber = 1 ' No count return the first 1
        End If
    End With
    
    GoTo NewIndex
    
FoundNextItemNumber:
    ' If we get here an item number could not be found in the
    ' OPCGroupItems collection which means that it has been deleted
    ' and is available for a new item addition.
    FindNextItemNumber = ItemNum
NewIndex:
End Function


' The ValidateOPCItem function is a valuable tool in your OPC application.
' The validate function allows you to test an item before it is
' actually added to the OPC group.  The validate function will test the
' Item ID, DataType, and ActiveState of the item.  If all of these

⌨️ 快捷键说明

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