VB.NET Writing
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

Writing to Tags in an NT Service application using OPC Web Client Web Service

Writing Single Tags

         '
         '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 iDataType = 0
         Const iNewValue = 10

         Try
             '
             'define our OPC Web Client
             Dim objOWC As New EasyDAWS
            '
             'now to make the call to read the tag
             objOWC.WriteItemValue(strPCName, strOPCServer, strTag, iNewValue)
             '
             'now to show the value to the screen
             If Err.Number = 0 Then
               MessageBox.Show("Write Successful!")
             Else
               MessageBox.Show("Write Failed: " & Err.Number & ", " & Err.Description)
             End If
             '
             'now to destroy the objects
             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

Writing Multiple Tags

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

             '
             'create an array to store the results of our write instruction
             Dim Results As Array

             '
             'now perform the writes.
             'You will see that I have broken down this one line instruction over several lines for
             'readability sake. There are essentially 4 parameters:
             ' (1) the machine name, which is blank here
             ' (2) the opc server
             ' (3) the tags I want to write to
             ' (4) the values I want to write
             Results = EasyDA.WriteMultipleItemValues _
               ( _
                 "", _
                 "OPCLabs.KitServer.2", _
                     New Object() _
                         { _
                           "Simulation.Register_I4", _
                           "Simulation.Register_R8", _
                           "Simulation.Register_BSTR" _
                         }, _
                     New Object() _
                         { _
                           10, _
                           20, _
                           30 _
                         } _
               )

             '
             'now to construct some form of output message based on the results
             'of the write
             Dim strWriteValue As New System.Text.StringBuilder

             If Not Results Is Nothing Then

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

                   '
                   'if the type of value is NOT an error
                   If VarType(Results(I)) <> VariantType.Error Then

                     '
                     'not an error, so append our message with a "success"
                     strWriteValue.Append("Element " & I & ": success")
                   Else

                     '
                     'an error, so first RAISE the error accordingly
                     EasyDA.RaiseError(Results(I))

                    '
                     'now record a message indicating (with as much info as poss.)
                     'what the error was
                     strWriteValue.Append(Err.Source & ": " & Err.Description _
                         & " (0x" _
                         & Hex(Err.Number) & ")" & " **")
                   End If

                   '
                   'add a carriage-return + linefeed to the message
                   strWriteValue.Append(vbCrLf)
               Next
             End If

             '
             'display the message to screen
             TextBox1.Text = strWriteValue.ToString


         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