使用 OPC Browser 加載 OPC Server 監測點


1,首先第一步,要連接OPC ,創建好 OPC對象。

        /// <summary>
        /// 連接OPC
        /// </summary>
       private string OPCIP=127.0.0.1;

       private string OPCName=PCAuto.OPCServer;
        public void Connect()
        {
            if (string.IsNullOrEmpty(OPCIP))
            {
                throw new ArgumentNullException("UaServer");
            }
            if (string.IsNullOrEmpty(OPCName))
            {
                throw new ArgumentNullException("UaServer");
            }
            if (CreateServer())
            {
                try
                {
                    opcServer.Connect(OPCName, OPCIP);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("連接到OPC服務器失敗!" + ex.Message);
                }
            }
            m_IsConnected = true;
        }

        /// <summary>
        /// 創建OPC服務
        /// </summary>
        /// <returns></returns>
        public bool CreateServer()
        {
            try
            {
                opcServer = new OPCServer();
            }
            catch (Exception ex)
            {
                MessageBox.Show("創建OPC服務出現異常:" + ex.Message);
                return false;
            }
            return true;
        }

2.窗體加載方法,我這里時winform 開發的程序。在窗體load事件加載 opc測點。使用 tree 控件。

        /// <summary>
        /// 測點掃描加載監測點樹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
    private TreeView treeView_opcItem;
private void ScanItem_Load(object sender, EventArgs e) { try { OpcDaClient opcDaClient = new OpcDaClient(); TreeNode node = new TreeNode(); OPCBrowser opcBrowser = opcDaClient.RecurBrowse(); opcDaClient.ShowInTreeView(opcBrowser, node); treeView_opcItem.Nodes.Add(node); TreeNodeCollection nodes = treeView_opcItem.Nodes; GetChildNode(nodes); } catch (Exception ex) { MessageBox.Show(ex.Message); } }

3.用到的子方法

        /// <summary>
        /// 創建opc節點瀏覽對象
        /// </summary>
        /// <returns></returns>
        public OPCBrowser RecurBrowse()
        {
            try
            {
                return opcServer.CreateBrowser();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }

 

   /// <summary>
        /// 展示OPC點號列表
        /// </summary>
        /// <param name="opcBrowser"></param>
        /// <param name="node"></param>
        public void ShowInTreeView(OPCBrowser opcBrowser, TreeNode node)
        {
            TreeNode treeNode = null;
            opcBrowser.ShowBranches();
            int count = opcBrowser.Count;
            List<string> list = null;
            if (opcBrowser.Count > 0)
            {
                list = new List<string>();
                foreach (string item in opcBrowser)
                {
                    list.Add(item);
                }
                foreach (string item2 in list)
                {
                    treeNode = new TreeNode(item2);
                    treeNode.Tag = opcBrowser.GetItemID(item2);
                    node.Nodes.Add(treeNode);
                    opcBrowser.MoveDown(item2);
                    ShowInTreeView(opcBrowser, treeNode);
                    opcBrowser.MoveUp();
                }
            }
            opcBrowser.ShowLeafs(Type.Missing);
            int count2 = opcBrowser.Count;
            foreach (string item3 in opcBrowser)
            {
                treeNode = new TreeNode(opcBrowser.GetItemID(item3));
                treeNode.Tag = opcBrowser.GetItemID(item3);
                node.Nodes.Add(treeNode);
            }
        }

4.獲取當前節點下的子節點

        private void GetChildNode(TreeNodeCollection nodes)
        {
            string empty = string.Empty;
            foreach (TreeNode node in nodes)
            {
                empty = node.Text;
                if (node.Nodes.Count == 0)
                {
                    itemNameList.Add(node.Text);
                }
                GetChildNode(node.Nodes);
            }
        }

 5.效果展示

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM