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 Parallel
{
	/// <summary>
	/// Form1 ̊Tv̐łB
	/// </summary>
	public class Parallel : 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.GroupBox groupBox2;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Button btnInit;
		private System.Windows.Forms.Button btnExt;
		private System.Windows.Forms.TextBox txtPollSts;
		private System.Windows.Forms.TextBox txtPollStart;
		private System.Windows.Forms.TextBox txtPollSet;
		private System.Windows.Forms.Button btnPollDel;
		private System.Windows.Forms.TextBox txtRet;
		private System.Windows.Forms.ComboBox cboYrAdrBox1;
		private System.Windows.Forms.ComboBox cboYrAdrBox2;
		private System.Windows.Forms.Button btnPolling;
		/// <summary>
		/// KvȃfUCiϐłB
		/// </summary>
		private System.ComponentModel.Container components = null;

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

		// ϐ̌^ݒ /////////////////////////////////////////////////////////////
		string		szBuffer = new string(' ', 256);	// eLXg̃obt@
		uint		Ret;								// ߂l
		int			Count,Stppu;						// 񐔤PPUݒ
		uint		Madr;								// }CAhX
		uint		Yradr1, Yradr2;						// ^[QbgPA^[QbgQ
		//uint		Delim, Eoi;							// f~^
		uint		PDataLine;							// p|[߂l
		uint[]		Cmd2 = new uint[32];				// pp(2ڗp)
		uint[]		Pstb = new uint[32];				// // Xe[^XoCg̔z(Target1)
		uint[]		Pstb2 = new uint[32];				// Xe[^XoCg̔z(Target2)
		string		csBuf;								// eLXg\
		GCHandle	gChCmd;								// CmdzpGCHandle
		GCHandle 	gChPstb; 							// PstbzpGCHandle
		IntPtr 		pPstb;								// PstbzpIntPtr
		GCHandle	gChCmd2;							// gChCmd2zpGCHandle
		GCHandle 	gChPstb2; 							// gChPstb2zpGCHandle
		IntPtr 		pPstb2;								// pPstb2zpIntPtr
		IntPtr 		pCmd2;								// pCmd2zpIntPtr


		public Parallel()
		{
			//
			// 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.btnInit = new System.Windows.Forms.Button();
			this.btnExt = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.txtPollSts = new System.Windows.Forms.TextBox();
			this.txtPollStart = new System.Windows.Forms.TextBox();
			this.txtPollSet = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.btnPollDel = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.txtRet = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.cboYrAdrBox1 = new System.Windows.Forms.ComboBox();
			this.label5 = new System.Windows.Forms.Label();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.cboYrAdrBox2 = new System.Windows.Forms.ComboBox();
			this.label6 = new System.Windows.Forms.Label();
			this.btnPolling = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnInit
			// 
			this.btnInit.Location = new System.Drawing.Point(446, 17);
			this.btnInit.Name = "btnInit";
			this.btnInit.Size = new System.Drawing.Size(96, 32);
			this.btnInit.TabIndex = 0;
			this.btnInit.Text = "(&I)";
			this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
			// 
			// btnExt
			// 
			this.btnExt.Location = new System.Drawing.Point(446, 160);
			this.btnExt.Name = "btnExt";
			this.btnExt.Size = new System.Drawing.Size(96, 32);
			this.btnExt.TabIndex = 2;
			this.btnExt.Text = "I(&X)";
			this.btnExt.Click += new System.EventHandler(this.btnExt_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtPollSts,
																					this.txtPollStart,
																					this.txtPollSet,
																					this.label3,
																					this.label2,
																					this.btnPollDel,
																					this.label1});
			this.groupBox1.Location = new System.Drawing.Point(10, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(428, 184);
			this.groupBox1.TabIndex = 3;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "p|[̏ڍ(߂l)";
			// 
			// txtPollSts
			// 
			this.txtPollSts.Location = new System.Drawing.Point(8, 152);
			this.txtPollSts.Name = "txtPollSts";
			this.txtPollSts.Size = new System.Drawing.Size(408, 23);
			this.txtPollSts.TabIndex = 6;
			this.txtPollSts.Text = "";
			// 
			// txtPollStart
			// 
			this.txtPollStart.Location = new System.Drawing.Point(8, 102);
			this.txtPollStart.Name = "txtPollStart";
			this.txtPollStart.Size = new System.Drawing.Size(408, 23);
			this.txtPollStart.TabIndex = 5;
			this.txtPollStart.Text = "";
			// 
			// txtPollSet
			// 
			this.txtPollSet.Location = new System.Drawing.Point(8, 52);
			this.txtPollSet.Name = "txtPollSet";
			this.txtPollSet.Size = new System.Drawing.Size(408, 23);
			this.txtPollSet.TabIndex = 4;
			this.txtPollSet.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 133);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(216, 16);
			this.label3.TabIndex = 3;
			this.label3.Text = "p|[Xe[^X";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 81);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(216, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "p|[s";
			// 
			// btnPollDel
			// 
			this.btnPollDel.Location = new System.Drawing.Point(336, 16);
			this.btnPollDel.Name = "btnPollDel";
			this.btnPollDel.Size = new System.Drawing.Size(80, 26);
			this.btnPollDel.TabIndex = 1;
			this.btnPollDel.Text = "(&D)";
			this.btnPollDel.Click += new System.EventHandler(this.btnPollDel_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 32);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(216, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "p|[ݒ";
			// 
			// txtRet
			// 
			this.txtRet.Location = new System.Drawing.Point(70, 200);
			this.txtRet.Name = "txtRet";
			this.txtRet.Size = new System.Drawing.Size(368, 23);
			this.txtRet.TabIndex = 7;
			this.txtRet.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(10, 200);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(60, 16);
			this.label4.TabIndex = 8;
			this.label4.Text = "߂l";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.cboYrAdrBox1,
																					this.label5});
			this.groupBox2.Location = new System.Drawing.Point(10, 232);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(190, 52);
			this.groupBox2.TabIndex = 9;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "^[QbgP";
			// 
			// cboYrAdrBox1
			// 
			this.cboYrAdrBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboYrAdrBox1.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.cboYrAdrBox1.Location = new System.Drawing.Point(128, 20);
			this.cboYrAdrBox1.Name = "cboYrAdrBox1";
			this.cboYrAdrBox1.Size = new System.Drawing.Size(52, 24);
			this.cboYrAdrBox1.TabIndex = 10;
			this.cboYrAdrBox1.SelectedIndexChanged += new System.EventHandler(this.cboYrAdrBox1_SelectedIndexChanged);
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(7, 24);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(128, 16);
			this.label5.TabIndex = 9;
			this.label5.Text = "@AhX =";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.cboYrAdrBox2,
																					this.label6});
			this.groupBox3.Location = new System.Drawing.Point(208, 232);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(190, 52);
			this.groupBox3.TabIndex = 10;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "^[QbgQ";
			// 
			// cboYrAdrBox2
			// 
			this.cboYrAdrBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboYrAdrBox2.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.cboYrAdrBox2.Location = new System.Drawing.Point(128, 20);
			this.cboYrAdrBox2.Name = "cboYrAdrBox2";
			this.cboYrAdrBox2.Size = new System.Drawing.Size(52, 24);
			this.cboYrAdrBox2.TabIndex = 10;
			this.cboYrAdrBox2.SelectedIndexChanged += new System.EventHandler(this.cboYrAdrBox2_SelectedIndexChanged);
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(8, 24);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(128, 16);
			this.label6.TabIndex = 9;
			this.label6.Text = "@AhX =";
			// 
			// btnPolling
			// 
			this.btnPolling.Location = new System.Drawing.Point(406, 248);
			this.btnPolling.Name = "btnPolling";
			this.btnPolling.Size = new System.Drawing.Size(136, 32);
			this.btnPolling.TabIndex = 11;
			this.btnPolling.Text = "|[O(&P)";
			this.btnPolling.Click += new System.EventHandler(this.btnPolling_Click);
			// 
			// Parallel
			// 
			this.ClientSize = new System.Drawing.Size(558, 292);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnPolling,
																		  this.groupBox3,
																		  this.groupBox2,
																		  this.label4,
																		  this.txtRet,
																		  this.groupBox1,
																		  this.btnExt,
																		  this.btnInit});
			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 = "Parallel";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "p|[";
			this.Load += new System.EventHandler(this.Parallel_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void Parallel_Load(object sender, System.EventArgs e)
		{
			// [ݒ]
			cboYrAdrBox1.SelectedIndex = 0;
			cboYrAdrBox2.SelectedIndex = 1;

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

		private void Parallel_Closed(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();									// GpibExit֐R[Ă܂B(SubClass.h)

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

		// [[`] ///////////////////////////////////////////////////////////
		private void btnInit_Click(object sender, System.EventArgs e)
		{
			string	DelMadr = new string(' ', 16);
	
			SubFunc.GpibInit(out csBuf);						// Init֐̌ĂяoĂ܂B
			gpib.Boardsts(0x08, out Madr);						// }CAhX擾܂B
			DelMadr = string.Format("{0:d}", Madr);				// 𕶎ɕϊ
			Ret = (uint)cboYrAdrBox1.FindStringExact(DelMadr, -1);
			if (Ret >= 0 && Ret <= 30)
			{													// vCfbNXΎs
				cboYrAdrBox1.Items.Remove(Ret.ToString());		// Target1}CAhX폜B
				cboYrAdrBox1.SelectedIndex = 0;
				cboYrAdrBox2.Items.Remove(Ret.ToString());		// Target2}CAhX폜B
				cboYrAdrBox2.SelectedIndex = 1;
			}
			txtRet.Text = csBuf;								// \

			Yradr1 = Convert.ToUInt32(cboYrAdrBox1.Text);
			Yradr2 = Convert.ToUInt32(cboYrAdrBox2.Text);
		}

		// [|[Os] ///////////////////////////////////////////////////////////
		private void btnPolling_Click(object sender, System.EventArgs e)
		{
			string	csBuf1,csBuf2;

			for (Count = 0; Count < 16; Count++)
			{													// Pstb[],Pstb2[]̏
				Pstb[Count] = 0x00;								// PڃXe[^XoCg̔z
				Pstb2[Count] = 0x00;							// QڃXe[^XoCg̔z
			}
			txtPollSet.Text = "";
			txtPollStart.Text = "";
			txtPollSts.Text = "";

			SubFunc.Cmd[0] = 2;									// Xi̐
			SubFunc.Cmd[1] = Yradr1;							// 1ڂ̃Xi
			SubFunc.Cmd[2] = 0x69;								// CR[h 69H = DIO2
			SubFunc.Cmd[3] = Yradr2;							// 2ڂ̃Xi
			SubFunc.Cmd[4] = 0x6a;								// CR[h 6aH = DIO3
			Stppu = 1;											// PPUo͂s

			Ret = gpib.Stppoll(SubFunc.pCmd, (uint)Stppu);		// p|[̉o͗pC̊t
			SubFunc.CheckRet("GpStppoll", Ret, out csBuf);		// GpStppoll̖߂l`FbN
			txtPollSet.Text = csBuf;
			if ((Ret & 0xFF) != 0)
			{
				return;
			}
			Ret = gpib.Exppoll(out PDataLine);					// GpExppoll̖߂l`FbN
			SubFunc.CheckRet("GpExppoll", Ret, out csBuf);
			txtPollStart.Text = csBuf;
			if ((Ret & 0xFF) != 0)
			{
				return;
			}

			switch (PDataLine)
			{													// ԂĂ[PDataLine][SRQ]L𔻒
				case 2:											// Crbg[ 00000010(DIO2) ]̏ꍇ
					SubFunc.Cmd[0] = 1;
					SubFunc.Cmd[1] = Yradr1;
					Ret = gpib.Poll(SubFunc.pCmd, pPstb);
					SubFunc.CheckRet("GpPoll", Ret, out csBuf);	// GpPoll̖߂l`FbN
					if (((Ret & 0xFF) != 0) && ((Ret & 0xFF) != 1))
					{
						txtPollSts.Text = csBuf;
						return;
					}
					szBuffer = string.Format("g[J=[ {0:d} ]FXe[^XoCg=[ {1:x} ]H",SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]]);
					txtPollSts.Text = szBuffer;
					break;
				case 4:											// Crbg[ 00000100(DIO3) ]̏ꍇ
					SubFunc.Cmd[0] = 1;
					SubFunc.Cmd[1] = Yradr2;
					Ret = gpib.Poll(SubFunc.pCmd, pPstb);
					SubFunc.CheckRet("GpPoll", Ret, out csBuf);	// GpPoll̖߂l`FbN
					if (((Ret & 0xFF) != 0) && ((Ret & 0xFF) != 1))
					{
						txtPollSts.Text = csBuf;
						return;
					}
					szBuffer = string.Format("g[J=[ {0:d} ]FXe[^XoCg=[ {1:x} ]H",SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]]);
					txtPollSts.Text = szBuffer;
					break;
				case 6:											// Crbg[ 00000110(DIO2 | DIO3) ]̏ꍇ
					SubFunc.Cmd[0] = 1;
					SubFunc.Cmd[1] = Yradr1;
					Ret = gpib.Poll(SubFunc.pCmd, pPstb);		// Pڃ`FbN
					SubFunc.CheckRet("Target1", Ret, out csBuf1);
					if (((Ret & 0xFF) != 0) && ((Ret & 0xFF) != 1))
					{
						txtPollSts.Text = csBuf1;
						return;
					}

					Cmd2[0] = 1;
					Cmd2[1] = Yradr2;
					Ret = gpib.Poll(pCmd2, pPstb2);				// Qڃ`FbN
					SubFunc.CheckRet("Target2", Ret, out csBuf2);
					if (((Ret & 0xFF) != 0) && ((Ret & 0xFF) != 1))
					{
						txtPollSts.Text = csBuf2;
						return;
					}

					szBuffer = string.Format("g[J=[ {0:d} ]FXe[^XoCg=[ {1:x} ]H / g[J=[ {2:d} ]FXe[^XoCg=[ {3:x} ]H",
						SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]], Cmd2[Pstb2[0]], Pstb2[Pstb2[0]]);
					txtPollSts.Text = szBuffer;
					break;
				default:
					csBuf = "[SRQ] ͌܂łB";
					txtPollSts.Text = csBuf;
					break;
			}
		}

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

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

		// [^[QbgPύX] ///////////////////////////////////////////////////////
		private void cboYrAdrBox1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Yradr1 = Convert.ToUInt32(cboYrAdrBox1.Text);
		}

		// [^[QbgQύX] ///////////////////////////////////////////////////////
		private void cboYrAdrBox2_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Yradr2 = Convert.ToUInt32(cboYrAdrBox2.Text);
		}
	}
}
