VB.NET
Home Product
Details Free 
Demo Pricing &
Ordering Related
Products Support About Us

Please use our Code Center to obtain source-code and example projects/solutions:

http://codecenter.softwaretoolbox.com

 

Simple Read Example

The following example builds upon the Tutorial - using with VB.NET.

This paper shows you how to read:

  • one-time read of one tag
  • one-time read of multiple tags
  • subscription reads/exception-based

Reading a Single Tag:

Simply copy & paste the code below into the Form load event:

   '
   'the following constants are used here to help make the code more readable
   'from an educational perspective.
   'Feel free to modify the contents of the values below.
   Const strPCName = ""
   Const strOPCServer = " OPCLabs.KitServer.2"
   Const strTag = "Simulation.Random"
   Const strAccessPath = ""
   Const iDataType = 0

   Try
     '
     'define our OPC Web Client
     Dim objOWC As New OPCLabs.EasyOPCDANet.EasyDACOM
     '
     'define the object to receive the value, quality and timestamp
     Dim objTagVTQ As OPCLabs.EasyOPCDANet.IVTQNet
     '
     'now to make the call to read the tag
     objTagVTQ = objOWC.ReadItem( _
       strPCName, _
       strOPCServer, _
       strTag, _
       strAccessPath, _
       iDataType)
'
         'now to show the value to the screen, but lets do the right thing here and
         'check the QUALITY of the TAG first!!!!
         '
         '0 = bad
         '192 = good
         If Not objTagVTQ Is Nothing Then
           If objTagVTQ.Quality >= 192 Then
               MessageBox.Show("Value: " & objTagVTQ.Value.ToString)
           Else
               MessageBox.Show("Value: " & objTagVTQ.Value.ToString _
                 & " is NOT GOOD quality, quality : " _
                 & objTagVTQ.Quality.ToString)
           End If

         End If
     '
     'now to destroy the objects

     objTagVTQ = Nothing
     objOWC = Nothing
     '
   Catch ex As Exception
     '
     'display an exception to screen
     MessageBox.Show(ex.Message)
   End Try

Reading Multiple Tags within a single transaction:

     Try
         '
         'create ourselves an opc web client object
         Dim EasyDA As New OPCLabs.EasyOPCDANet.EasyDACOM

         '
         'now create an array to receive our values into
         Dim Values As Array

         '
         'now request the read of multiple items
         'I have actually broken this single instruction over multiple lines
         'to try and make it more readable.
         'You will notice that there are essentially 3 parameters:
         ' (1) the computer name for the opc server, which is blank in this case
         ' (2) the opc server
         ' (3) the array of items to read
         Values = EasyDA.ReadMultipleItemValues _
           ( _
               "", _
               "OPCLabs.KitServer.2", _
               New Object() {"Simulation.Random", _
                           "Trends.Ramp (1 min) ", _
                           "Trends.Sine (1 min)", _
                           "Simulation.Register_I4"} _
           )

         '
         'now to take a look at what we received
         If Not Values Is Nothing Then

           '
           'iterate thru the array
           For I As Integer = LBound(Values) To UBound(Values)

               '
               'lets check to make sure that the value is not an error code type
               If VarType(Values(I)) <> VariantType.Error Then

                 '
                 'no error here, so lets display the value to a textbox
                 TextBox1.Text += "Element " & I & ": " & Values(I) & vbCrLf
               Else

                 '
                 'lets call the "raiseError" method and raise the error returned in
                 'the value
                 EasyDA .RaiseError(Values(I))

                 '
                 'display an error to the textbox
                 TextBox1.Text += vbCrLf & "** " _
                     & Err.Source _
                     & ": " & Err.Description _
                     & " (0x" & Hex(Err.Number) & ")" _
                     & " **"

               End If
           Next
         End If

     Catch ex As Exception
         '
         'display any exceptions to screen
         MessageBox.Show("Error: " & ex.Message)
     End Try

Subscription Reads (Exception-based reads)

    Subscription reads require that your application run in a Multi-threaded apartment. To enable this in VB.NET you must define a "Sub Main" as your applications entry-point, and apply the "MTAThread" attribute to it.

    IMPORTANT: You MUST read the following FAQ - Threading Apartments.

    Create a new module and then define a Sub MAIN as follows. Within the Sub Main be sure to open your form/application.

    Module Module1

      <MTAThread()> Public Sub main()
      End Sub

    End Module

    Now, the code to setup the subscription and receive the event notifications, which may exist anywhere within your application.

    In this example the "oItem" is the object that will be "bound" to a Tag subscription. This item needs to be kept alive to keep the subscription alive.

    Public oItem As IItemNet 'defined in a global location

    Private oSvr As New OPCLabs.EasyOPCDANet.EasyDACOM

      Public Sub New()
       oItem = oSvr.RequestItem("", "SWToolbox.TOPServer",  _
    "Channel_0_User_Defined.Ramp.Ramp1")

       AddHandler oItem.MeasurementChange, AddressOf itemchanged
    End Sub

    Private Sub itemchanged(ByVal sender As Object, ByVal e As EventArgs)
       Dim theTag As OPCLabs.EasyOPCDANet.IItemNet
       theTag = DirectCast(sender, OPCLabs.EasyOPCDANet.IItemNet)
       Me.ListBox1.Items.Add(theTag.VTQ.Value.ToString)
    End Sub

Copyright Software Toolbox, Inc., 1996-2004, All Rights Reserved Worldwide.
148A East Charles Street, Matthews, North Carolina, USA 28105
Phone: 704-849-2773 or 1-888-665-3678 (US), Fax: 704-849-6388
sales@softwaretoolbox.com | support@softwaretoolbox.com