📄 opcserverclass.cls
字号:
Rate = ServerGroups.DefaultGroupUpdateRate
GetDefaultGroupUpdateRate = True
GoTo SkipOPCGetDefaultUpdateRateError
ShowOPCGetDefaultUpdateRateError:
Call DisplayOPC_COM_ErrorValue("GetDefaultGroupUpdateRate", Err.Number)
GetDefaultGroupUpdateRate = False
SkipOPCGetDefaultUpdateRateError:
End Function
' The name say it all, gets the default DeadBand from the
' ServerGroups collection object.
'
Function GetDefaultGroupDeadBand(ByRef DeadBand As Single)
'Set error handling for OPC Function
On Error GoTo ShowOPCGetDefaultGroupDeadBandError
DeadBand = ServerGroups.DefaultGroupDeadband
GetDefaultGroupDeadBand = True
GoTo SkipOPCGetDefaultGroupDeadBandError
ShowOPCGetDefaultGroupDeadBandError:
Call DisplayOPC_COM_ErrorValue("GetDefaultGroupDeadBand", Err.Number)
GetDefaultGroupDeadBand = False
SkipOPCGetDefaultGroupDeadBandError:
End Function
' This sub handles adding the group to the OPC server and
' establishing the group interface. When adding a group you
' can preset some of the group parameters using the properties
' '.DefaultGroupIsActive' and '.DefaultGroupDeadband'. Set these
' before adding the group. Once the group has been successfully
' added you can change these same settings along with the group
' update rate on the fly using the properties on the resulting
' OPCGroupClass object.
' The GroupName parameter is the name your would like to use for
' the new group to be added. This paramter can be left blank.
' When you leave the group name blank you are telling the OPC server
' that is should generate a group name for us. See the SetDefault
' functions above for details on the UpdateRate, DeadBand, and active
' state of the group.
'
' The GroupKey is string that will be returned to the VB application.
' This string can be used as a key for this group in a collection.
'
Function AddOPCGroup(GroupName As String, UpdateRate As Long, DeadBand As Single, ActiveState As Boolean, ByRef GroupKey As String)
'Set error handling for OPC Function
On Error GoTo ShowOPCGroupAddError
' Create an new instance of the OPCGroupClass
Dim ConnectedGroup As New OPCGroupClass
Dim NewGroup As OPCGroup
Dim GroupNum As Integer
' Establish the initial default conditions for new groups added to this
' server.
SetDefaultGroupIsActive (ActiveState)
SetDefaultGroupUpdateRate (UpdateRate)
SetDefaultGroupDeadBand (DeadBand)
' Normally you can name an OPC group yourself but
' you also have the option of allowing the OPC Server
' to provide you with a unique group name automatically.
' To do this simply leave the GroupName empty.
' This blank group name is then passed to the OPC
' server which will recognize this as a automatic
' group name. The server will return the name it
' generates as part of the Automation Interface's
' OPCGroup object.
Set NewGroup = ServerGroups.Add(GroupName)
' If the GroupName was passed in as blank then get the group
' name that the OPC Server has automatically generated.
If GroupName = "" Then
GroupName = NewGroup.Name
End If
' The FindNextGroupNumber handles getting a unique group number
' if you continue to add groups to your OPCServerClass object
' this function simply returns the next group number. If you
' delete a group that was previously added this function will
' attempt to find the deleted group numbers and use them first.
' This prevents the group index numbers from just continuing to
' increment.
GroupNum = FindNextGroupNumber
' Once the next group number is found a unique group key is
' developed. This key can be used in your application to manage
' access to this group. One example might be to use the GroupKey
' returned here as a key within a TreeView of ListView control.
' As you can see here the group key is comprise of the group
' index specifically for this group and on the server index.
' By combining these two index numbers in one key we can be
' resonably sure that the group key will be unique across
' multiple OPC server groups.
GroupKey = "Group" + Str(GroupNum) + Str(OPCServerIndex)
' On the outside chance the OPC server didn't generated a group name
' we can create one from the Groupkey.
If GroupName = "" Then
GroupName = GroupKey
End If
' This sets the basic properties of the OPCGroupClass object
ConnectedGroup.SetOPCGroup NewGroup, GroupName, GroupKey, GroupNum
' Although the OPCGroups (ServerGroups) object is already a
' collection we need to have our own OPCGGroupClass object wrapper
' to contain the properties and events for each group we intend
' to add to the server. This means that we must keep the
' ServerGroups collection and the OPCServerGroups collection
' in sync.
With OPCServerGroups
.Add ConnectedGroup, GroupKey
End With
AddOPCGroup = True
GoTo SkipAddGroupError
ShowOPCGroupAddError:
Call DisplayOPC_COM_ErrorValue("Add Group", Err.Number)
AddOPCGroup = False
SkipAddGroupError:
End Function
' This function attempts to keep the Group index number
' from continuosly growing by finding a group number that
' may have been deleted and returning those first before
' going to the next group index number. The end result of
' this function it that the group number will never grow
' beyond actual number of groups.
'
Private Function FindNextGroupNumber()
' In this function an error state means that a vacant
' group number has been found.
On Error GoTo FoundNextGroupNumber
Dim i As Integer
Dim GroupNum As Integer
GroupNum = 1
With OPCServerGroups
' This loop tries to access each item in the OPCServerGroups
' collection by it's Key. If an item can not be pulled from the
' collection that means the group had been deleted and it's
' group number is available.
For i = 1 To .Count
GroupNum = i
.Item ("Group" + Str(i) + Str(OPCServerIndex))
Next i
' If we get here all of the group numbers in the collection
' exist and a next available group number should be returned.
If .Count <> 0 Then
FindNextGroupNumber = i
Else
FindNextGroupNumber = 1 ' No count return the first 1
End If
End With
GoTo NewGroup
FoundNextGroupNumber:
' If we get here a group number could not be found in the
' OPCServerGroups collection which means that it has been deleted
' and is available for a new group addition.
FindNextGroupNumber = GroupNum
NewGroup:
End Function
' This sub handles removing a group from the OPC server. The group
' is removed from the ServerGroups object which is the collection
' managed by the Automation Interface. Then the group is removed from
' the OPCServerGroups collection which is managed by the OPCServerClass
'
Function RemoveOPCGroup(GroupKey As String)
'Set error handling for OPC Function
On Error GoTo ShowOPCGroupRemoveError
Dim OPCGroupCls As OPCGroupClass
Set OPCGroupCls = OPCServerGroups.Item(GroupKey)
Dim GroupName As String
GroupName = OPCGroupCls.GetGroupName
' First the group is removed from the actual group collection managed by
' the automation interface if it is successful then remove the tiem from the
' OPCServerGroups collection
ServerGroups.Remove GroupName
' Now we remove it from the OPCServerGroups collection
' to allow any final processing on the group to be done
OPCServerGroups.Remove GroupKey
RemoveOPCGroup = True
GoTo SkipRemoveGroupError
ShowOPCGroupRemoveError:
Call DisplayOPC_COM_ErrorValue("Remove Group", Err.Number)
RemoveOPCGroup = False
SkipRemoveGroupError:
End Function
' The OPCServerClass object has the ability to generate an event when
' an OPC server is shutting down. When an OPC server supports the
' OPC 2.0 data access specification, it has the option of issuing a
' ServerShutDown event to any of it's attached clients. In the case
' of a VB application the OPCServer oject will signal the
' ServerShutDown event. This event is handled here in the OPCServerClass
' object. If this event fires, the ServerShuttingDown event of the
' OPCServerClass object will be fired. A VB application has the option of
' hooking the ServerShuttingDown event to be informed when the OPC
' connection is going to be lost.
'
Private Sub OPCServerObj_ServerShutDown(ByVal Reason As String)
' If we receive a server shut down message we need to release everthing and remove the server
' connection.
RaiseEvent ServerShuttingDown(OPCServerKey)
End Sub
' Create the new OPCServer object when the OPCServerClass object is
' instantiated.
'
Private Sub Class_Initialize()
Set OPCServerObj = New OPCServer
End Sub
' When the OPCServerClass object is deleted this function will cleanup
' the OPC connection by releasing the groups and items. While this function
' will clean up the groups and items that may have been attached to this
' OPCServerClass object you will still need to call DisconnectOPCServer
' in your VB application on this object to properly release the OPC Server
' from the Automation Interface.
'
Private Sub Class_Terminate()
' Normally you should remove all groups and their items from the server
' before you remove the server object. However the OPCGroups collection
' object supports a RemoveAll method that will remove all items from
' the groups and all groups from the server.
If Not ServerGroups Is Nothing Then
If ServerGroups.Count <> 0 Then
ServerGroups.RemoveAll
End If
End If
' We still need to remove the OPCServerGroups collection that contains
' the OPCGroupClasss objects
If OPCServerGroups.Count <> 0 Then
Dim i As Integer
Dim a As Integer
a = OPCServerGroups.Count
For i = 1 To OPCServerGroups.Count
With OPCServerGroups
.Remove (a)
a = a - 1
End With
Next i
End If
' Release the OPCServerGroups collection
Set OPCServerGroups = Nothing
End Sub
' 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 + -