DeltaV OPC Data Access Server functional overview > Barcode tutorial

Connect to the DeltaV OPC Data Access Server

The first thing the client application must do is open a connection to the DeltaV OPC Data Access Server. This is accomplished with the Connect command found in the OPCServer object . The name of the DeltaV OPC Data Access Server is passed as a parameter. OLE uses this name to lookup the information in the Windows registry. If you do not know the name of the OPCServer, you may optionally use the method GetOPCServers to retrieve all OPCServers registered on the system.

  1. Add a function to the OPCServer class called OpenServer. It should be a public function and should return an integer.
  2. In the function OpenServer, add the following code:
    Public Function OpenServer() As Integer
    OpenServer = False
        On Error GoTo ErrorHandler
        Set AnOpcServer = New OPCServer
        ' *** Connect directly to DeltaV OPC Server.
        AnOpcServer.Connect ("OPC.DELTAV.1")
    ' *** Uncomment the following lines to select
    ' *** the firstOPC Server registered on the
    ' *** system.
    'Dim AllOPCServers As Variant
    'AllOPCServers = anOpcServer.GetOPCServers
    'AnOpcServer.Connect (AllOPCServers(1))
        
        'Link our group collection to our server
         Set MyGroups = AnOpcServer.OPCGroups
         MyGroups.DefaultGroupIsActive = True
        
        'Get interface to OPCBrowseServer
    Set AnOpcServerBrowser = AnOpcServer.CreateBrowser
        
        OpenServer = True
        Exit Function
        
    ErrorHandler:
        MsgBox "OpenServer: Error!" & vbCrLf _
            & "Err.Number = " & Err.Number & vbCrLf _
            & "Err.Description = " & Err.Description & vbCrLf _
            & "Err.Source = " & Err.Source & vbCrLf
    End Function

    By using the Connect call, the variable Server now points to the default interface (IOPCServer) of the OPCServer object. This variable can be used to access all the properties and methods associated with that interface. To get the second server interface, OPCBrowser, the Set command is called on a variable of the correct type and given the OPCBrowser object created by calling the Server's CreateBrowser function.

  3. Add a function to the COPCServer class called CloseServer. It should be a public function and should return an integer.
  4. In the function CloseServer, add the following code:
    Public Function CloseServer() As Integer
    CloseServer = False 
    On Error GoTo ErrorHandler
        AnOpcServer.Disconnect
    Set AnOpcServer = Nothing
    Set AnOpcServerBrowser = Nothing
    CloseServer = True
    Exit Function
    ErrorHandler:
        MsgBox "CloseServer: Error!" & vbCrLf _
            & "Err.Number = " & Err.Number & vbCrLf _
            & "Err.Description = " & Err.Description & vbCrLf _
            & "Err.Source = " & Err.Source & vbCrLf
    End Function
    Note

    To close a connection to the OPCServer cleanly, you must call Disconnect and set your objects to "Nothing".

  5. Save the project and run it with full compile.

Although the new class has not been used, running with full compile will highlight any syntax errors so far.