📄 opcgroupclass.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "OPCGroupClass"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' The OPCGroupClass object houses all of the functionallity used
' to interact with OPC groups. The OPCGroupClass is used primarily
' by the OPCServerClass. When AddOPCGroup is called on an instance of
' the OPCServerClass object, it creates an instance of a OPCGroupClass
' object. In general you normally won't directly create a OPCGroupClass
' object. You gain access to them as you add groups to your OPCServerClass
' object. Each group you add to the server is given a unique group key
' name. Using this key name and the GetOPCServerGroupCollection function
' of the OPCServerClass you can access specific instances of the
' OPCGroupClass. The OPCGroupClass object wraps the Automation
' interface OPCGroup object and OPCItems (Collection). 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 OPCGroupClass object handles functions found in the second two
' branches of this diagram, the OPCGroup object and OPCItems collection.
' I have not implemented all of the methods and properties found in these
' objects. Adding access to any methods or properties not currently in
' this module is easy.
'
' While you won't normally directly create an OPCGroupClass object you
' will utilize the functions found in this object.
Option Explicit
Option Base 1
' The OPCGroupName contains the actual string name that you provided
' when adding the group. If you left the group name blank when adding
' the group this name will contain the group name assigned by the OPC
' server. This is a convient feature of the OPC server interface.
' Your program doesn't need to supply a group name you can allow the
' server to provide one. There is a catch however, some servers may not
' have implemented this properly. In some cases you may be able to
' add the first group without a name but the second time you add a group
' it may fail because the server thinks the second group name is the
' same as the first blank group you added. This problem is rare.
' Our KEPServer and KEPServerEX will allow you to add as many groups
' as you like.
Dim OPCGroupName As String
' This is a string key used to reference this OPCGroupClass object. The
' OPCGroupKey can be used by the VB application anyway the user
' sees fit. In some cases this is used as a key in controls such as
' a treeview of a listview. Using this key and the GetOPCServerGroupCollection
' of the OPCServerClass object you can get direct access to a specific
' instance of the OPCGroupClass. This key is generated by the AddOPCGroup
' function of the OPCServerClass object.
Dim OPCGroupKey As String
' The OPCGroupObj is the actual group object of the Automation Interface
Dim WithEvents OPCGroupObj As OPCGroup
Attribute OPCGroupObj.VB_VarHelpID = -1
' As items are added to this group they will be placed into this collection
Dim OPCGroupItems As New Collection
' This value holds the numeric value of the OPCGroupKey string above.
Dim GroupIndex As Integer
' This routine initializes the OPCGroupClass object.
'
Sub SetOPCGroup(ByVal OPCGroupObject As OPCGroup, ByVal GroupName As String, ByVal GroupKey As String, ByVal GroupIndx As Integer)
' Assign the group object for this instance of the class
Dim StoreName As String
Dim StoreKey As String
Set OPCGroupObj = OPCGroupObject
StoreName = GroupName
OPCGroupName = StoreName
StoreKey = GroupKey
OPCGroupKey = StoreKey
GroupIndex = GroupIndx
' ****************************************************************
' Mark this group to receive asynchronous updates via the DataChange event.
' This setting is IMPORTANT. Without setting '.IsSubcribed' to True your
' VB application will not receive DataChange notifications. This will
' make it appear that you have not properly connected to the server.
OPCGroupObj.IsSubscribed = True
End Sub
' This sub simply returns the group name set when the class instance was created'
'
Function GetGroupName()
GetGroupName = OPCGroupName
End Function
' This sub simply returns the group key set when the class instance was created
'
Function GetGroupKey()
GetGroupKey = OPCGroupKey
End Function
' This sub simply returns the group index set when the class instance was created
'
Function GetGroupIndex()
GetGroupIndex = GroupIndex
End Function
' This sub simply returns the Item Collection that is part of this class.
'
Function GetOPCGroupItemsCollection()
Set GetOPCGroupItemsCollection = OPCGroupItems
End Function
' This function allows the group's active state to be changed on the fly. The
' OPCGroup object provides a number of properties that can be used to control
' a group's operation. The '.IsActive' property allows you to turn all of the
' OPC items in the group On(active) and Off(inactive). To see the effect that
' the group's '.InActive' property has on an OPC Server run this demo and connect
' with KEPServerEX, add a group, add a few items. Once you see changing data
' right click on the group to access the group properties then toggle
' the active state. If you watch the KEPServerEX OPC Server you will see
' it's active tag count change from 'No Active Items' when the group is made
' inactive to the count of item in the group when the group is made active.
' Changing the actvie state of a group can be useful in controlling how your
' application makes use of an OPC Server's communication bandwidth. If you don't
' need any of the data in a given group simply set it inactive, this will allow an
' OPC Server to gather only the data current required by your application.
'
Function SetGroupActiveState(ByVal ActiveState As Integer)
'Set error handling for OPC Function
On Error GoTo ShowOPCGroupActiveError
OPCGroupObj.IsActive = ActiveState
SetGroupActiveState = True
GoTo SkipGroupActiveError
ShowOPCGroupActiveError:
Call DisplayOPC_COM_ErrorValue("Group Active State", Err.Number)
SetGroupActiveState = False
SkipGroupActiveError:
End Function
' This function get the current value of the group's active state
'
Function GetGroupActiveState(ByRef ActiveState As Boolean)
'Set error handling for OPC Function
On Error GoTo ShowOPCGetGroupActiveError
ActiveState = OPCGroupObj.IsActive
GetGroupActiveState = True
GoTo SkipGetGroupActiveError
ShowOPCGetGroupActiveError:
Call DisplayOPC_COM_ErrorValue("Get Group Active State", Err.Number)
GetGroupActiveState = False
SkipGetGroupActiveError:
End Function
' This function allows the group's deadband to be changed on the fly. Like the
' '.IsActive' property, the '.DeadBand' property can be changed at any time.
' The Deadband property allows you to control how much change must occur in
' an OPC item in this group before the value will be reported in the 'DataChange'
' event. The value entered for '.DeadBand' is 0 to 100 as a percentage of full
' scale for each OPC item data type within this group. If your OPC item is a
' Short(VT_I2) then your full scale is -32768 to 32767 or 65535. If you
' enter a Deadband value of 1% then all OPC Items in this goup would need
' to change by a value of 655 before the change would be returned in the
' 'DataChange' event. The '.DeadBand' property is a floating point number
' allowing very small ranges of change to be filtered.
'
Function SetGroupDeadBand(ByVal DeadBand As Single)
'Set error handling for OPC Function
On Error GoTo ShowOPCSetGroupDeadBandError
OPCGroupObj.DeadBand = DeadBand
SetGroupDeadBand = True
GoTo SkipSetGroupDeadBandError
ShowOPCSetGroupDeadBandError:
Call DisplayOPC_COM_ErrorValue("Set Group Dead Band", Err.Number)
SetGroupDeadBand = False
SkipSetGroupDeadBandError:
End Function
' This function get the current value of the group's Dead Band
'
Function GetGroupDeadBand(ByRef DeadBand As Single)
'Set error handling for OPC Function
On Error GoTo ShowOPCGetGroupDeadBandError
DeadBand = OPCGroupObj.DeadBand
GetGroupDeadBand = True
GoTo SkipGetGroupDeadBandError
ShowOPCGetGroupDeadBandError:
Call DisplayOPC_COM_ErrorValue("Get Group Dead Band", Err.Number)
GetGroupDeadBand = False
SkipGetGroupDeadBandError:
End Function
' This function allows the group's update rate to be changed on the fly. The
' '.UpdateRate' property allows you to control how often data from this
' group will be returned to your application in the 'DataChange' event.
' The '.UpdateRate' property can be used to control and improve the overall
' performance of you application. In this example you can see that the update
' rate is set for a fast update speed. In a demo that's OK. In your real
' world application, forcing the OPC Server to gather all of the OPC items in
' a group at their fastest rate may not be ideal. In applications where you
' have data that needs to be acquired at different rates you can create
' multiple groups each with its own update rate. Using multiple groups would
' allow you to gather time critical data in GroupA with an update rate
' of 200 millliseconds, and gather low priority data from GroupB with an
' update rate of 7000 milliseconds. The lowest value for the '.UpdateRate'
' is 0 which tells the OPC Server go as fast as possible. The maximium is
' 2147483647 milliseconds which is about 596 hours.
'
Function SetGroupUpdateRate(ByVal UpdateRate As Long)
'Set error handling for OPC Function
On Error GoTo ShowOPCSetGroupUdateRateError
OPCGroupObj.UpdateRate = UpdateRate
SetGroupUpdateRate = True
GoTo SkipSetGroupUdateRateError
ShowOPCSetGroupUdateRateError:
Call DisplayOPC_COM_ErrorValue("Set Group Update Rate", Err.Number)
SetGroupUpdateRate = False
SkipSetGroupUdateRateError:
End Function
' This function gets the current value of the group's Update Rate
'
Function GetGroupUpdateRate(ByRef UpdateRate As Long)
'Set error handling for OPC Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -