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

📄 addrefreshgroupform.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 AddRefreshGroupSample
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>

	public class AddRefreshGroup : System.Windows.Forms.Form
	{
		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;
		private System.Windows.Forms.Label OpcRemoveRefreshGroupValue;

		/// <summary>
		/// 必需的设计器变量。
		/// </summary>

		private System.ComponentModel.Container components = null;

		public AddRefreshGroup()
		{
			//
			// 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.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.OpcRemoveRefreshGroupValue = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// OpcGroupName
			// 
			this.OpcGroupName.BackColor = System.Drawing.Color.Yellow;
			this.OpcGroupName.Location = new System.Drawing.Point(48, 112);
			this.OpcGroupName.Name = "OpcGroupName";
			this.OpcGroupName.Size = new System.Drawing.Size(216, 32);
			this.OpcGroupName.TabIndex = 54;
			this.OpcGroupName.Text = "Opc Refresh Group Name";
			this.OpcGroupName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// AddOpcGroup
			// 
			this.AddOpcGroup.Location = new System.Drawing.Point(304, 112);
			this.AddOpcGroup.Name = "AddOpcGroup";
			this.AddOpcGroup.Size = new System.Drawing.Size(216, 32);
			this.AddOpcGroup.TabIndex = 53;
			this.AddOpcGroup.Text = "Add Opc Refresh Group";
			this.AddOpcGroup.Click += new System.EventHandler(this.AddOpcGroup_Click);
			// 
			// OpcServerDisConnect
			// 
			this.OpcServerDisConnect.Location = new System.Drawing.Point(304, 240);
			this.OpcServerDisConnect.Name = "OpcServerDisConnect";
			this.OpcServerDisConnect.Size = new System.Drawing.Size(216, 32);
			this.OpcServerDisConnect.TabIndex = 52;
			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(48, 240);
			this.OpcServerRemove.Name = "OpcServerRemove";
			this.OpcServerRemove.Size = new System.Drawing.Size(216, 32);
			this.OpcServerRemove.TabIndex = 51;
			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(48, 48);
			this.bOpcServerName.Name = "bOpcServerName";
			this.bOpcServerName.Size = new System.Drawing.Size(216, 32);
			this.bOpcServerName.TabIndex = 50;
			this.bOpcServerName.Text = "Opc Server Name";
			this.bOpcServerName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// bOpcServerConnect
			// 
			this.bOpcServerConnect.Location = new System.Drawing.Point(304, 48);
			this.bOpcServerConnect.Name = "bOpcServerConnect";
			this.bOpcServerConnect.Size = new System.Drawing.Size(216, 32);
			this.bOpcServerConnect.TabIndex = 49;
			this.bOpcServerConnect.Text = "Opc Server Connect";
			this.bOpcServerConnect.Click += new System.EventHandler(this.bOpcServerConnect_Click);
			// 
			// OpcRemoveRefreshGroupValue
			// 
			this.OpcRemoveRefreshGroupValue.BackColor = System.Drawing.Color.Yellow;
			this.OpcRemoveRefreshGroupValue.Location = new System.Drawing.Point(48, 176);
			this.OpcRemoveRefreshGroupValue.Name = "OpcRemoveRefreshGroupValue";
			this.OpcRemoveRefreshGroupValue.Size = new System.Drawing.Size(216, 32);
			this.OpcRemoveRefreshGroupValue.TabIndex = 57;
			this.OpcRemoveRefreshGroupValue.Text = "Opc Remove Refresh Group Value";
			this.OpcRemoveRefreshGroupValue.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// AddRefreshGroup
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(1016, 741);
			this.Controls.Add(this.OpcRemoveRefreshGroupValue);
			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 = "AddRefreshGroup";
			this.Text = "AddRefreshGroup";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Load += new System.EventHandler(this.AddRefreshGroup_Load);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void AddRefreshGroup_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() ;
			OpcServerRemove.Text = tOpcServer.HostInfo.ToString() ;
		}

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

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

		private RefreshGroup tRefreshGroup ;
		private int tEquestedUpdateRate = 1000;
		private string  OpcItem1Name = "WaterMotorControl.Group02.WaterMotorpictureBox1";
		private string tReturnGroupName;

		// HaiGrang.Package.OpcNetApiChs.DaNet.RefreshGroup AddRefreshGroup
		// ( System.Int32 Rate , HaiGrang.Package.OpcNetApiChs.DaNet.RefreshEventHandler UserHnd )

		// HaiGrang.Package.OpcNetApiChs.DaNet.RefreshGroup AddRefreshGroup
		// ( HaiGrang.Package.OpcNetApiChs.DaNet.RefreshEventHandler UserHnd )
		
		private void OpcAddGroup()
		{
			tRefreshGroup = tOpcServer.AddRefreshGroup
				(tEquestedUpdateRate, new HaiGrang.Package.OpcNetApiChs.DaNet.RefreshEventHandler( UserRefreshEventHandler ));
			int rtc  = tRefreshGroup.Add( OpcItem1Name) ; 
			tReturnGroupName = tRefreshGroup.OpcGrp.Name.ToString() ;
			OpcGroupName.Text = tReturnGroupName ;
		}

		private void UserRefreshEventHandler( object sender, RefreshEventArguments tRefreshEventArguments ) 
		{ 
			OpcRemoveRefreshGroupValue.Text = tRefreshEventArguments.items[0].OpcIRslt.DataValue.ToString() ;
		}  
  
		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@                    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 + -