Browsing
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

 

OPC Browsing

Be sure to check out the OPC Browser Tutorial.

This page shows you how to:

  • Browse available PC's
  • Browse available OPC Servers (using the dialog and via code)
  • Browse available OPC Groups/Branches (via code)
  • Browse available OPC Tags (using the dialog and via code)

Browsing available PCs

             try
             {
    //create a new browser object
               OPCUserBrowseMachineClass o = new OPCUserObjects.OPCUserBrowseMachine();
              
    //run the browser dialog
               o.RunMe();
              
    //check the result of the dialog
               if(o.MachineName.Equals(""))
               {
                   MessageBox.Show("No computer selected!");
               }
               else
               {
                   MessageBox.Show(o.MachineName.ToString());
               }
             }

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

Browsing Available OPC Servers

    Via Dialog Browser

         try
         {
     //define a browser object
           OPCUserBrowseServerClass o = new
                 OPCUserBrowseServerClass();

     //run the dialog
           o.MachineName = "";
           o..RunMe();
          
     //now to see what the user did with the dialog
           if(o.MachineName.Equals("") && o.ServerClass.Equals(""))
           {
               MessageBox.Show("Nothing selected!");
           }
           else
           {
               MessageBox.Show(o.MachineName + " - " + o.ServerClass);
           }
         }
         catch(Exception ex)
         {
           MessageBox.Show("Error: {0}", ex.Message.ToString());
         }

    Via Code

    The following code assumes that an empty for exists with a single listbox control in it. Place the following code in the Form_load event:
             try
             {

    //'create the opc web client object
               OPCLabs.EasyOPCDANet.EasyDACOM o = new OPCLabs.EasyOPCDANet.EasyDACOM();
              
    //create a dictionary
    //and make the call to get the opc servers list
               string strMachineName = "127.0.0.1"
               IDictionary serverElements = o.BrowseServers(strMachineName, null);
              
    //now iterate thru the collection
               foreach(object item in serverElements.Values)
               {

    //convert the item into a more usable object
    OPCLabs.EasyOPCDANet.IServerElementNet svr = (OPCLabs.EasyOPCDANet.IServerElementNet)item;

    //add the item to the listbox
                   listBox1.Items.Add(svr .ProgID);
               }
             }

             catch(Exception ex)
             {
               if(!(ex.InnerException == null))
               {
                   MessageBox.Show("Error: " + ex.InnerException.Message.ToString());
               }
               else
               {
                   MessageBox.Show("Error: " + ex.Message.ToString());
               }
             }

Browsing Available OPC Groups/Branches

    The following code assumes that an empty form exists with a single listbox control in it. Place the following code in the Form_load event:

try
{
// define an instance of the opc web client
           OPCLabs.EasyOPCDANet.EasyDACOM EasyDA = new OPCLabs.EasyOPCDANet.EasyDACOM();

string strMachineName = ""; //name of the computer to browse

// define an object to receive the branches
// and make the call to read the branches
           System.Collections.IDictionary BranchElements = EasyDA.BrowseBranches
           (
                       strMachineName,
                       "OPCLabs.KitServer.2",
                       "",
                       System.Reflection.Missing.Value,
                       System.Reflection.Missing.Value,
                       null
           );

           //now iterate thru our collection
           foreach(object key in BranchElements.Values)
           {
                       //convert the element into a usable object
                       OPCLabs.EasyOPCDANet.INodeElementNet be = (OPCLabs.EasyOPCDANet.INodeElementNet)key;

                       //is this object a branch?
                       if(be.IsBranch)
                       {
                                   //yes: add it to the listbox
                                   listBox1.Items.Add(be.Name.ToString());
                       }
           }

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

Browsing Available Tags

    Via Dialog Browser

           try
             {
               //create a browser object
               OPCUserBrowseItemClass o = new
                         OPCUserObjects.OPCUserBrowseItemClass();

               //set the server and computer to browse
               o.ServerClass = "OPCLabs.KitServer.2";
               o.MachineName = "127.0.0.1";

               //run the dialog
               o.RunMe();
              
               //now to see what the user picked
               if(o.ItemID.Equals(""))
               {
                   MessageBox.Show("Nothing selected!");
               }
               else
               {
                   MessageBox.Show("Tag: " + o.ItemID.ToString());
               }
             }
             catch(Exception ex)
             {
               MessageBox.Show("Error: " + ex.Message.ToString());
             }

    Via Code

      The following code assumes that an empty for exists with a single listbox control in it. Place the following code in the Form_load event:

         private void button1_Click(object sender,System.EventArgs e)
         {
             listBox1.Items.Clear();
             listBox1.Items.Add("Please wait...");

             try
             {
               //create a webclient object
               OPCLabs.EasyOPCDANet.EasyDACOM objOWC = new OPCLabs.EasyOPCDANet.EasyDACOM();

               //create a dictionary object for the leaves
               IDictionary leafElements;

               //set the name of the computer to browse on
               string strMachineName = "127.0.0.1";

               /*now make the call to get the leaves for the "Simulation"
                 * branch within the greenhouse opc server*/
               leafElements = objOWC.BrowseLeafs(strMachineName, "OPCLabs.KitServer.2", "Simulation", null, null, null);

               /*now clear the listbox because we are about to
               * populate it*/
               listBox1.Items.Clear();

               //now iterate thru our collection
               foreach(OPCLabs.EasyOPCDANet.INodeElementNet key in leafElements.Values)
               {

                   //now to add into listbox
                   listBox1.Items.Add("Tag: " + key.Name.ToString()
                     + ", ItemID: " + key.ItemID.ToString());
               }

             }
             catch(Exception ex)
             {
               MessageBox.Show(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