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

📄 form1.cs

📁 OPC 开发工具包2.0.OPC工具包是用来简化OPC规范服务器开发的工具包
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

class tag
{
	public uint handle;
	public string name;
	public object value;
	public ushort quality;
	public long timeStamp;

	public tag(string pName) 
	{
		handle=0;
		quality=0;
		value=0;
		timeStamp=DateTime.Now.ToFileTime();
		name=pName;
	}
}
namespace DAS_CSDEMO
{
	unsafe public delegate void WRITECALLBACK(uint handle, object value, ref int errorCode);
	unsafe public delegate void WRITECALLBACKEX(uint handle, uint propertyID, object value, ref int errorCode);
	unsafe public delegate void READCALLBACK(uint handle,ref object value, ref ushort quality,ref long timeStamp,ref int errorCode);
	unsafe public delegate void READCALLBACKEX(uint handle, uint propertyID, ref object value, ref ushort quality,ref long timeStamp,ref int errorCode);
	unsafe public delegate void SERVERNOTIFY(uint handle, ushort operationType);
	unsafe public delegate void GROUPNOTIFY(uint handle, string groupName, ushort operationType);
	unsafe public delegate void ITEMNOTIFY(uint handle, uint tagHandle,ushort operationType);
	unsafe public delegate void TAGNOTIFY(string tagName, string tagAccessPath, uint* tagHandle);
	unsafe public delegate void LOGNOTIFY(string logMsg);


	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem mnFile;
		private System.Windows.Forms.MenuItem mnRegister;
		private System.Windows.Forms.MenuItem mnUnregister;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem mnExit;
		private System.Windows.Forms.MenuItem mnHelp;
		private System.Windows.Forms.MenuItem mnAbout;
		private System.Windows.Forms.Timer timer1;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.StatusBarPanel statusBarPanel1;
		private System.Windows.Forms.StatusBarPanel statusBarPanel2;
		private System.Windows.Forms.StatusBarPanel statusBarPanel3;
		private System.Windows.Forms.TreeView treeView1;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.ListView listView1;
		private System.ComponentModel.IContainer components;
		private const string ClassID = "{E01F7DF1-6311-48b5-A1A5-E4B3B9C933F2}";
		private const string ProgID = "OPC.ASCSDEMO.1";
		private const string Desc = "OPC ASCSDEMO 1";
		private ArrayList tagList;
		private int seed, cntCount=0;

		private WRITECALLBACK m_writeCallback;
		private READCALLBACK m_readCallback;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.ColumnHeader columnHeader4;
		private SERVERNOTIFY m_serverNotify;

		
		[DllImport("ASOPCDA.dll")]
		public static extern bool RegServer(string classID, string progID, string desc, string path);
		[DllImport("ASOPCDA.dll")]
		public static extern bool UnregServer(string classID, string progID);
		[DllImport("ASOPCDA.dll")]
		public static extern bool CreateServer(string classID, uint updateRate);
		[DllImport("ASOPCDA.dll")]
		public static extern void FreeServer();
		[DllImport("ASOPCDA.dll")]
		public static extern bool CreateServer(string userID, string passWord);
		[DllImport("ASOPCDA.dll")]
		public static extern void ShutDown();
		[DllImport("ASOPCDA.dll")]
		public static extern bool SetServerState(ushort serverState);
		[DllImport("ASOPCDA.dll")]
		public static extern void SetVersion(string vendor, ushort majorVersion, ushort minorVersion, ushort buildNumber);
		[DllImport("ASOPCDA.dll")]
		public static extern uint RegTag(uint parentHandle,string tagName, object value, ushort quality, ushort accessRight);
		[DllImport("ASOPCDA.dll")]
		public static extern uint RegTagEx(uint parentHandle,string tagName, ushort dataType, ushort accessRight);
		[DllImport("ASOPCDA.dll")]
		public static extern bool UnregTag(uint handle);
		[DllImport("ASOPCDA.dll")]
		public static extern bool AddTagProperty(uint handle, string propertyName, string propertyDesc, object propertyValue);
		[DllImport("ASOPCDA.dll")]
		public static extern bool AddTagPropertyEx(uint handle, string propertyName, string propertyDesc, ushort dataType);
		[DllImport("ASOPCDA.dll")]
		public static extern bool SetTagQualifier(char qualifier);
		[DllImport("ASOPCDA.dll")]
		public static extern bool UpdateTag(uint handle, object value, ushort quality);
		[DllImport("ASOPCDA.dll")]
		public static extern bool UpdateTagWithTimeStamp(uint handle, object value, ushort quality, long TimeStamp);
		[DllImport("ASOPCDA.dll")]
		public static extern bool SetTagProperty(uint handle, uint propertyID, object value);
		[DllImport("ASOPCDA.dll")]
		public static extern bool SetWriteCallback(WRITECALLBACK lpCallback);
		[DllImport("ASOPCDA.dll")]
		public static extern bool SetWriteCallbackEx(WRITECALLBACKEX lpCallback);
		[DllImport("ASOPCDA.dll")]
		public static extern bool SetReadCallback(READCALLBACK lpCallback);
		[DllImport("ASOPCDA.dll")]
		public static extern bool SetReadCallbackEx(READCALLBACKEX lpCallback);
		[DllImport("ASOPCDA.dll")]
		public static extern bool SetServerNotify(SERVERNOTIFY lpCallback);
		[DllImport("ASOPCDA.dll")]
		public static extern bool SetGroupNotify(GROUPNOTIFY lpCallback);
		[DllImport("ASOPCDA.dll")]
		public static extern bool SetItemNotify(ITEMNOTIFY lpCallback);
		[DllImport("ASOPCDA.dll")]
		public static extern bool SetLogNotify(LOGNOTIFY lpCallback);

