Write Single Tag IIS
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

 

  • Writing to Tags using the OPC Web Client ActiveX Controls

    Quick links within this page:

    Objective

      The web-page we will create will simply write a new value to a Tag, and then display a success/failure message.

    Writing to a Single Tag

      In order for our web-page to display anything, we need to tell the server to fetch the Tag value from the OPC Server and then to display it in the page.

      ASP

      First, you need to add the following line at the top of the page:

      <%@ Language="vbscript" %>

        <html>

        <head>
        <title>OPC Web Client Example, Single Tag Write to  the Web Server</title>
        </head>

        <body>

          <p>The Tag write was a
        <%
          Set opcWebClient = Server.CreateObject("OPCLabs.EasyDA3.0")


          opcWebClient .WriteItemValue "", "OPCLabs.KitServer.2", _
           & "Simulation.Register_I4"

          If Err.Number = 0 Then
           Response.Write "Success"
          Else
           Response.Write "<font color=red>Failure: " & Err.Number & ", " _
             & Err.Description & "</font>"
          End If

          Set opcWebClient = Nothing
        %>
          </p>

        </body>
        </html>

      Save your page with a .ASP extension onto your web-server. Now launch your web-browser and connect to your web-page from your web-server. You should see the value of a tag in the page.

      ASP.NET

       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 EasyDA3
         '
         '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
           Response.Write("Write Successful!")
         Else
           Response.Write("Write Failed: " & Err.Number & ", " & Err.Description)
         End If
         '
         'now to destroy the objects
         objOWC = Nothing
         '
       Catch ex As Exception
         '
         'display an exception to screen
         Response.Write(ex.Message)
       End Try

    Writing to Multiple Tags

      ASP

    <%@Language="vbscript"%>
    <html>

    <head>
    <title>OPC Web Client Example, Reading Multiple Tags</title>
    </head>

    <body>

      <p>Tag is:
    <%
       'define an opc web client object
       Dim EasyDA
       Set EasyDA = CreateObject("OPCLabs.EasyDA3.0")

       'define the results object, which will become a collection
       Dim Results

       'make the call to write values to multiple tags
       'do note how we are dynamically passing-in the array of tag names
       'and tag values

       Results = EasyDA.WriteMultipleItemValues("", "OPCLabs.KitServer.2", _
       Array("Simulation.Register_I4", " Simulation.Register_R8", "Simulation.Register_BSTR"), _
       Array(23456, 2.34567890, "ABC"))

       'now to iterate thru the results collection
       dim strWriteValue
       Dim I
       For I = LBound(Results) To UBound(Results)
           If VarType(Results(I)) <> 10 Then

               'no error, say so in the message string
               strWriteValue = strWriteValue & "Element " & I & ": success"
           Else

               'error ocurred, handle gracefully/appropriately
               On Error Resume Next
               EasyDA.RaiseError Results(I)
               strWriteValue = strWriteValue & Err.Source & ": " _
                   & Err.Description & " (0x" _
                   & Hex(
    Err.Number) & ")" & " **"
               On Error Goto 0
           End If
           strWriteValue = strWriteValue & "<br>"
       Next

      'display our message
      Response.Write strWriteValue
      %>

       </p>


    </body>
    </html>

      ASP.NET

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       'Put user code to initialize the page here
       '
       Try

       'lets define our tags in a string array ahead of time...
         Dim strTags() As String = New String() _
               { _
                 "Simulation.Register_I4", _
                 "Simulation.Register_R8", _
                 "Simulation.Register_BSTR" _
               }

       'lets define our value we want to write, also ahead of time
         Dim strValues() As String = New String() _
               { _
                 "1", _
                 "2", _
                 "3" _
               }

       'lets now create an opc web client and make the call
       'to write all the values for all the tags.

         Dim objOWC As New OPCLabs.EasyOPCDANet.EasyDACOM
         objOWC .WriteMultipleItemValues _
           ( _
             "127.0.0.1", _
             "OPCLabs.KitServer.2", _
             strTags, _
             strValues _
           )

       'lets now check if any errors occurred
         If Err.Number = 0 Then
           Response.Write("Write=SUCCESS!")
         Else
           Response.Write("Write=FAILED! " &
    Err.Number)
         End If
       Catch ex As Exception
         Response.Write("Error: " & ex.Message)
       End Try
      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