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

📄 opcserverclass.cls

📁 VB开发OPC Client的教程和源码
💻 CLS
📖 第 1 页 / 共 3 页
字号:
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "OPCServerClass"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' The OPCServerClass object is the basic building block object
' for your OPC application.  All interaction with an OPC server
' begins first by connecting to the OPC server.  The OPCServerClass
' object wraps the Automation interface OPCServer 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 OPCServerClass object handles functions found in the first two
' branches of this diagram, the OPCServer object and OPCGroups 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.
'
' The OPCServerClass object handles encapsulating the ServerShutDown
' event.  This allows each instance of an the OPCServerClass object
' you create to get its own ServerShutDown event.

Option Explicit
Option Base 1
' The OPCServer objects must be declared here due to the use of WithEvents
' The OPCServer object is the basic object of the the Automation
' Interface.
Dim WithEvents OPCServerObj As OPCServer
Attribute OPCServerObj.VB_VarHelpID = -1

' The server name is the Prog ID of the connected OPC Server. This
' string is passed to the ConnectOPCServer function of this module
' from the VB application.
Dim OPCServerName As String

' This is a string key used to reference this OPCServerClass object.  The
' OPCServerKey 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.
Dim OPCServerKey As String

' The ServerGroups object contains the OPCGroups collection of the
' Automation Interface.  This is used to add new groups to the OPC
' server.  This is shown in the function AddOPCGroup.  This collection
' is kept by the actual Automation Interface wrapper.  The OPCServerClass
' object also keeps its own collection of OPCGroupClass objects.
Dim ServerGroups As OPCGroups

' The OPCServerGroups collection keeps list of OPC groups just like the
' ServerGroups object above.  This collection however is a list of
' OPCGroupClass objects.  The OPCGroupClass object wrap the methods and
' properties of the Automation Interface's OPCGroup and OPCItems collection.
Dim OPCServerGroups As New Collection

' The OPCServerIndex is the numeric version of the OPCServerClass key.
Dim OPCServerIndex As Integer

' 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 this event to be informed when the OPC connection is going to be
' lost.
Public Event ServerShuttingDown(ByVal ServerKey As String)


' The GetOPCServerList function is used to get a list of OPC servers
' available on either the local machine or a remote machine by
' supplying an optional node name.  The GetOPCServerList function
' can be used without an existing OPC connection as you would expect
' otherwise we would have chicken and egg problem.
'
Sub GetOPCServerList(ByRef ServerList As Variant, Optional ByVal NodeName As Variant)
    Dim i As Integer
    'Set error handling for OPC Function
    On Error GoTo ShowOPCGetServersError
    
    ServerList = OPCServerObj.GetOPCServers(NodeName)
     
    GoTo SkipOPCGetServersError
    
ShowOPCGetServersError:
    Call DisplayOPC_COM_ErrorValue("Get OPC Server List", Err.Number)
SkipOPCGetServersError:
End Sub


' This sub handles connecting with the selected OPC Server
' The OPCServer Object provides a method called 'Connect' that
' allows you to 'connect' with an OPC server.  The 'Connect'
' method can take two arguments, a server name and a Node name.
' In this example we don't use the Node name since it is
' optional.  When the 'Connect' method is called you should see
' the OPC Server application start if it is not aleady running.
' This function requires that the ServerKey be passed in as string
' forcing the VB application to manage the server keys.  Once a
' OPCServerClass object has been created and connected, the OPCGroupClass
' and OPCItemClass object create their own keys.
'
Function ConnectOPCServer(ServerName As String, ServerKey As String, ServerIndex As Integer, Optional ByVal NodeName As Variant)
    'Set error handling for OPC Function
    On Error GoTo ShowOPCConnectError
    '
    'Create a new OPC Server object
    'Load the selected server name to start the interface
    Dim StoreName As String
    Dim StoreKey As String
    
    StoreName = ServerName
    OPCServerName = StoreName
    OPCServerIndex = ServerIndex
    
    ' The ServerKey is stored as part of the OPCServerClass object to
    ' make access the collections and treeview easier.
    StoreKey = ServerKey
    OPCServerKey = StoreKey
    
    'Attempt to connect with the server (Local only in this example)
    OPCServerObj.Connect OPCServerName, NodeName

    ' Establish the base group collection object to add new groups
    ' this object will be used below in the group interface functions
    ' At thismpoint there are no groups we simply have the interface
    ' that will allow groups to be added.
    Set ServerGroups = OPCServerObj.OPCGroups
    
    ' Establish the initial default conditions for new groups added to this
    ' server.
    SetDefaultGroupIsActive (True)
    SetDefaultGroupUpdateRate (100)
    SetDefaultGroupDeadBand (0)
    
    
    ConnectOPCServer = True
    GoTo SkipOPCConnectError
    