		unsafe public void WriteCallback(uint handle, object value, ref int errorCode)
		{
			errorCode=1;
			foreach(tag Tag in tagList)
			{
				if(Tag.handle==handle)
				{
					Tag.value=value;
					Tag.quality=192;
					Tag.timeStamp=DateTime.Now.ToFileTime();
					UpdateTagWithTimeStamp(Tag.handle, Tag.value, Tag.quality, Tag.timeStamp);
					errorCode=0;
					Monitor();
					break;
				}
			}
		}

		unsafe public  void ReadCallback(uint handle, ref object value, ref ushort quality, ref long timeStamp,ref int errorCode)
		{
			errorCode=1;
			foreach(tag Tag in tagList)
			{
				if(Tag.handle==handle)
				{
					value=Tag.value;
					quality=Tag.quality;
					timeStamp=Tag.timeStamp;
					errorCode=0;
					break;
				}
			}
		}

		unsafe public void ServerNotify(uint handle, ushort operationType)
		{
			if(operationType==0)
				cntCount++;
			else
				cntCount--;
			if(cntCount<=0)
				Close();
		}

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

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

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.mnFile = new System.Windows.Forms.MenuItem();
			this.mnRegister = new System.Windows.Forms.MenuItem();
			this.mnUnregister = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.mnExit = new System.Windows.Forms.MenuItem();
			this.mnHelp = new System.Windows.Forms.MenuItem();
			this.mnAbout = new System.Windows.Forms.MenuItem();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
			this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
			this.statusBarPanel3 = new System.Windows.Forms.StatusBarPanel();
			this.treeView1 = new System.Windows.Forms.TreeView();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.listView1 = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel3)).BeginInit();
			this.SuspendLayout();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mnFile,
																					  this.mnHelp});
			// 
			// mnFile
			// 
			this.mnFile.Index = 0;
			this.mnFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																				   this.mnRegister,
																				   this.mnUnregister,
																				   this.menuItem1,
																				   this.mnExit});
			this.mnFile.Text = "&File";
			// 
			// mnRegister
			// 
			this.mnRegister.Index = 0;
			this.mnRegister.Text = "&Register Server";
			this.mnRegister.Click += new System.EventHandler(this.mnRegister_Click);
			// 
			// mnUnregister
			// 
			this.mnUnregister.Index = 1;
			this.mnUnregister.Text = "&Unregister Server";
			this.mnUnregister.Click += new System.EventHandler(this.mnUnregister_Click);
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 2;
			this.menuItem1.Text = "-";
			// 
			// mnExit
			// 
			this.mnExit.Index = 3;
			this.mnExit.Text = "E&xit";
			this.mnExit.Click += new System.EventHandler(this.mnExit_Click);
			// 
			// mnHelp
			// 
			this.mnHelp.Index = 1;
			this.mnHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																				   this.mnAbout});
			this.mnHelp.Text = "&Help";
			// 
			// mnAbout
			// 
			this.mnAbout.Index = 0;
			this.mnAbout.Text = "&About...";
			// 
			// timer1
			// 
			this.timer1.Interval = 1000;
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 411);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
																						  this.statusBarPanel1,
																						  this.statusBarPanel2,
																						  this.statusBarPanel3});
			this.statusBar1.ShowPanels = true;
			this.statusBar1.Size = new System.Drawing.Size(632, 22);
			this.statusBar1.TabIndex = 3;
			this.statusBar1.Text = "statusBar1";
			// 
			// statusBarPanel1
			// 

⌨️ 快捷键说明

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