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

    Have the ability to write a value to an OPC Tag from a web-page.

Writing to a single Tag

    In order for our web-page to do anything, we now need to add some scripting that will:

    • respond to the button being clicked
    • perform the tag write request
    • display some message to indicate a success or failure

    Our click-event handler will instantiate the OPC Web Client, make the call to read the value and then display it in the textbox. Here goes:

    VBSCRIPT

<html>

<head>
<title>OPC Web Client Example, Single Tag Read Within Internet Explorer</title>
</head>

<body>

  <p>Enter a number to write here
  <input type="text" name="T1" size="20">
  <input type="button" value="Button" name="B3">
  </p>


</body>
<script language="vbscript">
  sub b3_onClick
  set opcwebclient = CreateObject("OPCLabs.EasyDA3.0")
  opcwebclient.WriteItemValue "", "OPCLabs.KitServer.2",_
     "Simulation.Register_I4", Cint(t1.value)
  if Err.Number = 0 then
   msgbox "Successfully wrote to tag!"
  else
   msgbox "An error occurred: " & Err.Number
  end if
  end sub

</script>

</html>

    Simply place the above text under the "</body>" HTML tag and then save your HTML page. Now open that web-page directly from Internet Explorer and then click on the button.

    JAVASCRIPT

<html>

<head>
<title>OPC Web Client Example, Single Tag Read Within Internet Explorer</title>
</head>

<body>

  <p>
  <input type="text" name="T1" size="20">
  <input type="button" value="Button" name="B3" onClick="doWrite();">
  </p>

</body>
<script language="javascript">
 function doWrite()
 {
  try
  {
   var opcWebClient = new ActiveXObject(" OPCLabs.EasyDA3.0");
  opcWebClient.WriteItemValue("", "OPCLabs.KitServer.2 ", "Simulation.Register_I4", document.all.T1.value);
  alert("Success");
  }
  catch(e)
  {
   alert(e)
  }
 }
</script>
</html>

    With the above modification set, and with the Javascript code present, you can now save this web-page and load it directly into the web-browser.

Writing to Multiple Tags

    VBSCRIPT

    <html>

    <head>
    <title>OPC Web Client Example, Read Multiple Tags Within Internet Explorer</title>
    </head>

    <body>

      <p>The following tags and values will be written:</p>
      <ul>
       <li><font color="#800000">Simulation.Register_I4 = 23456</font></li>
       <li><font color="#800000">Simulation.Register_R8 = 2.34567890</font></li>
       <li><font color="#800000">Simulation.Register_BSTR
         = &quot;ABC&quot;</font></li>
      </ul>
      <p><input type="button" value="Write" name="B3">
      </p>

      <p>
      <textarea rows="4" name="S1" cols="33"></textarea>
      </p>


    </body>
    <script language="vbscript">
     
      sub b3_onClick
       Dim EasyDA
       Set EasyDA = CreateObject("OPCLabs.EasyDA3.0")

       Dim Results
       Results = EasyDA .WriteMultipleItemValues("", "OPCLabs.KitServer.2", _
       Array("Simulation.Random", "Trends.Ramp (1 min)", "Simulation.Register_I4"), _
       Array(23456, 2, 99))

       dim strWriteValue
       Dim I
       For I = LBound(Results) To UBound(Results)
           If VarType(Results(I)) <> 10 Then
               strWriteValue = strWriteValue & "Element " & I & ": success"
           Else
               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 & vbCrLf
       Next

       document.all.s1.value = strWriteValue
      end sub

     
    </script>
    </html>

    JAVASCRIPT

    Feature not currently supported within Javascript due to the differences in Array handling for this development language. This feature will be made available in the next product release.

    Sign-up to our newsletter to receive product updates & other important information.

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