How to access the variable name from OPC server?

C

Thread Starter

Calvin

Option Explicit

Dim WithEvents OpcFactoryServer As OPCServer 'OPC Automation2 Interface
'
Dim ListOfsGroups As OPCGroups 'For all OFS groups
'
Dim WithEvents OneOfsGroup As OPCGroup 'For an OFS Group
'
Dim AnOfsItemCollection As OPCItems 'Collection of Items
'
Dim OneOfsItem As OPCItem 'For an OFS Item
Const NBR_ITEMS = 4 'Number of ITEMS managed by the
group
'
Dim G_hndClientItemCounter As Long 'Items counter for all groups
'
Dim G_tabItmsOfsHndCli(NBR_ITEMS) As Long 'Handles of ITEMS
'
Dim G_tabItmsOfsHndSrv(NBR_ITEMS) As Long 'Handles returned by the server
'
Dim G_pValues() As Variant 'Values returned by the request
Dim G_pQualities() As Variant 'Quality returned by the
request
Dim G_pErrors() As Long 'Errors returned by the request
'
Dim G_TransID As Long 'ID for Async Transaction
Dim G_CancelID As Long 'ID for Async Cancel

Dim isFormActivated As Boolean 'For connecting one time to the
server
'
Const S_OK = 0 'Status OK returned by OLE
Automation interfaces
'
Const OPC_DS_DEVICE = 2 'Read physically the DEVICE
(and not the cache).
'



Public Sub Start_Click()
'======================================================================
Dim ProgID As String
Dim ItemsDef(NBR_ITEMS) As String
Dim DeviceAddress As String
Dim IndItem As Long
Dim myName As String

'======================================================================

If Not isFormActivated Then
ProgID = "Schneider-Aut.OFS" '"Schneider-Aut.OFS"
On Error Resume Next
Set OpcFactoryServer = New OPCServer 'OPC Automation 2.01 Connect
'----------------------------------------------------------------------
'Local connection
OpcFactoryServer.Connect ProgID

If (Err.Number <> S_OK) Or (OpcFactoryServer Is Nothing) Then
MsgBox "1000: error during creating " + ProgID$, (Err.Number)
Else
MsgBox "You are connected!"

End If
'----------------------------------------------------------------------
'Write info about clientName
OpcFactoryServer.ClientName = "Microsoft Access"
Set ListOfsGroups = OpcFactoryServer.OPCGroups
'----------------------------------------------------------------------
ListOfsGroups.DefaultGroupIsActive = False
ListOfsGroups.DefaultGroupUpdateRate = 500 'ms
ListOfsGroups.DefaultGroupDeadband = 1
ListOfsGroups.DefaultGroupLocaleID = &H409
'----------------------------------------------------------------------
Set OneOfsGroup = ListOfsGroups.Add("Microsoft Access")

If (Err.Number <> S_OK) Or (ListOfsGroups Is Nothing) Then
MsgBox "1010: can't load the main OPC group", (Err.Number)
End If
'----------------------------------------------------------------------
Set AnOfsItemCollection = OneOfsGroup.OPCItems
'----------------------------------------------------------------------
DeviceAddress = "MBT:152.160.178.60"

'Preparing the Handles for "AddItems"
For IndItem = 1 To NBR_ITEMS 'Loop for %MW1,%MW2,%MW3,%MW4"

ItemsDef(IndItem) = DeviceAddress & "!40000" & Format(IndItem)

Label1.Caption = ItemsDef(IndItem)

G_hndClientItemCounter = G_hndClientItemCounter + 1
G_tabItmsOfsHndCli(IndItem) = G_hndClientItemCounter
'Label5.Caption = G_hndClientItemCounter
'Text5.Value = G_tabItmsOfsHndCli(IndItem)
'GUIitemDisplay valuesItem(IndItem)
Next
'----------------------------------------------------------------------
'By default all item created in this collection are active
AnOfsItemCollection.DefaultIsActive = True

'----------------------------------------------------------------------
'Create all the OPC items for this group
For IndItem = 1 To NBR_ITEMS
AnOfsItemCollection.AddItem ItemsDef(IndItem),
G_tabItmsOfsHndCli(IndItem)
G_tabItmsOfsHndSrv(IndItem) =
AnOfsItemCollection.Item(IndItem).ServerHandle
Text1.Value = G_tabItmsOfsHndSrv(IndItem)
If (OneOfsGroup.OPCItems ItemsDef(IndItem) =
"MBT:152.160.178.60!GUI_CurrentTime") Then
MsgBox "Good!"
Else
MsgBox "Bad!"
End If

If Err.Number <> S_OK Then
MsgBox "1040: Can't create the items " + "of the group" + vbCrLf +
vbCrLf + _
"Possible Causes : " + vbCrLf, (Err.Number)
End If
Next
'----------------------------------------------------------------------
'Make the group ready for data acquisition and notification mechanism
OneOfsGroup.IsActive = True
OneOfsGroup.IsSubscribed = True
isFormActivated = True
'======================================================================
Else
'Toggle the notification mechanism
OneOfsGroup.IsSubscribed = Not OneOfsGroup.IsSubscribed
End If

End Sub
 
Top