⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 browsetreebrowse.cs

📁 利用OpcNet控件(包含源码)进行OPC通讯的开发样例
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using HaiGrang.Package.OpcNetApiChs.Opc;
using HaiGrang.Package.OpcNetApiChs.Da;
using HaiGrang.Package.OpcNetApiChs.DaNet;

namespace BrowseTreeBrowse
{
	/// <summary>
	/// BrowseTreeBrowse 的摘要说明。
	/// </summary>

	public class BrowseTreeBrowseForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button OpcServerDisConnect;
		private System.Windows.Forms.Label OpcServerHostInfo;
		private System.Windows.Forms.Label bOpcServerName;
		private System.Windows.Forms.Button bOpcServerConnect;
		private System.Windows.Forms.Button BrowseTree;
		private System.Windows.Forms.Label BrowseTreeName;
		private System.Windows.Forms.TreeView OpcBrowTree;
		private System.Windows.Forms.Button OpcBrowseTreeNode;

		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		
		private System.ComponentModel.Container components = null;

		public BrowseTreeBrowseForm()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>

		protected override void Dispose( bool disposing )
		{
			if(tOpcServer != null)
			{
				tOpcServer.Disconnect() ;
			}
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.OpcServerDisConnect = new System.Windows.Forms.Button();
			this.OpcServerHostInfo = new System.Windows.Forms.Label();
			this.bOpcServerName = new System.Windows.Forms.Label();
			this.bOpcServerConnect = new System.Windows.Forms.Button();
			this.BrowseTree = new System.Windows.Forms.Button();
			this.BrowseTreeName = new System.Windows.Forms.Label();
			this.OpcBrowTree = new System.Windows.Forms.TreeView();
			this.OpcBrowseTreeNode = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// OpcServerDisConnect
			// 
			this.OpcServerDisConnect.Location = new System.Drawing.Point(56, 432);
			this.OpcServerDisConnect.Name = "OpcServerDisConnect";
			this.OpcServerDisConnect.Size = new System.Drawing.Size(216, 32);
			this.OpcServerDisConnect.TabIndex = 36;
			this.OpcServerDisConnect.Text = "Opc Server DisConnect";
			this.OpcServerDisConnect.Click += new System.EventHandler(this.OpcServerDisConnect_Click);
			// 
			// OpcServerHostInfo
			// 
			this.OpcServerHostInfo.BackColor = System.Drawing.Color.Yellow;
			this.OpcServerHostInfo.Location = new System.Drawing.Point(56, 216);
			this.OpcServerHostInfo.Name = "OpcServerHostInfo";
			this.OpcServerHostInfo.Size = new System.Drawing.Size(216, 32);
			this.OpcServerHostInfo.TabIndex = 35;
			this.OpcServerHostInfo.Text = "Opc Server HostInfo";
			this.OpcServerHostInfo.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// bOpcServerName
			// 
			this.bOpcServerName.BackColor = System.Drawing.Color.Yellow;
			this.bOpcServerName.Location = new System.Drawing.Point(56, 72);
			this.bOpcServerName.Name = "bOpcServerName";
			this.bOpcServerName.Size = new System.Drawing.Size(216, 32);
			this.bOpcServerName.TabIndex = 34;
			this.bOpcServerName.Text = "Opc Server Name";
			this.bOpcServerName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// bOpcServerConnect
			// 
			this.bOpcServerConnect.Location = new System.Drawing.Point(56, 288);
			this.bOpcServerConnect.Name = "bOpcServerConnect";
			this.bOpcServerConnect.Size = new System.Drawing.Size(216, 32);
			this.bOpcServerConnect.TabIndex = 33;
			this.bOpcServerConnect.Text = "Opc Server Connect";
			this.bOpcServerConnect.Click += new System.EventHandler(this.bOpcServerConnect_Click);
			// 
			// BrowseTree
			// 
			this.BrowseTree.Location = new System.Drawing.Point(56, 360);
			this.BrowseTree.Name = "BrowseTree";
			this.BrowseTree.Size = new System.Drawing.Size(216, 32);
			this.BrowseTree.TabIndex = 38;
			this.BrowseTree.Text = "Browse Tree";
			this.BrowseTree.Click += new System.EventHandler(this.BrowseTree_Click);
			// 
			// BrowseTreeName
			// 
			this.BrowseTreeName.BackColor = System.Drawing.Color.Yellow;
			this.BrowseTreeName.Location = new System.Drawing.Point(56, 144);
			this.BrowseTreeName.Name = "BrowseTreeName";
			this.BrowseTreeName.Size = new System.Drawing.Size(216, 32);
			this.BrowseTreeName.TabIndex = 37;
			this.BrowseTreeName.Text = "Browse Tree Name";
			this.BrowseTreeName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// OpcBrowTree
			// 
			this.OpcBrowTree.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.OpcBrowTree.ImageIndex = -1;
			this.OpcBrowTree.Location = new System.Drawing.Point(344, 72);
			this.OpcBrowTree.Name = "OpcBrowTree";
			this.OpcBrowTree.SelectedImageIndex = -1;
			this.OpcBrowTree.Size = new System.Drawing.Size(216, 392);
			this.OpcBrowTree.TabIndex = 39;
			// 
			// OpcBrowseTreeNode
			// 
			this.OpcBrowseTreeNode.Location = new System.Drawing.Point(624, 72);
			this.OpcBrowseTreeNode.Name = "OpcBrowseTreeNode";
			this.OpcBrowseTreeNode.Size = new System.Drawing.Size(216, 32);
			this.OpcBrowseTreeNode.TabIndex = 40;
			this.OpcBrowseTreeNode.Text = "Opc BrowseTree";
			this.OpcBrowseTreeNode.Click += new System.EventHandler(this.OpcBrowseTreeNode_Click);
			// 
			// BrowseTreeBrowseForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(1016, 741);
			this.Controls.Add(this.OpcBrowseTreeNode);
			this.Controls.Add(this.OpcBrowTree);
			this.Controls.Add(this.BrowseTree);
			this.Controls.Add(this.BrowseTreeName);
			this.Controls.Add(this.OpcServerDisConnect);
			this.Controls.Add(this.OpcServerHostInfo);
			this.Controls.Add(this.bOpcServerName);
			this.Controls.Add(this.bOpcServerConnect);
			this.Name = "BrowseTreeBrowseForm";
			this.Text = "BrowseTreeBrowse";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Load += new System.EventHandler(this.BrowseTreeBrowseForm_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>

		[STAThread]
		static void Main() 
		{
			Application.Run(new BrowseTreeBrowseForm());
		}

		private void BrowseTreeBrowseForm_Load(object sender, System.EventArgs e)
		{
		
		}

		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@                      Opc Connect Server                       @@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

		private void bOpcServerConnect_Click(object sender, System.EventArgs e)
		{
			OpcServerConnect() ;
		}

		private string tMachineString = "" ;
		private string tKEPServerExUrlString = "KEPware.KEPServerEx.V4" ;

		OpcServer tOpcServer = new OpcServer() ;

		private void OpcServerConnect()
		{
			tOpcServer.Connect( tMachineString, tKEPServerExUrlString );
			bOpcServerName.Text = tOpcServer.ServerName.ToString() ;
			OpcServerHostInfo.Text = tOpcServer.HostInfo.ToString() ;
		}

		//  System.Int32 Browse ( System.Boolean OneLevelOnly , System.Boolean IncludeItems , 
		//  out System.Windows.Forms.TreeNode[] tree )
		//  System.Int32 Browse ( System.String startBranch , out System.Windows.Forms.TreeNode[] tree )
		//  System.Int32 Browse ( out System.Windows.Forms.TreeNode[] tree )

		private void BrowseTree_Click(object sender, System.EventArgs e)
		{
			OpcBrowseTree();
		}

		private void OpcBrowseTree()
		{
			OpcBrowTree.Nodes.Clear(); 
			BrowseTree itemTree =  tOpcServer.AddBrowseTree( OpcBrowTree ); 
			int rtc = itemTree.CreateTree(); 
			OpcBrowTree.Nodes.AddRange( itemTree.Root() );		
		}

		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@                    Opc DisConnect Server                      @@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

		private void OpcServerDisConnect_Click(object sender, System.EventArgs e)
		{
			tOpcServer.Disconnect() ;
			bOpcServerName.Text = "Opc Server Disconnect" ;
			OpcServerHostInfo.Text = "Opc Server Disconnect" ;
		}

		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@                     Opc Browse Tree Node                      @@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

		// BrowseTree ( HaiGrang.Package.OpcNetApiChs.DaNet.OpcServer srv )

		private BrowseTree tBrowseTree ;
		private int tBrowseTreeNumber ;
		private TreeNode[] tTreeNode ;

		private void OpcBrowseTreeNode_Click(object sender, System.EventArgs e)
		{
			OpcBrowTree.Nodes.Clear(); 
			tBrowseTree =  tOpcServer.AddBrowseTree( OpcBrowTree ); 
			tBrowseTreeNumber = tBrowseTree.Browse(out tTreeNode) ;
			int rtc = tBrowseTree.CreateTree(); 
			OpcBrowTree.Nodes.AddRange( tBrowseTree.Root() );
		}















	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -