DeltaV OPC Data Access Server functional overview > Barcode tutorial

Add items

Just as you add groups to the server's OPCGroup object, you add items to a group object. Items must be valid fields in the DeltaV database. The ItemID is the DeltaV path to the item (area name is not included). An example path note "OPCSAMPLE/STEP_COUNT.CV". The same item may exist in multiple groups. There are a few settings that are changed at the item level. They are:

RequestedDataType - This is the data type of the item that the client application would like to receive. For example, to request the String representation of a Named Set use a requested data type of string (VT_BSTR) instead an integer value (VT_I4).

ClientHandle - This value is any unsigned 32-bit integer. The DeltaV OPC Data Access Server will provide this handle when returning data in an asynchronous connection. This allows the client application to identify the item to which the data refers.

IsActive - The client application can change the active state of an item at any time using the SetActiveState command. If an item is not active the DeltaV OPC Data Access Server will not keep its value up to date. The client application will not be notified of changes. Read requests to cache will return successfully however, the data value will be stale and the quality status will be "bad" (for a detailed description of statuses, refer to Appendix B). Read requests to "device" always return the current value if no errors are encountered.

Note

AccessPaths and Blobs are defined in the OPC specification but are not required for the DeltaV system. These variables are ignored.

The DeltaV OPC Data Access Server returns the following information about the item:

Canonical Data Type - This is the native data type of the item.

Server Handle - This is an unsigned 32-bit integer. Clients give this handle to the DeltaV OPC Data Access Server to identify an item. The DeltaV OPC Data Access Server uses the group server handle with the item server handle to identify the item.

AccessRights - This is the access rights allowed to a given item. The return values are Read only, Write only or Read/Write.

Note

