Ejemplo n.º 1
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            OutProc outProc = new OutProc();
            int     result  = (int)EnumResultCode.S_OK;

            try
            {
                outProc.CreateOpcClient();
                OpcClient opcClient = outProc.OpcClient;

                //	initialize the client instance
                if (!ResultCode.SUCCEEDED(opcClient.Initialize()))
                {
                    opcClient = null;
                    return;
                }                   //	end if

                //	initialize the DA client simulation
                result |= opcClient.InitializeDaObjects();

                OpcForm opcForm = new OpcForm(outProc);

                System.Windows.Forms.Application.Run(opcForm);


                opcClient.Terminate();
                opcClient = null;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }       //	end try...catch
        }           //	end Main
Ejemplo n.º 2
0
        public OpcForm(OutProc anOutProc)
        {
            InitializeComponent();

            try
            {
                m_outProc = anOutProc;

                m_opcClient = m_outProc.OpcClient;

                m_session = m_opcClient.GetSession();

                m_executionOptions = m_opcClient.GetExecutionOptions();

                //create the browse tree root element
                TreeNode treeRoot = new TreeNode(m_session.Url, 0, 0);
                DaAddressSpaceElement rootDaAddressSpaceElement = new DaAddressSpaceElement(EnumAddressSpaceElementType.BRANCH, String.Empty, string.Empty, string.Empty, 0, null);
                rootDaAddressSpaceElement.Session = m_session;
                treeRoot.Tag = rootDaAddressSpaceElement;
                itemsTreeView.Nodes.Add(treeRoot);

                DaAddressSpaceElement[] addressSpaceElements = null;

                #region Browse branches
                //---------------------
                DaAddressSpaceElementBrowseOptions browseOptions = new DaAddressSpaceElementBrowseOptions();
                browseOptions.ElementTypeFilter = EnumAddressSpaceElementType.BRANCH;

                //get the root's children
                if (ResultCode.SUCCEEDED(m_session.Browse(
                                             ((DaAddressSpaceElement)treeRoot.Tag).ItemId,
                                             ((DaAddressSpaceElement)treeRoot.Tag).ItemPath,
                                             browseOptions,
                                             out addressSpaceElements,
                                             m_executionOptions)))
                {
                    if (addressSpaceElements != null)
                    {
                        for (int i = 0; i < addressSpaceElements.Length; i++)
                        {
                            TreeNode node = new TreeNode(addressSpaceElements[i].Name, 1, 1);
                            node.Tag = addressSpaceElements[i];
                            treeRoot.Nodes.Add(node);
                        }        //end for
                    }
                }                //end if

                //-
                #endregion

                addressSpaceElements = null;

                #region Browse Leaves
                //-------------------
                browseOptions.ElementTypeFilter = EnumAddressSpaceElementType.LEAF;

                //get the root's children
                if (ResultCode.SUCCEEDED(m_session.Browse(
                                             ((DaAddressSpaceElement)treeRoot.Tag).ItemId,
                                             ((DaAddressSpaceElement)treeRoot.Tag).ItemPath,
                                             browseOptions,
                                             out addressSpaceElements,
                                             m_executionOptions)))
                {
                    if (addressSpaceElements != null)
                    {
                        for (int i = 0; i < addressSpaceElements.Length; i++)
                        {
                            TreeNode node = new TreeNode(addressSpaceElements[i].Name, 2, 2);
                            node.Tag = addressSpaceElements[i];
                            treeRoot.Nodes.Add(node);
                        }        //end for
                    }            //  end if
                }                //end if
                                 //-
                #endregion

                #region GetDaProperties
                //---------------------

                DaProperty[] daProperties = null;
                m_propertyGetOptions.WhatPropertyData = EnumPropertyData.ALL;

                if (ResultCode.SUCCEEDED(rootDaAddressSpaceElement.GetDaProperties(
                                             m_propertyGetOptions,
                                             out daProperties,
                                             m_executionOptions)))
                {
                    if (daProperties != null)
                    {
                        for (int i = 0; i < daProperties.Length; i++)
                        {
                            TreeNode node = new TreeNode(daProperties[i].Name, 3, 3);
                            //add all properties except OPC-specific properties
                            if (daProperties[i].Id >= 100)
                            {
                                treeRoot.Nodes.Add(node);
                            }
                        }           //end for
                    }
                }                   //  end if

                //-
                #endregion
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }       //	end try...catch
        }           //	end constructor