C# Component 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

 

Back to Services Source Example

Using the OPCData.NET Components within a C# 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.

Writing to a Tag

string strPCName = "";
string strOPCServer = "OPCLabs.KitServer.2";
string strTag = "Simulation.Register_I4";
int iValue = 10;

try
{
//'create the opc web client object
   OPCLabs.EasyOPCDANet.EasyDAWS objOWC = new OPCLabs.EasyOPCDANet.EasyDAWS();

//make the call to write the value
   objOWC.WriteItemValue
   (
     strPCName,
     strOPCServer,
     strTag,
     iValue,
     null,
     null,
     null
   );
  
//display a message
   MessageBox.Show("Wrote value!");
  
//clean up
   objOWC = null;
}
catch(System.Web.Services.Protocols.SoapException webex)
{
   MessageBox.Show("Error: " + webex.Message.ToString());
}
catch(Exception ex)
{
   MessageBox.Show("Error: " + ex.Message.ToString());
}

Writing to Multiple Tags within a single transaction

    try
    {
    //define our initial variables and set their values
    object oPC = "127.0.0.1";
    object oOPC = "
    OPCLabs.KitServer.2";
    object oTags = new object[]{"Simulation.Register_I4", "Simulation.Register_R4"};
    object oAccess = System.Runtime.InteropServices.VarEnum.VT_ERROR;
    object oDTypes = System.Runtime.InteropServices.VarEnum.VT_ERROR;
    object oModes = System.Runtime.InteropServices.VarEnum.VT_ERROR;
              
    //'create the opc web client object
    OPCLabs.EasyOPCDANet.EasyDAWS o = new OPCLabs.EasyOPCDANet.EasyDAWS();
              
    //make the call to read multiple tags
    object values = (object)o.WriteMultipleItemValues(
         oPC,
         oOPC,
         oTags,
         new object[]{10, 20},
         null,
         null,
         null);
              
    //string needed to generate output message
    System.Text.StringBuilder sb = new System.Text.StringBuilder();

    //create an array so we can iterate thru it
    Array ar = (Array)values;

    //iterate thru array
    for(int i = 0; i < ar.GetLength(0); i++)
    {
    //build-up our string message
         sb.Append(ar.GetValue(i) + "\n");
    }

    //display the message output to screen
    MessageBox.Show(sb.ToString());

    }
    catch(Exception ex)
    {
         MessageBox.Show("Err: " + ex.Message.ToString());
    }

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