Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

OPC VB Error

By: a guest on Jun 17th, 2011  |  syntax: None  |  size: 5.99 KB  |  views: 304  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Option Strict Off
  2. Option Explicit On
  3. Friend Class View
  4.         Inherits System.Windows.Forms.Form
  5.        
  6.         Dim WithEvents ConnectedOPCServer As OPCAutomation.OPCServer
  7.     Dim ConnectedServerGroups As OPCAutomation.OPCGroup
  8.     Dim WithEvents ConnectedGroup As OPCAutomation.OPCGroup
  9.  
  10.         ' OPC Item related data
  11.         ' These arrays are dimensioned for one item.
  12.         ' If you are doing more then one you would a increase the size.
  13.         Dim OPCItemCollection As OPCAutomation.OPCItems
  14.         Dim ItemCount As Integer
  15.         '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"'
  16.     Dim OPCItemIDs(1) As String
  17.         Dim ItemServerHandles() As Integer
  18.         Dim ItemServerErrors() As Integer
  19.         '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"'
  20.         Dim ClientHandles(1) As Integer
  21.        
  22.         Private Sub ExitExample_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles ExitExample.Click
  23.                 'When you unlodad or close the form it is time to remove the Items, Group, and Server Connection
  24.                 Call Remove_Items()
  25.                 Call Remove_Group()
  26.                 Call Disconnect_Server()
  27.                 End
  28.         End Sub
  29.        
  30.         ' General startup initialization
  31.         Private Sub View_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
  32.                 'Create a new OPC Server object
  33.         ConnectedOPCServer = New OPCAutomation.OPCServer
  34.         'Attempt to connect with the server
  35.         ConnectedOPCServer.Connect("Kepware.KEPServerEX.V5")
  36.  
  37.         ' Add the group and set its update rate
  38.         ConnectedServerGroups = ConnectedOPCServer.OPCGroups.Add("Group1")        
  39.  
  40.         ' Set the update rate for the group
  41.         ConnectedGroup.UpdateRate = 500
  42.         ' Subscribe the group so that you will be able to get the data change
  43.         ' callbacks from the server
  44.         ConnectedGroup.IsSubscribed = True
  45.  
  46.         ItemCount = 1
  47.         OPCItemIDs(1) = "From Client PLC.Device1.jhEventIndex"
  48.         ClientHandles(1) = 1
  49.  
  50.         ' Establish a connection to the OPC item interface of the connected group
  51.         OPCItemCollection = ConnectedGroup.OPCItems
  52.         OPCItemCollection.DefaultIsActive = True
  53.         OPCItemCollection.AddItems(ItemCount, OPCItemIDs, ClientHandles, ItemServerHandles, ItemServerErrors)
  54.  
  55.  
  56.     End Sub    
  57.        
  58.         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
  59.                 ' We don't have error handling here since this is an event called from the OPC interface
  60.                
  61.                 ' You can use the 'Clienthandles' array returned by the server to pull out the
  62.                 ' index number of the control to update and load the value.  Since we only have
  63.                 ' one we do not worry about that. If you were doing more then one item you might
  64.                 ' use the following code:
  65.                 '
  66.                 Dim x As Short
  67.                 For x = 1 To NumItems
  68.                         '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"'
  69.                         MyText(ClientHandles(x)).Text = ItemValues(x)
  70.                 Next x
  71.         End Sub
  72.        
  73.        
  74.         Private Sub View_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  75.                 'When you unlodad or close the form it is time to remove the Items, Group, and Server Connection
  76.                 Call Remove_Items()
  77.                 Call Remove_Group()
  78.                 Call Disconnect_Server()
  79.                 End
  80.         End Sub
  81.        
  82.         Sub Remove_Items()
  83.                 '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"'
  84.                 Dim RemoveItemServerHandles(1) As Integer
  85.                 Dim RemoveItemServerErrors() As Integer
  86.                 Dim RemoveCount As Short
  87.                 Dim r As Short
  88.                
  89.                 'When you unlodad or close the form it is time to remove the Items, Group, and Server Connection
  90.                 RemoveCount = OPCItemCollection.Count
  91.                
  92.                 'Build the Item Array for items to be removed.
  93.                 For r = 1 To RemoveCount
  94.                         RemoveItemServerHandles(r) = ItemServerHandles(r)
  95.                 Next r
  96.                 'Remove the items
  97.                 OPCItemCollection.Remove(RemoveCount, RemoveItemServerHandles, RemoveItemServerErrors)
  98.                 'Release the item interface and all the resources to be freed
  99.                 '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"'
  100.                 OPCItemCollection = Nothing
  101.         End Sub
  102.        
  103.         Sub Remove_Group()
  104.                 'Remove the group
  105.         'ConnectedServerGroups.Remove(("Test1"))
  106.         ConnectedOPCServer.OPCGroups.Remove("Test1")
  107.                 ' Release the group interface and allow the server to cleanup the resources used
  108.                 '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"'
  109.                 ConnectedGroup = Nothing
  110.                 '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"'
  111.         'ConnectedServerGroups = Nothing
  112.                
  113.         End Sub
  114.        
  115.         Sub Disconnect_Server()
  116.                 'Remove/Disconnect the server connecton
  117.                 ConnectedOPCServer.Disconnect()
  118.                
  119.                 ' Release the old instance of the OPC Server object and allow the resources
  120.                 ' to be freed
  121.                 '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"'
  122.                 ConnectedOPCServer = Nothing
  123.         End Sub
  124. End Class