Simple Write
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 Write Example

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

Writing to 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.Register_I4"
   Const iDataType = 0
   Const iNewValue = 10

   Try
     '
     'define our OPC Web Client
     Dim objOWC As New OPCLabs.EasyOPCDANet.EasyDACOM
     '
     '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 ex As Exception
     '
     'display an exception to screen

     MessageBox.Show(ex.Message)
   End Try

Writing to Multiple Tags within a single transaction:

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

         '
         '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() {23456, _
                       2.3456789, _
                       "ABC"} _
           )

         '
         '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 ex As Exception
         'display exceptions to screen
         MessageBox.Show("Error: " & ex.Message)
     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