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>
	/// Form1 ̊Tv̐łB
	/// </summary>
	public class Polling : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		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.Button btnInit;
		private System.Windows.Forms.Button btnPolling;
		private System.Windows.Forms.Button btnExt;
		private System.Windows.Forms.Button btnPollDel;
		private System.Windows.Forms.Button btnRetDel;
		private System.Windows.Forms.TextBox txtPollingSts;
		private System.Windows.Forms.TextBox txtRet;
		/// <summary>
		/// KvȃfUCiϐłB
		/// </summary>
		private System.ComponentModel.Container components = null;

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

		// ϐAz̐錾 ///////////////////////////////////////////////////////////
		string		szBuffer = new string(' ', 1024);	// eLXg̃obt@
		uint		Ret;								// ߂l
		int			Count;								// 
		uint		Myadr, Yradr;						// }CAhXA@AhX
		//uint		Delim, Eoi;							// f~^
		//uint		Srlen;								// obt@̒̕
		uint[]		Pstb = new uint[32];				// Xe[^XoCg̔z
		string		srBuffer = new string(' ', 10000);	// Mf[^̃obt@/read work
		string		csBuf;								// eLXg\
		string		PollBuf;							// |[OeLXg\
		GCHandle	gChCmd;								// CmdzpGCHandle
		GCHandle 	gChPstb; 							// PstbzpGCHandle
		IntPtr 		pPstb;								// PstbzpIntPtr
		///////////////////////////////////////////////////////////////////////////////

		public Polling()
		{
			//
			// Windows tH[ fUCi T|[gɕKvłB
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent ĂяǒɁARXgN^ R[hǉĂB
			//
		}

		/// <summary>
		/// gpĂ郊\[XɌ㏈s܂B
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// fUCi T|[gɕKvȃ\bhłB̃\bh̓e
		/// R[h GfB^ŕύXȂłB
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.cboYrAdrBox = new System.Windows.Forms.ComboBox();
			this.label2 = new System.Windows.Forms.Label();
			this.txtMyAdr = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.btnInit = new System.Windows.Forms.Button();
			this.btnPolling = new System.Windows.Forms.Button();
			this.btnExt = new System.Windows.Forms.Button();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.btnPollDel = new System.Windows.Forms.Button();
			this.btnRetDel = new System.Windows.Forms.Button();
			this.txtPollingSts = new System.Windows.Forms.TextBox();
			this.txtRet = new System.Windows.Forms.TextBox();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.cboYrAdrBox,
																					this.label2,
																					this.txtMyAdr,
																					this.label1});
			this.groupBox1.Location = new System.Drawing.Point(8, 5);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(544, 54);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "ݒ";
			// 
			// 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(476, 20);
			this.cboYrAdrBox.Name = "cboYrAdrBox";
			this.cboYrAdrBox.Size = new System.Drawing.Size(56, 24);
			this.cboYrAdrBox.TabIndex = 0;
			this.cboYrAdrBox.SelectedIndexChanged += new System.EventHandler(this.cboYrAdrBox_SelectedIndexChanged);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(201, 24);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(272, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "|[O鑊@AhX =";
			// 
			// txtMyAdr
			// 
			this.txtMyAdr.Location = new System.Drawing.Point(134, 21);
			this.txtMyAdr.Name = "txtMyAdr";
			this.txtMyAdr.ReadOnly = true;
			this.txtMyAdr.Size = new System.Drawing.Size(40, 23);
			this.txtMyAdr.TabIndex = 1;
			this.txtMyAdr.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(6, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(128, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "}CAhX =";
			// 
			// btnInit
			// 
			this.btnInit.Location = new System.Drawing.Point(8, 184);
			this.btnInit.Name = "btnInit";
			this.btnInit.Size = new System.Drawing.Size(129, 32);
			this.btnInit.TabIndex = 1;
			this.btnInit.Text = "(&I)";
			this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
			// 
			// btnPolling
			// 
			this.btnPolling.Location = new System.Drawing.Point(280, 184);
			this.btnPolling.Name = "btnPolling";
			this.btnPolling.Size = new System.Drawing.Size(129, 32);
			this.btnPolling.TabIndex = 3;
			this.btnPolling.Text = "|[O(&P)";
			this.btnPolling.Click += new System.EventHandler(this.btnPolling_Click);
			// 
			// btnExt
			// 
			this.btnExt.Location = new System.Drawing.Point(416, 184);
			this.btnExt.Name = "btnExt";
			this.btnExt.Size = new System.Drawing.Size(129, 32);
			this.btnExt.TabIndex = 4;
			this.btnExt.Text = "I(&X)";
			this.btnExt.Click += new System.EventHandler(this.btnExt_Click);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 74);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(176, 16);
			this.label3.TabIndex = 5;
			this.label3.Text = "|[OXe[^X";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 133);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(64, 16);
			this.label4.TabIndex = 6;
			this.label4.Text = "߂l";
			// 
			// btnPollDel
			// 
			this.btnPollDel.Location = new System.Drawing.Point(392, 64);
			this.btnPollDel.Name = "btnPollDel";
			this.btnPollDel.Size = new System.Drawing.Size(160, 26);
			this.btnPollDel.TabIndex = 7;
			this.btnPollDel.Text = "Xe[^X(&E)";
			this.btnPollDel.Click += new System.EventHandler(this.btnPollDel_Click);
			// 
			// btnRetDel
			// 
			this.btnRetDel.Location = new System.Drawing.Point(392, 123);
			this.btnRetDel.Name = "btnRetDel";
			this.btnRetDel.Size = new System.Drawing.Size(160, 26);
			this.btnRetDel.TabIndex = 8;
			this.btnRetDel.Text = "߂l(&R)";
			this.btnRetDel.Click += new System.EventHandler(this.btnRetDel_Click);
			// 
			// txtPollingSts
			// 
			this.txtPollingSts.Location = new System.Drawing.Point(8, 95);
			this.txtPollingSts.Name = "txtPollingSts";
			this.txtPollingSts.Size = new System.Drawing.Size(544, 23);
			this.txtPollingSts.TabIndex = 9;
			this.txtPollingSts.Text = "";
			// 
			// txtRet
			// 
			this.txtRet.Location = new System.Drawing.Point(8, 154);
			this.txtRet.Name = "txtRet";
			this.txtRet.Size = new System.Drawing.Size(544, 23);
			this.txtRet.TabIndex = 10;
			this.txtRet.Text = "";
			// 
			// Polling
			// 
			this.ClientSize = new System.Drawing.Size(562, 224);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.txtRet,
																		  this.txtPollingSts,
																		  this.btnRetDel,
																		  this.btnPollDel,
																		  this.label4,
																		  this.label3,
																		  this.btnExt,
																		  this.btnPolling,
																		  this.btnInit,
																		  this.groupBox1});
			this.Font = new System.Drawing.Font("lr SVbN", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			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 = "|[O";
			this.Load += new System.EventHandler(this.Polling_Load);
			this.Closed += new System.EventHandler(this.Polling_Closed);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// AvP[ṼC Gg |CgłB
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Polling());
		}

		private void Polling_Load(object sender, System.EventArgs e)
		{
			// [ݒ]
			txtMyAdr.Text = "0";
			cboYrAdrBox.SelectedIndex = 1;						// @AhX̏l

			// PinnednhCmdɕz
			if (gChCmd.IsAllocated == false) {
				gChCmd = GCHandle.Alloc(SubFunc.Cmd, GCHandleType.Pinned);
			}
			// PinnednhPstbɕz
			if (gChPstb.IsAllocated == false) {
				gChPstb = GCHandle.Alloc(Pstb, GCHandleType.Pinned);
			}
			// PinnednhŃIuWFNg̃AhX擾
			SubFunc.pCmd = gChCmd.AddrOfPinnedObject();
			pPstb = gChPstb.AddrOfPinnedObject();
		}

		private void Polling_Closed(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();

			// GCHandlẻ
			if (gChCmd.IsAllocated == true) {
				gChCmd.Free();
			}
			if (gChPstb.IsAllocated == true) {
				gChPstb.Free();
			}
		}

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

			Yradr = Convert.ToUInt32(cboYrAdrBox.Text);			// @̃AhX擾܂B
	
			SubFunc.GpibInit(out csBuf);						// GpibInit֐̌ĂяoĂ܂B(SubClass.h)
			gpib.Boardsts(0x08, out Myadr);						// }CAhX擾܂B
			DelMyadr = string.Format("{0:d}", Myadr);			// 𕶎ɕϊ
			Ret = (uint)cboYrAdrBox.FindStringExact(DelMyadr, -1);
			if (Ret >= 0)
			{													// vCfbNXΎs
				cboYrAdrBox.Items.Remove(Ret.ToString());		// }CAhXI{bNX폜B
				if (Myadr == Yradr)
				{
					cboYrAdrBox.SelectedIndex = 0;
				}
			}
			txtRet.Text = csBuf;								// _CAOɕ\
			txtMyAdr.Text = Myadr.ToString();					// }CAhX̕\

			Yradr = Convert.ToUInt32(cboYrAdrBox.Text);			// @̃AhX擾܂B
		}

		// [|[O] ///////////////////////////////////////////////////////////////
		private void btnPolling_Click(object sender, System.EventArgs e)
		{
			txtRet.Text = "|[OłDDD";
			txtRet.Refresh();									// \`悳ȂɎgp܂

			csBuf = "GpPoll";
			for (Count = 0; Count < 16; Count++)
			{
				Pstb[Count] = 0x00;								// z̓eNA܂B
			}
			SubFunc.Cmd[0] = 1;									// bZ[W
			SubFunc.Cmd[1] = Yradr;								// @AhX
			Ret = gpib.Poll(SubFunc.pCmd, pPstb);				// VA|[s܂B
			if ((Ret & 0xFF) == 0)								// [SRQ] ̂݊mF(0xFFŃ}XN)
			{
				PollBuf = string.Format("[SRQ]mF.g[J=[ {0:x} ]/Xe[^XoCg=[ {1:x} ]H",SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]]);
			}
			else if ((Ret & 0xFF) == 1)							// [SRQ] [EOI]mF
			{
				PollBuf = string.Format("[SRQ]&[EOI]mF.g[J=[{0:x}]/Xe[^XoCg=[ {1:x} ]H",SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]]);
			}
			else if ((Ret & 0xFF) == 128)						// mFȂ
			{
				PollBuf = string.Format("[SRQ] mFł܂łB");
			}
			SubFunc.CheckRet("GpPoll", Ret, out csBuf);			// 0,1,128 ȊȌꍇ

			txtPollingSts.Text = PollBuf;						// |[Ǒʂ\
			txtRet.Text = csBuf;								// ߂l\
		}

		// [I[`] /////////////////////////////////////////////////////////
		private void btnExt_Click(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();
			this.Close();										// _CAO܂B(vOI)
		}

		//  [|[Ȍ] ////////////////////////////////////////////////////
		private void btnPollDel_Click(object sender, System.EventArgs e)
		{
			txtPollingSts.Text = "";
		}

		// [߂l̕\] ///////////////////////////////////////////////////////
		private void btnRetDel_Click(object sender, System.EventArgs e)
		{
			txtRet.Text = "";
		}

		// [@AhXύX] /////////////////////////////////////////////////////////
		private void cboYrAdrBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Yradr = Convert.ToUInt32(cboYrAdrBox.Text);			// @̃AhX擾܂B
		}
	}
}
