using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using CgpibCs;
using CSubFuncCs;

namespace Polling
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Polling : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox txtMyAdr;
		private System.Windows.Forms.ComboBox cboYrAdrBox;
		private System.Windows.Forms.TextBox txtPollingSts;
		private System.Windows.Forms.Button btnPollDel;
		private System.Windows.Forms.Button btnRetDel;
		private System.Windows.Forms.Button btnPolling;
		private System.Windows.Forms.TextBox txtRet;
		private System.Windows.Forms.Button btnInit;
		private System.Windows.Forms.Button btnExt;	
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		Cgpib		gpib = new Cgpib();
		CSubFunc	SubFunc = new CSubFunc(); 

		///////////////////////////////////////////////////////// Setting ///
		string		szBuffer = new string(' ', 1024);
		uint		Ret;
		int			Count;
		uint		Myadr, Yradr;
		//uint		Srlen;
		uint[]		Pstb = new uint[32];
		string		srBuffer = new string(' ', 10000);
		string		csBuf;
		string		PollBuf;
		GCHandle    gChCmd;                 				// GCHandle for cmd array
    	GCHandle    gChPstb;             					// GCHandle for Pstb array
    	IntPtr     	pPstb;                   				// IntPtr for Pstb array
		/////////////////////////////////////////////////////////////////////

		public Polling()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.cboYrAdrBox = new System.Windows.Forms.ComboBox();
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.txtMyAdr = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.txtPollingSts = new System.Windows.Forms.TextBox();
			this.btnPollDel = new System.Windows.Forms.Button();
			this.btnRetDel = new System.Windows.Forms.Button();
			this.btnPolling = new System.Windows.Forms.Button();
			this.label4 = new System.Windows.Forms.Label();
			this.txtRet = new System.Windows.Forms.TextBox();
			this.btnInit = new System.Windows.Forms.Button();
			this.btnExt = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.cboYrAdrBox,
																					this.label1});
			this.groupBox1.Location = new System.Drawing.Point(245, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(239, 53);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Address of device";
			// 
			// cboYrAdrBox
			// 
			this.cboYrAdrBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboYrAdrBox.Items.AddRange(new object[] {
															 "0",
															 "1",
															 "2",
															 "3",
															 "4",
															 "5",
															 "6",
															 "7",
															 "8",
															 "9",
															 "10",
															 "11",
															 "12",
															 "13",
															 "14",
															 "15",
															 "16",
															 "17",
															 "18",
															 "19",
															 "20",
															 "21",
															 "22",
															 "23",
															 "24",
															 "25",
															 "26",
															 "27",
															 "28",
															 "29",
															 "30"});
			this.cboYrAdrBox.Location = new System.Drawing.Point(135, 19);
			this.cboYrAdrBox.Name = "cboYrAdrBox";
			this.cboYrAdrBox.Size = new System.Drawing.Size(48, 24);
			this.cboYrAdrBox.TabIndex = 1;
			this.cboYrAdrBox.SelectedIndexChanged += new System.EventHandler(this.cboYrAdrBox_SelectedIndexChanged);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(63, 21);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(72, 20);
			this.label1.TabIndex = 0;
			this.label1.Text = "Address  =";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtMyAdr,
																					this.label2});
			this.groupBox2.Location = new System.Drawing.Point(8, 8);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(239, 53);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Information";
			// 
			// txtMyAdr
			// 
			this.txtMyAdr.Location = new System.Drawing.Point(152, 20);
			this.txtMyAdr.Name = "txtMyAdr";
			this.txtMyAdr.ReadOnly = true;
			this.txtMyAdr.Size = new System.Drawing.Size(32, 22);
			this.txtMyAdr.TabIndex = 1;
			this.txtMyAdr.Text = "0";
			this.txtMyAdr.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(48, 21);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(96, 20);
			this.label2.TabIndex = 0;
			this.label2.Text = "My Address =";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 69);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(96, 16);
			this.label3.TabIndex = 2;
			this.label3.Text = "Polling Status";
			// 
			// txtPollingSts
			// 
			this.txtPollingSts.Location = new System.Drawing.Point(8, 88);
			this.txtPollingSts.Name = "txtPollingSts";
			this.txtPollingSts.Size = new System.Drawing.Size(476, 22);
			this.txtPollingSts.TabIndex = 3;
			this.txtPollingSts.Text = "";
			// 
			// btnPollDel
			// 
			this.btnPollDel.Location = new System.Drawing.Point(181, 124);
			this.btnPollDel.Name = "btnPollDel";
			this.btnPollDel.Size = new System.Drawing.Size(88, 24);
			this.btnPollDel.TabIndex = 4;
			this.btnPollDel.Text = "Del &Status";
			this.btnPollDel.Click += new System.EventHandler(this.btnPollDel_Click);
			// 
			// btnRetDel
			// 
			this.btnRetDel.Location = new System.Drawing.Point(274, 124);
			this.btnRetDel.Name = "btnRetDel";
			this.btnRetDel.Size = new System.Drawing.Size(88, 24);
			this.btnRetDel.TabIndex = 5;
			this.btnRetDel.Text = "Del &Return";
			this.btnRetDel.Click += new System.EventHandler(this.btnRetDel_Click);
			// 
			// btnPolling
			// 
			this.btnPolling.Enabled = false;
			this.btnPolling.Location = new System.Drawing.Point(380, 118);
			this.btnPolling.Name = "btnPolling";
			this.btnPolling.Size = new System.Drawing.Size(104, 36);
			this.btnPolling.TabIndex = 6;
			this.btnPolling.Text = "&Polling";
			this.btnPolling.Click += new System.EventHandler(this.btnPolling_Click);
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 144);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(96, 16);
			this.label4.TabIndex = 7;
			this.label4.Text = "Return value";
			// 
			// txtRet
			// 
			this.txtRet.Location = new System.Drawing.Point(8, 163);
			this.txtRet.Name = "txtRet";
			this.txtRet.Size = new System.Drawing.Size(476, 22);
			this.txtRet.TabIndex = 8;
			this.txtRet.Text = "";
			// 
			// btnInit
			// 
			this.btnInit.Location = new System.Drawing.Point(181, 192);
			this.btnInit.Name = "btnInit";
			this.btnInit.Size = new System.Drawing.Size(144, 24);
			this.btnInit.TabIndex = 9;
			this.btnInit.Text = "&Initialize";
			this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
			// 
			// btnExt
			// 
			this.btnExt.Location = new System.Drawing.Point(340, 192);
			this.btnExt.Name = "btnExt";
			this.btnExt.Size = new System.Drawing.Size(144, 24);
			this.btnExt.TabIndex = 10;
			this.btnExt.Text = "E&xit";
			this.btnExt.Click += new System.EventHandler(this.btnExt_Click);
			// 
			// Polling
			// 
			this.ClientSize = new System.Drawing.Size(492, 224);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnExt,
																		  this.btnInit,
																		  this.txtRet,
																		  this.label4,
																		  this.btnPolling,
																		  this.btnRetDel,
																		  this.btnPollDel,
																		  this.txtPollingSts,
																		  this.label3,
																		  this.groupBox2,
																		  this.groupBox1});
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Polling";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Polling (Serial Poll)";
			this.Load += new System.EventHandler(this.Polling_Load);
			this.Closed += new System.EventHandler(this.Polling_Closed);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Polling());
		}

		private void Polling_Load(object sender, System.EventArgs e)
		{
			// ****************************************************** Setting ***
			cboYrAdrBox.SelectedIndex = 1;
			Yradr = Convert.ToUInt32(cboYrAdrBox.Text);
			// ******************************************************************

			// Specify Pinned handle to Cmd
			if (gChCmd.IsAllocated == false) {
				gChCmd = GCHandle.Alloc(SubFunc.Cmd, GCHandleType.Pinned);
			}
			// Specify Pinned handle to Pstb
			if (gChPstb.IsAllocated == false) {
				gChPstb = GCHandle.Alloc(Pstb, GCHandleType.Pinned);
			}
			// Get address object by Pinned handle
			SubFunc.pCmd = gChCmd.AddrOfPinnedObject();
			pPstb = gChPstb.AddrOfPinnedObject();
		}

		private void Polling_Closed(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();
			// GCHandle free
			if (gChCmd.IsAllocated == true) {
				gChCmd.Free();
			}
			if (gChPstb.IsAllocated == true) {
				gChPstb.Free();
			}
		}

		//////////////////////////////////////////// Initialize ///
		private void btnInit_Click(object sender, System.EventArgs e)
		{
			string	DelMyadr = new string(' ', 16);

			btnPolling.Enabled = true;
			SubFunc.GpibInit(out csBuf);
			gpib.Boardsts(0x08, out Myadr);
			DelMyadr = string.Format("{0:d}", Myadr);
			Ret = (uint)cboYrAdrBox.FindStringExact(DelMyadr, -1);
			if (Ret >= 0)
			{
				cboYrAdrBox.Items.Remove(Ret.ToString());
				if (Myadr == Yradr)
				{
					cboYrAdrBox.SelectedIndex = 0;
				}
			}
			txtRet.Text = csBuf;
			txtMyAdr.Text = Myadr.ToString();

			Yradr = Convert.ToUInt32(cboYrAdrBox.Text);
		}

		/////////////////////////////////////////////// Polling ///
		private void btnPolling_Click(object sender, System.EventArgs e)
		{
			txtRet.Text = "Polling...";
			txtRet.Refresh();
			csBuf = "GpPoll";
			for (Count = 0; Count < 16; Count++)
			{
				Pstb[Count] = 0x00;
			}
			SubFunc.Cmd[0] = 1;
			SubFunc.Cmd[1] = Yradr;

			Ret = gpib.Poll(SubFunc.pCmd, pPstb);
			if ((Ret & 0xFF) == 0)
			{
				PollBuf = string.Format("[SRQ] is found. Talker = [ {0:x} ] / StatusByte = [ {1:x} ]H",SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]]);
			}
			else if ((Ret & 0xFF) == 1)
			{
				PollBuf = string.Format("[SRQ]&[EOI] is found. Talker = [{0:x}] / StatusByte = [ {1:x} ]H",SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]]);
			}
			else if ((Ret & 0xFF) == 128)
			{
				PollBuf = string.Format("[SRQ] was not found out.");
			}
			SubFunc.CheckRet("GpPoll", Ret, out csBuf);			// Not 0,1,128

			txtPollingSts.Text = PollBuf;
			txtRet.Text = csBuf;
		}

		/////////////////////////////////////////// Delete Poll ///
		private void btnPollDel_Click(object sender, System.EventArgs e)
		{
			txtPollingSts.Text = "";
		}

		///////////////////////////////////////// Delete Return ///
		private void btnRetDel_Click(object sender, System.EventArgs e)
		{
			txtRet.Text = "";
		}

		//////////////////////////////////////// Address of device Change ///
		private void cboYrAdrBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Yradr = Convert.ToUInt32(cboYrAdrBox.Text);
		}

		////////////////////////////////////////////////// Exit ///
		private void btnExt_Click(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();
			this.Close();
		}
	}
}
