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

📄 addgroupform.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 AddGroupSample
{
	/// <summary>
	/// AddGroupSample 的摘要说明。
	/// </summary>

	public class AddGroup : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label OpcRemoveGroupName;
		private System.Windows.Forms.Button RemoveOpcGroup;
		private System.Windows.Forms.Label OpcGroupName;
		private System.Windows.Forms.Button AddOpcGroup;
		private System.Windows.Forms.Button OpcServerDisConnect;
		private System.Windows.Forms.Label OpcServerRemove;
		private System.Windows.Forms.Label bOpcServerName;
		private System.Windows.Forms.Button bOpcServerConnect;

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

		public AddGroup()
		{
			//
			// 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.OpcRemoveGroupName = new System.Windows.Forms.Label();
			this.RemoveOpcGroup = new System.Windows.Forms.Button();
			this.OpcGroupName = new System.Windows.Forms.Label();
			this.AddOpcGroup = new System.Windows.Forms.Button();
			this.OpcServerDisConnect = new System.Windows.Forms.Button();
			this.OpcServerRemove = new System.Windows.Forms.Label();
			this.bOpcServerName = new System.Windows.Forms.Label();
			this.bOpcServerConnect = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// OpcRemoveGroupName
			// 
			this.OpcRemoveGroupName.BackColor = System.Drawing.Color.Yellow;
			this.OpcRemoveGroupName.Location = new System.Drawing.Point(80, 184);
			this.OpcRemoveGroupName.Name = "OpcRemoveGroupName";
			this.OpcRemoveGroupName.Size = new System.Drawing.Size(216, 32);
			this.OpcRemoveGroupName.TabIndex = 48;
			this.OpcRemoveGroupName.Text = "Opc Remove Group Name";
			this.OpcRemoveGroupName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// RemoveOpcGroup
			// 
			this.RemoveOpcGroup.Location = new System.Drawing.Point(336, 184);
			this.RemoveOpcGroup.Name = "RemoveOpcGroup";
			this.RemoveOpcGroup.Size = new System.Drawing.Size(216, 32);
			this.RemoveOpcGroup.TabIndex = 47;
			this.RemoveOpcGroup.Text = "Remove Opc Group";
			this.RemoveOpcGroup.Click += new System.EventHandler(this.RemoveOpcGroup_Click);
			// 
			// OpcGroupName
			// 
			this.OpcGroupName.BackColor = System.Drawing.Color.Yellow;
			this.OpcGroupName.Location = new System.Drawing.Point(80, 120);
			this.OpcGroupName.Name = "OpcGroupName";
			this.OpcGroupName.Size = new System.Drawing.Size(216, 32);
			this.OpcGroupName.TabIndex = 46;
			this.OpcGroupName.Text = "Opc Group Name";
			this.OpcGroupName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// AddOpcGroup
			// 
			this.AddOpcGroup.Location = new System.Drawing.Point(336, 120);
			this.AddOpcGroup.Name = "AddOpcGroup";
			this.AddOpcGroup.Size = new System.Drawing.Size(216, 32);
			this.AddOpcGroup.TabIndex = 45;
			this.AddOpcGroup.Text = "Add Opc Group";
			this.AddOpcGroup.Click += new System.EventHandler(this.AddOpcGroup_Click);
			// 
			// OpcServerDisConnect
			// 
			this.OpcServerDisConnect.Location = new System.Drawing.Point(336, 248);
			this.OpcServerDisConnect.Name = "OpcServerDisConnect";
			this.OpcServerDisConnect.Size = new System.Drawing.Size(216, 32);
			this.OpcServerDisConnect.TabIndex = 44;
			this.OpcServerDisConnect.Text = "Opc Server DisConnect";
			this.OpcServerDisConnect.Click += new System.EventHandler(this.OpcServerDisConnect_Click);
			// 
			// OpcServerRemove
			// 
			this.OpcServerRemove.BackColor = System.Drawing.Color.Yellow;
			this.OpcServerRemove.Location = new System.Drawing.Point(80, 248);
			this.OpcServerRemove.Name = "OpcServerRemove";
			this.OpcServerRemove.Size = new System.Drawing.Size(216, 32);
			this.OpcServerRemove.TabIndex = 43;
			this.OpcServerRemove.Text = "Opc Server Remove";
			this.OpcServerRemove.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// bOpcServerName
			// 
			this.bOpcServerName.BackColor = System.Drawing.Color.Yellow;
			this.bOpcServerName.Location = new System.Drawing.Point(80, 56);
			this.bOpcServerName.Name = "bOpcServerName";
			this.bOpcServerName.Size = new System.Drawing.Size(216, 32);
			this.bOpcServerName.TabIndex = 42;
			this.bOpcServerName.Text = "Opc Server Name";
			this.bOpcServerName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// bOpcServerConnect
			// 
			this.bOpcServerConnect.Location = new System.Drawing.Point(336, 56);
			this.bOpcServerConnect.Name = "bOpcServerConnect";
			this.bOpcServerConnect.Size = new System.Drawing.Size(216, 32);
			this.bOpcServerConnect.TabIndex = 41;
			this.bOpcServerConnect.Text = "Opc Server Connect";
			this.bOpcServerConnect.Click += new System.EventHandler(this.bOpcServerConnect_Click);
			// 
			// AddGroup
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(1016, 741);
			this.Controls.Add(this.OpcRemoveGroupName);
			this.Controls.Add(this.RemoveOpcGroup);
			this.Controls.Add(this.OpcGroupName);
			this.Controls.Add(this.AddOpcGroup);
			this.Controls.Add(this.OpcServerDisConnect);
			this.Controls.Add(this.OpcServerRemove);
			this.Controls.Add(this.bOpcServerName);
			this.Controls.Add(this.bOpcServerConnect);
			this.Name = "AddGroup";
			this.Text = "AddGroup";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Load += new System.EventHandler(this.AddGroup_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new AddGroup());
		}

		private void AddGroup_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 = "192.168.0.44" ;

		OpcServer tOpcServer = new OpcServer() ;

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

		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@                           Add Opc Group                       @@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

		private void AddOpcGroup_Click(object sender, System.EventArgs e)
		{
			OpcAddGroup() ;
		}

		private OpcGroup tOpcGroup ;
		private string tGroupName = "thisGroup" ;
		private bool tActive = true ;
		private int tEquestedUpdateRate = 1000;
		private float tDeadBand = 0.0F ;
		private string tReturnGroupName ;

		private void OpcAddGroup()
		{
			tOpcGroup = tOpcServer.AddGroup(tGroupName, tActive, tEquestedUpdateRate, ref tDeadBand, 0, 0);
			tReturnGroupName = tOpcGroup.Name.ToString() ;
			OpcGroupName.Text = tReturnGroupName ;
		}

		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@                           Add Opc Group                       @@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

		private void RemoveOpcGroup_Click(object sender, System.EventArgs e)
		{
			tOpcGroup.Remove(true) ;
			OpcGroupName.Text = "Opc Group already Remove" ;
		}

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

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

	










	}
}

⌨️ 快捷键说明

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