C#
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 Read Example

The following example builds upon the Tutorial - Using with C#.

This page shows how to:

  • do a one-time read, for one tag
  • do a one-time read, for multiple tags, in a single function-call
  • do subscription reads (Exception based reads)

Reading a Single Tag:

Simply copy and paste the following code into your application:

try
{
//create our opc web client object
   OPCLabs.EasyOPCDANet.EasyDACOM objOWC = new OPCLabs.EasyOPCDANet.EasyDACOM();

//create our vtq object to receive the reading
           OPCLabs.EasyOPCDANet.IVTQNet objTagVTQ;

//make the call to get the tag reading
   objTagVTQ = objOWC.ReadItem(strPCName, strOPCServer, strTag, strAccessPath, iDataType, null);
  
//like any good coder, we check the quality of the tag
if(objTagVTQ.Quality >= 192)
{
MessageBox.Show("Value: " + objTagVTQ.Value.ToString()
 + " Quality: " + objTagVTQ.Quality.ToString()
 + " Timestamp: " + objTagVTQ .TimeStamp.ToString());
}
else
{
MessageBox.Show("BAD QUALITY TAG! " + objTagVTQ.Quality.ToString());
}
  
//now to clean-up
   objTagVTQ = null;
   objOWC = null;
}
catch(Exception ex)
{
   MessageBox.Show("Error: " + ex.Message.ToString());
}

Reading Multiple Tags within a single transaction:

Simply copy and paste the following code into your application:

    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.Random", "Trends.Ramp (1 min)"};
               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.EasyDACOM o = new OPCLabs.EasyOPCDANet.EasyDACOM();
              
    //make the call to read multiple tags
               object values = (object)o.ReadMultipleItemValues(
                   oPC,
                   oOPC,
                   oTags,
                   System.Reflection.Missing.Value,
                   System.Reflection.Missing.Value,
                   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("Value: " + ar.GetValue(i) + "\n");
               }

    //display the message output to screen
               MessageBox.Show(sb.ToString());
    }
    catch(Exception ex)
    {
    //'send exception messages to the screen
               MessageBox.Show("Err: " + ex.Message.ToString());
    }

Subscription Reads

    Subscription Reads cause an event to be raised whenever an item/Tag changes value. The OPC Server is continously polling the tag, so you dont have to!

    In this example the "tag" is the object (of type "IItemNet") that will be "bound" to a Tag subscription. This item needs to be kept alive to keep the subscription alive.

  1. IMPORTANT: You MUST read the following FAQ - Threading Apartments.

  [MTAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

//these variables would be defined in a global area so that they remain alive
private OPCLabs.EasyOPCDANet.EasyDACOM opc;
private OPCLabs.EasyOPCDANet.IItemNet tag;
private OPCLabs.EasyOPCDANet.IEasyDAModeNet mode;

private void button1_Click(object sender, System.EventArgs e)
     {
         try
       {
           tag=opc.RequestItem("","SWToolbox.TOPServer",
               "Channel_0_User_Defined.Ramp.Ramp1",
               Missing.Value, Missing.Value, mode, Missing.Value);

           tag.RequestedUpdateRate+=500;
           tag.MeasurementChange+=new EventHandler(tag_MeasurementChange);
         }//try
        
catch(Exception ex)
       {
           MessageBox.Show(ex.Message.ToString());
         }
//catch
     }//sub

     private void tag_MeasurementChange(object sender, EventArgs e)
     {
       OPCLabs.EasyOPCDANet.IItemNet osvr = (OPCLabs.EasyOPCDANet.IItemNet)sender;
         this.Text=osvr.VTQ.Value.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