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

📄 opcitemclass.cls

📁 VB开发OPC Client的教程和源码
💻 CLS
字号:
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "OPCItemClass"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' The OPCItemClass object houses all of the functionallity used
' to interact with OPC Items.  The OPCItemClass is used primarily
' by the OPCGroupClass.  When AddOPCItem is called on an instance of
' the OPCGroupClass object, it creates an instance of a OPCItemClass
' object.  In general you normally won't directly create a OPCItemClass
' object.  You gain access to them as you add items to your OPCGroupClass
' object.  Each item you add to the group is given a unique item key
' name.  The OPCItemClass object wraps the Automation interface OPCItem
' object. This diagram shows the relationships between the various
' components of the Automation interface wrapper.
'
'    OPCServer
'           |
'           +-OPC Groups (Collection)
'                      |
'                      +-OPC Group
'                                |
'                                +-OPC Items (Collection)
'                                           |
'    >                                      +-OPC Items     <
'
' The OPCItemClass object handles functions found in the last branch
' of this diagram, the OPCitem object.  I have not implemented all of
' the methods and properties found in this object.  Adding access to
' any methods or properties not currently in this module is easy.
'
' While you won't normally directly create an OPCItemClass object you
' will utilize the functions found in this object.


Option Explicit
Option Base 1

' The OPCItemObj is the Automation Interface OPCItem object.  This is the
' actual object returned by the Automation Interface when the item is added
' to the group in the OPCGroupClass function AddOPCItem.
Dim OPCItemObj As OPCItem

' The OPCOItemID contains the fully qualified item reference to this item.
' In this case of this example and KEPServerEX a typical ItemID might
' appear as "Channel_1.Device_1.R100".  While not as effecient as using the
' actual collection key generated by the ItemIndex, the OPCItemID can be used
' to identify an OPC item in your application.
Dim OPCItemID As String

' The Item Value is a variant used to hold whatever data value that is
' returned by the item refernced in the OPCItemID.  Keep in mind that the
' Variant type my be a single value or an array of values.  To determine
' what type the data is see either the Timer function found in the main
' form of this example or the WriteNewValue function found on the
' frmWriteItem form.
Dim ItemValue As Variant

' The Item quality contains the error condition of the item.  Normally you
' would think that a zero is no error and anything else was error.  In OPC
' terminology the quality value is a bit field.  As a bit field the value
' contained in the quality data is non zero when the data is good.  A value
' &HCO indicates that the item is good and has no error.
Dim ItemQuality As Long

' The ItemTimeStamp contains the time of the last read operation for this item.
' Using this data your application can determine if the data available for
' this item is fresh enough to be used.
Dim ItemTimeStamp As Date

' The ItemDataType contains the datatype of the OPC item as it was requested
' by your application.  This does not however guarantee that the OPC server has
' not returned the data in another format.  You should check the variant type
' of data returned from the OPC server for this item.
Dim ItemDataType As Integer
Dim ItemIndex As Long


' This function is called from OPCGroupClass function AddOPCItem to
' establish the contents of this tag.
'
Sub SetOPCItem(OPCItemObject As OPCItem, OPCItem_IDval As String, ByVal OPCItemIndex As Long, ByVal ItmDataType As Integer)
    ' Assign the group object for this instance of the class
    Set OPCItemObj = OPCItemObject
    Dim newString As String
    newString = OPCItem_IDval
    OPCItemID = newString
    
    ItemIndex = OPCItemIndex
    ' This is the data type that was requested during the AddItem call
    ' This may not be the actual datatype of the item once the OPC Server accepts the item
    ' Use the GetItemDataType in OPCItemDirect mode to find what type the OPC
    ' Server has assigned to the item
    ItemDataType = ItmDataType
End Sub

' This function is called from the DataChange event handler found in
' the OPCGroupClass.  The function simply updates the fields for this item
'
Sub UpdateOPCItemData(ByVal ItmVal As Variant, ByVal ItmQuality As Long, ByVal ItmTime As Date)

    ItemValue = ItmVal
    ItemQuality = ItmQuality
    ItemTimeStamp = ItmTime

End Sub

' This function returns the ItemID string for this item.
'
Function GetItemID()
    GetItemID = OPCItemID
End Function

' This function returns the ItemIndex for this item.
'
Function GetItemIndex()
    GetItemIndex = ItemIndex
End Function

' This function returns a value for this item.  The value returned can
' be either the local ItemValue or the Item value as it is contained in the
' OPC Servers copy of the Automation Interface's OPCItem object.  The mode
' parameter selects which item will be read.
'
Function GetItemValue(ByVal Mode As Integer)
    'Set error handling for OPC Function
    On Error GoTo ShowOPCGetItemValueError
    
    If Mode = OPCItemDirect Then
        GetItemValue = OPCItemObj.Value
    Else
        GetItemValue = ItemValue
    End If
