C# Reading
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 Examples

Reading Tags in an NT Service application using OPC Web Client Web Service

Reading Single Tags

    string strPCName = "";
    string strOPCServer = "
    OPCLabs.KitServer.2";
    string strTag = "Simulation.Random";
    string strAccessPath = "";
    int iDataType = 0;

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

    //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(System.Web.Services.Protocols.SoapException webex)
    {
       MessageBox.Show("Error: "  + webex.Message.ToString());
    }
    catch(Exception ex)
    {
       MessageBox.Show("Error: " + ex.Message.ToString());
    }

Reading Multiple Tags

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

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

    }
    catch(System.Web.Services.Protocols.SoapException webex)
    {
               MessageBox.Show("Error: "  + webex.Message.ToString());
    }
    catch(Exception ex)
    {
    //'send exception messages to the screen
               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