ShowOPCConnectError:
    Call DisplayOPC_COM_ErrorValue("Connect", Err.Number)
    ConnectOPCServer = False
SkipOPCConnectError:
End Function


' This sub handles disconnecting from the OPC Server.  The OPCServer
' Object provides the method 'Disconnect'.  Calling this on an
' active OPCServer object will release the OPC Server interface with
' your application.  When this occurs you should see the OPC server
' application shut down if it started automatically on the OPC
' connect. This function should not be called until the group(s) and items
' have been removed from this OPCServerClass object.
'
Function DisconnectOPCServer()
    'Set error handling for OPC Function
    On Error GoTo ShowOPCDisconnectError

    'Disconnect from the server, This should only occur after the
    'items and group have been removed.  The server will release
    ' the groups and items for this connection even if you don't
    ' but it is good practice to release the groups and items
    ' progmatically since you don't want to count on the OPC
    ' server to clean up after your application.
    OPCServerObj.Disconnect
    DisconnectOPCServer = True
    
    GoTo SkipDisconnectError
ShowOPCDisconnectError:
    Call DisplayOPC_COM_ErrorValue("Disconnect", Err.Number)
    DisconnectOPCServer = False
SkipDisconnectError:
End Function

' This function handles returning the OPCServerKey string. The
' OPCServerKey is local to the OPCSererClass object and does not
' interact with the underlying Automation Interface.
'
Function GetOPCServerKey()
    GetOPCServerKey = OPCServerKey
End Function

' This functions returns a reference to the OPCServerGroups
' collection maintained by the OPCServerClass object. The
' OPCServerGroups is local to the OPCSererClass object and is not
' retrieved from the underlying Automation Interface.
'
Function GetOPCServerGroupCollection()
    Set GetOPCServerGroupCollection = OPCServerGroups
End Function


' This function returns Serverindex as a numeric value.  The
' OPCServerIndex is local to the OPCSererClass object and does not
' interact with the underlying Automation Interface.
'
Function GetOPCServerIndex()
    GetOPCServerIndex = OPCServerIndex
End Function


' This function returns the name of the OPC Server, this is the ProgId
' of the OPC server.
'
Function GetServerName(ByRef ServerName As String)
    ' Attempt to get the server ServerName.
    ServerName = OPCServerName
    GetServerName = True
End Function


' The next set of functions provide wrappers to the OPCServer object
' methods that allow you to get the various properties of the OPCServer
' object.
'


' Get the server StartTime property
' The server start time returns the time the server has started on
' the host machine.  The server start time is unique to the
' server. All connected clients will receive the same time.
'
Function GetStartTime(ByRef StartTime As Date)
    'Set error handling for OPC Function
    On Error GoTo ShowOPCStartTimeError
    
    ' Attempt to get the server start time.
    StartTime = OPCServerObj.StartTime
    GetStartTime = True
    GoTo SkipOPCStartTimeError
    
ShowOPCStartTimeError:
    Call DisplayOPC_COM_ErrorValue("StartTime", Err.Number)
    GetStartTime = False
    
SkipOPCStartTimeError:
End Function


' Get the server CurrentTime property
' The value is the current time at the server.  It is acquired
' by the OPC automation interface using the GetStatus interface.
'
Function GetCurrentTime(ByRef CurrentTime As Date)
    'Set error handling for OPC Function
    On Error GoTo ShowOPCCurrentTimeError
    
    ' Attempt to get the server current time.
    CurrentTime = OPCServerObj.CurrentTime
    GetCurrentTime = True
    GoTo SkipOPCCurrentTimeError
    
ShowOPCCurrentTimeError:
    Call DisplayOPC_COM_ErrorValue("CurrentTime", Err.Number)
    GetCurrentTime = False
    
SkipOPCCurrentTimeError:
End Function


' Get the LastUpdateTime property
' The LastUpdateTime return the time the last update occured
' for the server this value is for all clients connected to
' the server therfore the time returned may not be the last
' time your client received an update.
'
Function GetLastUpdateTime(ByRef LastUpdateTime As Date)
    'Set error handling for OPC Function
    On Error GoTo ShowOPCLastUpdateTimeError
    
    ' Attempt to get the server Last Update time.
    LastUpdateTime = OPCServerObj.LastUpdateTime
    GetLastUpdateTime = True
    GoTo SkipOPCLastUpdateTimeError

⌨️ 快捷键说明

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