GoTo SkipOPCGetItemValueError

ShowOPCGetItemValueError:
    Call DisplayOPC_COM_ErrorValue("GetItemValue", Err.Number)
SkipOPCGetItemValueError:

End Function

' This function returns a quality for this item.  The value returned can
' be either the local quality value or the quality value as it is contained
' in the OPC Servers copy of the Automation Interface's OPCItem object.
' The mode parameter selects which value will be read.
'
Function GetItemQuality(ByVal Mode As Integer)
    'Set error handling for OPC Function
    On Error GoTo ShowOPCGetItemQualityError
    
    If Mode = OPCItemDirect Then
        GetItemQuality = OPCItemObj.Quality
    Else
        GetItemQuality = ItemQuality
    End If
    
GoTo SkipOPCGetItemQualityError

ShowOPCGetItemQualityError:
    Call DisplayOPC_COM_ErrorValue("GetItemQuality", Err.Number)
SkipOPCGetItemQualityError:

End Function


' This function allows the quality of this item to be set. This is used in
' the AsyncWriteComplete call back event of teh OPCGroupClass object.
'
Function SetItemQuality(ByVal Quality As Long)
    ItemQuality = Quality
End Function

' This function returns a time stamp for this item.  The value returned can
' be either the local time stamp or the time stamp as it is contained
' in the OPC Servers copy of the Automation Interface's OPCItem object.
' The mode parameter selects which value will be read.
'
 Function GetItemTimeStamp(ByVal Mode As Integer)
    'Set error handling for OPC Function
    On Error GoTo ShowOPCGetItemTimeStampError
    
    If Mode = OPCItemDirect Then
        GetItemTimeStamp = OPCItemObj.TimeStamp
    Else
        GetItemTimeStamp = ItemTimeStamp
    End If
    
GoTo SkipOPCGetItemTimeStampError

ShowOPCGetItemTimeStampError:
    Call DisplayOPC_COM_ErrorValue("GetItemTimeStamp", Err.Number)
SkipOPCGetItemTimeStampError:

End Function
 
' This function returns a data type for this item.  The value returned can
' be either the local data type or the data type as it is contained
' in the OPC Servers copy of the Automation Interface's OPCItem object.
' The mode parameter selects which value will be read.
'
 Function GetItemDataType(ByVal Mode As Integer)
    'Set error handling for OPC Function
    On Error GoTo ShowOPCGetItemDataTypeError

    If Mode = OPCItemDirect Then
        GetItemDataType = OPCItemObj.RequestedDataType
    Else
        GetItemDataType = ItemDataType
    End If
    
GoTo SkipOPCGetItemDataTypeError

ShowOPCGetItemDataTypeError:
    Call DisplayOPC_COM_ErrorValue("GetItemDataType", Err.Number)
SkipOPCGetItemDataTypeError:
    
End Function

Function GetItemCanonicalType()
    'Set error handling for OPC Function
    On Error GoTo ShowOPCGetItemCanonicalTypeError
    
    GetItemCanonicalType = OPCItemObj.CanonicalDataType
    
GoTo SkipOPCGetItemCanonicalTypeError

ShowOPCGetItemCanonicalTypeError:
    Call DisplayOPC_COM_ErrorValue("GetItemCanonicalType", Err.Number)
SkipOPCGetItemCanonicalTypeError:

End Function

Function GetItemServerHandle()
    'Set error handling for OPC Function
    On Error GoTo ShowOPCGetItemServerHandleError
    
    GetItemServerHandle = OPCItemObj.ServerHandle
    
GoTo SkipOPCGetItemServerHandleError

ShowOPCGetItemServerHandleError:
    Call DisplayOPC_COM_ErrorValue("GetItemServerHandle", Err.Number)
SkipOPCGetItemServerHandleError:

End Function

Function GetItemActiveState()
    'Set error handling for OPC Function
    On Error GoTo ShowOPCGetItemActiveStateError
    
    GetItemActiveState = OPCItemObj.IsActive
    
    GoTo SkipOPCGetItemActiveStateError

ShowOPCGetItemActiveStateError:
    Call DisplayOPC_COM_ErrorValue("GetItemActiveState", Err.Number)
SkipOPCGetItemActiveStateError:

End Function

' This function allows you to turn a specific OPC item on and off
' using the .IsActive flag of the item.
'
Function SetItemActiveState(ByVal ActiveState As Boolean)
    'Set error handling for OPC Function
    On Error GoTo ShowOPCSetItemActiveStateError
    
    OPCItemObj.IsActive = ActiveState
    
GoTo SkipOPCSetItemActiveStateError

ShowOPCSetItemActiveStateError:
    Call DisplayOPC_COM_ErrorValue("SetItemActiveState", Err.Number)
SkipOPCSetItemActiveStateError:

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 + -