VB.NET Component Read
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

 

Back to Services Source Examples

Using the OPCData.NET Components within a VB.NET NT Service Application

The following example shows you one possible way in which you could use the OPCData.NET Components within an NT Service application.

This example is not for all developers as NT Service development is not easy. Therefore this and other NT Service based examples should only be used by experienced NT Service programmers.

How this example is constructed

    This example actually uses a Windows Form to display some options allowing the end-user to specify the OPC Server, Tag and PC etc. to read. The values are then read and displayed to the form. A Form was used for simplicity in showing this example. Please note that a Windows Form is NOT required to use the OPC Web Client within an NT Service.

    This example contains the following form:
    The code in this example is applied to the Click event handler for the button in this window. The textfields in this window are identified as "txt" and then the name, i.e. "txtPC", "txtOPC", "txtTag" and "txtAccess".

    IMPORTANT NOTE:
    When using an
    NT Service with Windows Forms you must ensure that the Service properties (within the CONTROL PANEL > Administrative Tools > Services) are set to allow the service to interact with the desktop. If this setting is not applied then you will not see the Form!

Reading a Tag

   Try
     '
     'define our OPC Web Client object
     Dim objOWC As New EasyOPCDALib.EasyDA3
     '
     'define the tag object and get a reading from the
     'OPC Server
     Dim objVTQ As EASYOPCDALib.VTQ = objOWC.ReadItem( _
       txtPC.Text, _
       txtOPC.Text, _
       txtTag.Text, _
       txtAccess.Text, _
       0)
     '
     'check if any errors occurred
     If Err.Number = 0 Then
       labelValue.Text = objVTQ.Value
       labelQuality.Text = objVTQ.Quality
     Else
       labelValue.Text = "Error " & Err.Number
     End If
     '
     'clean-up
     'NOTE: in your application you probably would not destroy
     'the objects as we are here. You would most likely
     'want to keep them alive as long as possible.
     objVTQ = Nothing
     objOWC = Nothing
   Catch ex As Exception
     labelValue.Text = "Error: " & 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.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