VB.NET - Reading Tags
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

 

Web Service Consumption

The following example shows you how to read a tag in VB.NET by consuming the OPC Web Client Web Service.

Reading a Single Tag:

Simply copy and paste the following code into your application:

         '
         '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 EasyDAWS

             'define the object to receive the value, quality and timestamp
             Dim objTagVTQ As IVTQNet

             'now to make the call to read the tag
             'NOTE: that we are using "readItem" and NOT "readItemValue". This is because
             'I DO WANT the quality & timestamp values
             objTagVTQ = objOWC.ReadItem( _
               strPCName, _
               strOPCServer, _
               strTag)

             '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 webex As Web.Services.Protocols.SoapException
             'display exception to screen
             If webex.InnerException Is Nothing Then
               MessageBox.Show(webex.Message)
             Else
               MessageBox.Show(webex.InnerException.Message)
             End If
         Catch ex As Exception
             'display an exception to screen
             If ex.InnerException Is Nothing Then
               MessageBox.Show(ex.Message)
             Else
               MessageBox.Show(ex.InnerException.Message)
             End If
         End Try

Reading Multiple Tags within a single transaction:

Simply copy and paste the following code into your application:

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

         '
         '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 webex As Web.Services.Protocols.SoapException
         'display exception to screen
         If webex.InnerException Is Nothing Then
           MessageBox.Show(webex.Message)
         Else
           MessageBox.Show(webex.InnerException.Message)
         End If
     Catch ex As Exception
         '
         'display an exception to screen
         If ex.InnerException Is Nothing Then
           MessageBox.Show(ex.Message)
         Else
           MessageBox.Show(ex.InnerException.Message)
         End If
     End Try

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