In this example we make use of the VBA Feature called Events.
The OPC group subscribes to Data Change Event. Based on the refresh rate if the Data is Changed the DataChange function is invoked which can be used for processing.
The automation interface supports the event notification mechanism that is provided with Visual Basic The Automation server triggers events in response to Async Refresh, Async Read and Async Write Method calls. In addition, Automation server triggers events when data changes according to the client specification. The implementation assumes that the Automation Client is equipped to deal with these events.
DataChange
Description | The DataChange event is fired when a value or the quality of a value for an item within the group has changed. Note the event will not fire faster than the update rate of the group. Therefore, item values will be held by the server and buffered until the current time + update rate is greater than the time of the previous update (event fired). |
Syntax | DataChange (TransactionID As Long, NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date) |
Example
Open Excel Tools -> Macro ->Visual Basic Editor
Click Insert Class Module
Paste the following code
Option Explicit
Option Base 1
Const MaxItemCount = 100
Public WithEvents OPCMyserver As OPCServer
Public WithEvents OPCMygroups As OPCGroups
Public WithEvents OPCMygroup As OPCGroup
Dim OPCMyitems As OPCItems
Dim OPCMyitem As OPCItem
Dim ItemCount As Integer
Private Sub Class_Initialize()
MsgBox "Server startup"
Connect "Matrikon.OPC.Simulation.1"
AddGroup "Group1", "Random.Int1"
Subscribe True
End Sub
Private Sub Class_Terminate()
Dim Errors() As Long
On Error Resume Next
OPCMygroup.IsActive = False
OPCMygroups.Remove OPCMygroup.ServerHandle
Set OPCMyitems = Nothing
Set OPCMyitem = Nothing
Set OPCMygroups = Nothing
Set OPCMygroup = Nothing
OPCMyserver.DisConnect
Set OPCMyserver = Nothing
End Sub
Function Connect(ServerName As String)
On Error GoTo ConnectError
Set OPCMyserver = New OPCServer
OPCMyserver.Connect ServerName, ""
Connect = True
Exit Function
ConnectError:
Connect = False
End Function
Function AddGroup(GroupName As String, ItemID As String)
On Error GoTo AddError
Dim i As Integer
Dim ItemServerHandles() As Long
Dim ClientHandles(1) As Long
Dim OPCItemIDs(1) As String
Dim Errors() As Long
Set OPCMygroups = OPCMyserver.OPCGroups
Set OPCMygroup = OPCMygroups.Add(GroupName)
OPCMygroup.UpdateRate = 1000
Set OPCMyitems = OPCMygroup.OPCItems
ItemCount = 1
For i = 1 To ItemCount
ClientHandles(1) = i
OPCItemIDs(1) = ItemID
OPCMyitems.AddItems 1, OPCItemIDs, ClientHandles, _
ItemServerHandles, Errors ''', RequestedDataTypes, AccessPaths
Cells(i, 1) = OPCItemIDs(1)
If Errors(1) <> 0 Then
Cells(i, 2) = "#N/A"
End If
Next i
AddGroup = True
Exit Function
AddError:
AddGroup = False
End Function
Sub Subscribe(bSubscribe As Boolean)
On Error Resume Next
OPCMygroup.IsActive = bSubscribe
OPCMygroup.IsSubscribed = bSubscribe
If bSubscribe = True Then
Dim anItem As OPCItem
For Each anItem In OPCMygroup.OPCItems
anItem.Read OPCCache ', value, qual, time
Cells(anItem.ClientHandle, 2) = anItem.Value
Cells(anItem.ClientHandle, 3) = anItem.TimeStamp
Cells(anItem.ClientHandle, 4) = anItem.Quality
Set anItem = Nothing
Next anItem
End If
End Sub
Sub UnSubscribe()
OPCMygroup.IsSubscribed = False
End Sub
'Subscribed to OPC group Data Change Event
Private Sub OPCMygroup_DataChange(ByVal TransactionID As Long, _
ByVal NumItems As Long, ClientHandles() As Long, _
ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date)
Dim i As Integer
For i = 1 To NumItems
Cells(1, 2) = ItemValues(i)
Cells(1, 3) = TimeStamps(i)
Cells(1, 4) = Qualities(i)
Next i
End Sub
'Subscribed to Server shutdown event
Private Sub OPCMyserver_ServerShutDown(ByVal Reason As String)
MsgBox "Server shutdown"
End Sub
Note the WithEvents declaration makes sure the events for the objects are turned on.
Dim instance As Class1
Public WithEvents OPCMyserver As OPCServer
Public WithEvents OPCMygroups As OPCGroups
Public WithEvents OPCMygroup As OPCGroup
Private Sub Workbook_Open()
Set instance = New Class1
Set OPCMygroup = instance.OPCMygroup
Set OPCMyserver = instance.OPCMyserver
Set OPCMygroups = instance.OPCMygroups
End Sub