The DeltaV OPC Data Access Server assumes that all items are read/write. If a client application attempts to write to a parameter that is in fact read only in the DeltaV system, the write will fail, and the DeltaV OPC Data Access Server will return an error to the client.

  1. Add two functions to the OPCServer class, AddItem and RemoveItem. They should be public functions and should return an integer.
  2. Add the following parameters to the AddItem function:
    Public Function AddItem(GroupName As String, ItemID As String, DataType 
    As Variant) As Long
  3. In the AddItem function, add the following code:
        Dim anOPCGroupHandle As Long
        Dim anOPCItemCollection As OPCItems
        
        Dim AddItemCount As Long
        Dim anOPCClientHandles() As Long
        Dim anOPCItemIDs() As String
        Dim anOPCItemServerHandles() As Long
        Dim anOPCItemServerErrors() As Long
        Dim anOPCRequestedDataTypes() As Variant
        Dim anOPCAccessPathss() As Variant
        
        Dim Msg As String
        
        AddItem = 0
        On Error GoTo ErrorHandler
        
        'Look up group
        anOPCGroupHandle = LookUpGroup(GroupName)
        If anOPCGroupHandle <= 0 Then
            MsgBox "Error - Group Not Found"
            Exit Function
        End If
        
        'Link item collection to group
        Set anOPCItemCollection = MyGroups(GroupName).OPCItems
        anOPCItemCollection.DefaultAccessPath = ""
        
        AddItemCount = 1
        ReDim anOPCClientHandles(AddItemCount)
        ReDim anOPCItemIDs(AddItemCount)
        ReDim anOPCItemServerHandles(AddItemCount)
        ReDim anOPCItemServerErrors(AddItemCount)
        ReDim anOPCRequestedDataTypes(AddItemCount)
        ReDim anOPCAccessPathss(AddItemCount)
        
        anOPCClientHandles(1) = anOPCItemCollection.Count + 1
        anOPCItemIDs(1) = ItemID
        anOPCRequestedDataTypes(1) = DataType
        anOPCAccessPathss(1) = ""
        
        anOPCItemCollection. AddItems AddItemCount, anOPCItemIDs, _
                 anOPCClientHandles, anOPCItemServerHandles, _
                 anOPCItemServerErrors
                 ', anOPCRequestedDataTypes, anOPCAccessPathss
        'Test for Error
        If anOPCItemServerErrors(1) >= 0 Then
            'Item was added
            anOPCItemCollection.GetOPCItem(anOPCItemServerHandles(1)).RequestedDataType 
            = DataType
        Else
            MsgBox anOPCItemServerErrors(1) & ": Error adding item " & ItemID
            Msg = AnOpcServer.GetErrorString(anOPCItemServerErrors(1))
            MsgBox Msg
        End If
            
        'Return the item's unique server handle
        AddItem = anOPCItemServerHandles(1)
        
        Exit Function
        
    ErrorHandler:
        MsgBox "AddItem: Error!" & vbCrLf _
               & "Err.Number = " & Err.Number & vbCrLf _
               & "Err.Description = " & Err.Description & vbCrLf _
               & "Err.Source = " & Err.Source & vbCrLf
    End Function
  4. Add the following parameters to the RemoveItem function:
    Public Function RemoveItem(GroupName As String, ItemID As String) As Integer
  5. In the RemoveItem function, add the following code:
        Dim anOPCGroupHandle As Long
        Dim anOPCItemCollection As OPCItems
        
        Dim RemoveItemCount As Long
        Dim anOPCItemServerHandles(1) As Long
        Dim anOPCItemServerErrors(1) As Long
        
        Dim Msg As String
        
        RemoveItem = False
        On Error GoTo ErrorHandler
        
        anOPCGroupHandle = LookUpGroup(GroupName)
        If (anOPCGroupHandle < 0) Then
            MsgBox "Error - Group not found"
            Exit Function
        End If
        
        'Link item collection to group
        Set anOPCItemCollection = MyGroups(GroupName).OPCItems
        
        RemoveItemCount = 1
        anOPCItemServerHandles(1) = LookUpItem(anOPCGroupHandle, ItemID)
        If (anOPCItemServerHandles(1) < 0) Then
            MsgBox "Error - Item not found"
            Exit Function
        End If
        
        anOPCItemCollection.Remove RemoveItemCount, _
            anOPCItemServerHandles, anOPCItemServerErrors
        'Test for Error
        If anOPCItemServerErrors(1) >= 0 Then
            'Item was removed
        Else
            MsgBox anOPCItemServerErrors(1) & ": Error removing item " & ItemID
            Msg = AnOpcServer.GetErrorString(anOPCItemServerErrors(1))
            MsgBox Msg
        End If
        
        RemoveItem = True
        
        Exit Function
        
    ErrorHandler:
        MsgBox "RemoveItem: Error!" & vbCrLf _
               & "Err.Number = " & Err.Number & vbCrLf _
               & "Err.Description = " & Err.Description & vbCrLf _
               & "Err.Source = " & Err.Source & vbCrLf
    End Function

    RemoveItem sets the ItemDisp interface to nothing. This releases the interface in the OpcServer.

  6. Add a support function for looking up the group handle from the name. The function should be called "LookUpItem". It is a private function with the following declaration:
    Private Function LookUpItem(GroupHandle As Integer, ItemID As String) 
    As Integer
  7. In the function LookUpItem, add the following code:
    Dim done As Integer
    Dim Count As Integer
    LookUpItem = -1
    On Error GoTo errHandler
    done = False
    Count = 0
    While Not done
        If Count > Groups(GroupHandle).ItemCount Then
        done = True
        ElseIf ItemID = Items(GroupHandle, Count).ID Then
        LookUpItem = Count
            done = True
        Else
            Count = Count + 1
        End If
    Wend
    Exit Function
    ErrHandler:
        MsgBox Err.Description
    Exit Function

    This function takes the Server Handle for the group and the Item ID. If the client handle is not known, the application calls LookUpGroup with the group name. LookUpGroup returns the client handle for the group.

  8. Save the project and run with full compile to highlight any errors.