Option Strict Off
Option Explicit On
Friend Class View
Inherits System.Windows.Forms.Form
Dim WithEvents ConnectedOPCServer As OPCAutomation.OPCServer
Dim ConnectedServerGroups As OPCAutomation.OPCGroup
Dim WithEvents ConnectedGroup As OPCAutomation.OPCGroup
' OPC Item related data
' These arrays are dimensioned for one item.
' If you are doing more then one you would a increase the size.
Dim OPCItemCollection As OPCAutomation.OPCItems
Dim ItemCount As Integer
'UPGRADE_WARNING: Lower bound of array OPCItemIDs was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
Dim OPCItemIDs(1) As String
Dim ItemServerHandles() As Integer
Dim ItemServerErrors() As Integer
'UPGRADE_WARNING: Lower bound of array ClientHandles was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
Dim ClientHandles(1) As Integer
Private Sub ExitExample_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles ExitExample.Click
'When you unlodad or close the form it is time to remove the Items, Group, and Server Connection
Call Remove_Items()
Call Remove_Group()
Call Disconnect_Server()
End
End Sub
' General startup initialization
Private Sub View_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
'Create a new OPC Server object
ConnectedOPCServer = New OPCAutomation.OPCServer
'Attempt to connect with the server
ConnectedOPCServer.Connect("Kepware.KEPServerEX.V5")
' Add the group and set its update rate
ConnectedServerGroups = ConnectedOPCServer.OPCGroups.Add("Group1")
' Set the update rate for the group
ConnectedGroup.UpdateRate = 500
' Subscribe the group so that you will be able to get the data change
' callbacks from the server
ConnectedGroup.IsSubscribed = True
ItemCount = 1
OPCItemIDs(1) = "From Client PLC.Device1.jhEventIndex"
ClientHandles(1) = 1
' Establish a connection to the OPC item interface of the connected group
OPCItemCollection = ConnectedGroup.OPCItems
OPCItemCollection.DefaultIsActive = True
OPCItemCollection.AddItems(ItemCount, OPCItemIDs, ClientHandles, ItemServerHandles, ItemServerErrors)
End Sub
Sub ConnectedGroup_DataChange(ByVal TransactionID As Integer, ByVal NumItems As Integer, ByRef ClientHandles As System.Array, ByRef ItemValues As System.Array, ByRef Qualities As System.Array, ByRef TimeStamps As System.Array) Handles ConnectedGroup.DataChange
' We don't have error handling here since this is an event called from the OPC interface
' You can use the 'Clienthandles' array returned by the server to pull out the
' index number of the control to update and load the value. Since we only have
' one we do not worry about that. If you were doing more then one item you might
' use the following code:
'
Dim x As Short
For x = 1 To NumItems
'UPGRADE_WARNING: Couldn't resolve default property of object ItemValues(x). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
MyText(ClientHandles(x)).Text = ItemValues(x)
Next x
End Sub
Private Sub View_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
'When you unlodad or close the form it is time to remove the Items, Group, and Server Connection
Call Remove_Items()
Call Remove_Group()
Call Disconnect_Server()
End
End Sub
Sub Remove_Items()
'UPGRADE_WARNING: Lower bound of array RemoveItemServerHandles was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
Dim RemoveItemServerHandles(1) As Integer
Dim RemoveItemServerErrors() As Integer
Dim RemoveCount As Short
Dim r As Short
'When you unlodad or close the form it is time to remove the Items, Group, and Server Connection
RemoveCount = OPCItemCollection.Count
'Build the Item Array for items to be removed.
For r = 1 To RemoveCount
RemoveItemServerHandles(r) = ItemServerHandles(r)
Next r
'Remove the items
OPCItemCollection.Remove(RemoveCount, RemoveItemServerHandles, RemoveItemServerErrors)
'Release the item interface and all the resources to be freed
'UPGRADE_NOTE: Object OPCItemCollection may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
OPCItemCollection = Nothing
End Sub
Sub Remove_Group()
'Remove the group
'ConnectedServerGroups.Remove(("Test1"))
ConnectedOPCServer.OPCGroups.Remove("Test1")
' Release the group interface and allow the server to cleanup the resources used
'UPGRADE_NOTE: Object ConnectedGroup may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
ConnectedGroup = Nothing
'UPGRADE_NOTE: Object ConnectedServerGroups may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
'ConnectedServerGroups = Nothing
End Sub
Sub Disconnect_Server()
'Remove/Disconnect the server connecton
ConnectedOPCServer.Disconnect()
' Release the old instance of the OPC Server object and allow the resources
' to be freed
'UPGRADE_NOTE: Object ConnectedOPCServer may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
ConnectedOPCServer = Nothing
End Sub
End Class