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 MltLine
{
	/// <summary>
	/// Form1 ̊Tv̐łB
	/// </summary>
	public class MltLine : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.GroupBox groupBox5;
		private System.Windows.Forms.Button btnUNL;
		private System.Windows.Forms.Button btnUNT;
		private System.Windows.Forms.Button btnDCL;
		private System.Windows.Forms.Button btnLLO;
		private System.Windows.Forms.Button btnGET;
		private System.Windows.Forms.Button btnSDC;
		private System.Windows.Forms.Button btnGTL;
		private System.Windows.Forms.Button btnInit;
		private System.Windows.Forms.Button btnExt;
		private System.Windows.Forms.Button btnSend;
		private System.Windows.Forms.Button btnDel;
		private System.Windows.Forms.Button btnAllDel;
		private System.Windows.Forms.TextBox txtMsg;
		private System.Windows.Forms.TextBox txtRet;
		private System.Windows.Forms.ComboBox cboYrAdrBox;
		/// <summary>
		/// KvȃfUCiϐłB
		/// </summary>
		private System.ComponentModel.Container components = null;

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

		// ReLXg //////////////////////////////////////////////////////////
		const int	GP_GTL	=	0x01;
		const int	GP_SDC	=	0x04;
		const int	GP_PPC	=	0x05;
		const int	GP_GET	=	0x08;
		const int	GP_TCT	=	0x09;
		const int	GP_LLO	=	0x11;
		const int	GP_DCL	=	0x14;
		const int	GP_PPU	=	0x15;
		const int	GP_SPE	=	0x18;
		const int	GP_SPD	=	0x19;
		const int	GP_MLA	=	0x20;
		const int	GP_UNL	=	0x3F;
		const int	GP_MTA	=	0x40;
		const int	GP_UNT	=	0x5F;

		// ݒ /////////////////////////////////////////////////////////////
		uint		Myadr,Yradr;					// }CAhXA@AhX
		//uint		Delim,Eoi;						// f~^
		bool		bRet;							// AddComand֐BOOLl
		string		csBuf;							// ̃obt@
		int			CmdNum,CmdNumPos;				// R}h,R}ḧʒu
		//int		NumPos;							// 
		uint		Ret;							// ߂l
		int[]		CmdNumArray = new int[50];		//
		string		DelMyadr = new string(' ', 8);	// }CAhX(폜p)
		GCHandle	gChCmd;							// CmdzpGCHandle
		/////////////////////////////////////////////////////////////////////////////

		public MltLine()
		{
			//
			// 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.groupBox3 = new System.Windows.Forms.GroupBox();
			this.btnUNL = new System.Windows.Forms.Button();
			this.btnUNT = new System.Windows.Forms.Button();
			this.btnDCL = new System.Windows.Forms.Button();
			this.btnLLO = new System.Windows.Forms.Button();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.btnGET = new System.Windows.Forms.Button();
			this.btnSDC = new System.Windows.Forms.Button();
			this.btnGTL = new System.Windows.Forms.Button();
			this.cboYrAdrBox = new System.Windows.Forms.ComboBox();
			this.label1 = new System.Windows.Forms.Label();
			this.btnInit = new System.Windows.Forms.Button();
			this.btnExt = new System.Windows.Forms.Button();
			this.groupBox4 = new System.Windows.Forms.GroupBox();
			this.btnSend = new System.Windows.Forms.Button();
			this.btnDel = new System.Windows.Forms.Button();
			this.btnAllDel = new System.Windows.Forms.Button();
			this.txtMsg = new System.Windows.Forms.TextBox();
			this.groupBox5 = new System.Windows.Forms.GroupBox();
			this.txtRet = new System.Windows.Forms.TextBox();
			this.groupBox1.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox4.SuspendLayout();
			this.groupBox5.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.groupBox3,
																					this.groupBox2});
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(403, 123);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "}`CbZ[W쐬";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnUNL,
																					this.btnUNT,
																					this.btnDCL,
																					this.btnLLO});
			this.groupBox3.Location = new System.Drawing.Point(236, 24);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(154, 88);
			this.groupBox3.TabIndex = 1;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "R}ĥ";
			// 
			// btnUNL
			// 
			this.btnUNL.Location = new System.Drawing.Point(80, 56);
			this.btnUNL.Name = "btnUNL";
			this.btnUNL.Size = new System.Drawing.Size(64, 24);
			this.btnUNL.TabIndex = 6;
			this.btnUNL.Text = "U&NL";
			this.btnUNL.Click += new System.EventHandler(this.btnUNL_Click);
			// 
			// btnUNT
			// 
			this.btnUNT.Location = new System.Drawing.Point(8, 56);
			this.btnUNT.Name = "btnUNT";
			this.btnUNT.Size = new System.Drawing.Size(64, 24);
			this.btnUNT.TabIndex = 5;
			this.btnUNT.Text = "&UNT";
			this.btnUNT.Click += new System.EventHandler(this.btnUNT_Click);
			// 
			// btnDCL
			// 
			this.btnDCL.Location = new System.Drawing.Point(80, 25);
			this.btnDCL.Name = "btnDCL";
			this.btnDCL.Size = new System.Drawing.Size(64, 24);
			this.btnDCL.TabIndex = 4;
			this.btnDCL.Text = "&DCL";
			this.btnDCL.Click += new System.EventHandler(this.btnDCL_Click);
			// 
			// btnLLO
			// 
			this.btnLLO.Location = new System.Drawing.Point(8, 25);
			this.btnLLO.Name = "btnLLO";
			this.btnLLO.Size = new System.Drawing.Size(64, 24);
			this.btnLLO.TabIndex = 3;
			this.btnLLO.Text = "&LLO";
			this.btnLLO.Click += new System.EventHandler(this.btnLLO_Click);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnGET,
																					this.btnSDC,
																					this.btnGTL,
																					this.cboYrAdrBox,
																					this.label1});
			this.groupBox2.Location = new System.Drawing.Point(12, 24);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(226, 88);
			this.groupBox2.TabIndex = 0;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "@AhXKv";
			// 
			// btnGET
			// 
			this.btnGET.Location = new System.Drawing.Point(152, 56);
			this.btnGET.Name = "btnGET";
			this.btnGET.Size = new System.Drawing.Size(64, 24);
			this.btnGET.TabIndex = 4;
			this.btnGET.Text = "GE&T";
			this.btnGET.Click += new System.EventHandler(this.btnGET_Click);
			// 
			// btnSDC
			// 
			this.btnSDC.Location = new System.Drawing.Point(80, 56);
			this.btnSDC.Name = "btnSDC";
			this.btnSDC.Size = new System.Drawing.Size(64, 24);
			this.btnSDC.TabIndex = 3;
			this.btnSDC.Text = "SD&C";
			this.btnSDC.Click += new System.EventHandler(this.btnSDC_Click);
			// 
			// btnGTL
			// 
			this.btnGTL.Location = new System.Drawing.Point(8, 56);
			this.btnGTL.Name = "btnGTL";
			this.btnGTL.Size = new System.Drawing.Size(64, 24);
			this.btnGTL.TabIndex = 2;
			this.btnGTL.Text = "&GTL";
			this.btnGTL.Click += new System.EventHandler(this.btnGTL_Click);
			// 
			// 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(168, 25);
			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(8, 27);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(160, 21);
			this.label1.TabIndex = 0;
			this.label1.Text = "@AhX =";
			// 
			// btnInit
			// 
			this.btnInit.Location = new System.Drawing.Point(444, 14);
			this.btnInit.Name = "btnInit";
			this.btnInit.Size = new System.Drawing.Size(88, 24);
			this.btnInit.TabIndex = 5;
			this.btnInit.Text = "(&I)";
			this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
			// 
			// btnExt
			// 
			this.btnExt.Location = new System.Drawing.Point(444, 283);
			this.btnExt.Name = "btnExt";
			this.btnExt.Size = new System.Drawing.Size(88, 32);
			this.btnExt.TabIndex = 7;
			this.btnExt.Text = "I(&X)";
			this.btnExt.Click += new System.EventHandler(this.btnExt_Click);
			// 
			// groupBox4
			// 
			this.groupBox4.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnSend,
																					this.btnDel,
																					this.btnAllDel,
																					this.txtMsg});
			this.groupBox4.Location = new System.Drawing.Point(8, 136);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(524, 122);
			this.groupBox4.TabIndex = 8;
			this.groupBox4.TabStop = false;
			this.groupBox4.Text = "o͂}`CbZ[W(MAX=30)";
			// 
			// btnSend
			// 
			this.btnSend.Location = new System.Drawing.Point(432, 32);
			this.btnSend.Name = "btnSend";
			this.btnSend.Size = new System.Drawing.Size(80, 80);
			this.btnSend.TabIndex = 10;
			this.btnSend.Text = "M(&S)";
			this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			// 
			// btnDel
			// 
			this.btnDel.Location = new System.Drawing.Point(329, 80);
			this.btnDel.Name = "btnDel";
			this.btnDel.Size = new System.Drawing.Size(96, 32);
			this.btnDel.TabIndex = 9;
			this.btnDel.Text = "폜(&B)";
			this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
			// 
			// btnAllDel
			// 
			this.btnAllDel.Location = new System.Drawing.Point(329, 32);
			this.btnAllDel.Name = "btnAllDel";
			this.btnAllDel.Size = new System.Drawing.Size(96, 32);
			this.btnAllDel.TabIndex = 8;
			this.btnAllDel.Text = "S폜(&R)";
			this.btnAllDel.Click += new System.EventHandler(this.btnAllDel_Click);
			// 
			// txtMsg
			// 
			this.txtMsg.Location = new System.Drawing.Point(12, 32);
			this.txtMsg.Multiline = true;
			this.txtMsg.Name = "txtMsg";
			this.txtMsg.Size = new System.Drawing.Size(310, 80);
			this.txtMsg.TabIndex = 0;
			this.txtMsg.Text = "";
			// 
			// groupBox5
			// 
			this.groupBox5.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtRet});
			this.groupBox5.Location = new System.Drawing.Point(8, 263);
			this.groupBox5.Name = "groupBox5";
			this.groupBox5.Size = new System.Drawing.Size(403, 56);
			this.groupBox5.TabIndex = 9;
			this.groupBox5.TabStop = false;
			this.groupBox5.Text = "߂l";
			// 
			// txtRet
			// 
			this.txtRet.Location = new System.Drawing.Point(12, 24);
			this.txtRet.Name = "txtRet";
			this.txtRet.Size = new System.Drawing.Size(380, 23);
			this.txtRet.TabIndex = 0;
			this.txtRet.Text = "";
			// 
			// MltLine
			// 
			this.ClientSize = new System.Drawing.Size(542, 328);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox5,
																		  this.groupBox4,
																		  this.btnExt,
																		  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 = "MltLine";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "}`C bZ[W";
			this.Load += new System.EventHandler(this.MltLine_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox4.ResumeLayout(false);
			this.groupBox5.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void MltLine_Load(object sender, System.EventArgs e)
		{
			// [ݒ]
			CmdNum = 1;
			CmdNumPos = 0;
			cboYrAdrBox.Text = "0";

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

		private void MltLine_Closed(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();										// [SubClass.h] ̒̊֐R[܂B

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

		// bZ[WXg̍쐬 /////////////////// [DispCommandList] ֐ //////////
		private string DispCommandList()
		{
			int		TxtRet;											// 16ibZ[Wp
			int		Count;											// JE^
			string	Text = "", TmpString;							// TmpString = 쐬MSGText = SĂMSG

			for (Count = 1; Count < CmdNum; Count++)
			{														// SĂMSG̍Ōɍ쐬MSGǉ
				TxtRet = (int)SubFunc.Cmd[Count];					// ԖڂCmdz
				TmpString = string.Format(" [{0:x}H]", TxtRet); 	// \p
				Text = Text + TmpString;							// VKMSGǉ
			}
			return	Text;
		}

		// bZ[W̔z쐬 //////////////////////// [AddCommand] ֐ //////////
		private bool AddCommand(uint CommandType, int CommandNum)
		{
			string	ErrMsg;											// G[bZ[Wp

			if (CmdNum > 30)
			{														// bZ[W30ȏ̎
				ErrMsg = "ȏݒł܂B";
				MessageBox.Show(ErrMsg);
				return false;
			}
			if (CommandNum == 2)
			{														// bZ[Wɑ@AhX
				SubFunc.Cmd[CmdNum] = (Yradr | 0x20);				// ̗p
				CmdNum = CmdNum + 1;
			}
			SubFunc.Cmd[CmdNum] = CommandType;						// ǂ̃bZ[WlœnB
			CmdNum = CmdNum + 1;

			return true;											// ÎƂ
		}

		// [[`] ///////////////////////////////////////////////////////////
		private void btnInit_Click(object sender, System.EventArgs e)
		{
			SubFunc.GpibInit(out csBuf);							// Init֐̌ĂяoĂ܂B
			gpib.Boardsts(0x08, out Myadr);							// }CAhX擾
			DelMyadr = string.Format("{0:d}", Myadr);				// 𕶎ɕϊ
			Ret = (uint)cboYrAdrBox.FindStringExact(DelMyadr, -1);	// }CAhX0-30̒ɂ邩
			if (Ret >= 0)
			{														// vCfbNXΎs
				cboYrAdrBox.Items.Remove(Ret.ToString());			// }CAhXI{bNX폜B
				if (Myadr == Yradr)
				{
					cboYrAdrBox.SelectedIndex = 0;
				}
			}
			txtRet.Text = csBuf;

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

		// [M[`] /////////////////////////////////////////////////////////////
		private void btnSend_Click(object sender, System.EventArgs e)
		{
			SubFunc.Cmd[0] = (uint)CmdNum - 1;

			if (CmdNum <= 1)
			{														// R}h [0] ̏ꍇ
				csBuf = "R}h͂ĂB";
			}
			else
			{
				Ret = gpib.Comand(SubFunc.pCmd);					// R}h()̑M
				SubFunc.CheckRet("GpComand", Ret, out csBuf);		// GpComand̖߂l`FbN
			}
			txtRet.Text = csBuf;									// ߂l\
		}

		// [ GTL ] ////////////////////////////////////////////////////////////////////
		private void btnGTL_Click(object sender, System.EventArgs e)
		{
			bRet = AddCommand(GP_GTL, 2);							// AddCommand(R}h, )
			if (bRet == true)
			{
				CmdNumArray[CmdNumPos] = 2;
				CmdNumPos = CmdNumPos + 1;
				txtRet.Text = "";
			}
			txtMsg.Text = DispCommandList();						// R}h̕\
		}

		// [ GET ] ////////////////////////////////////////////////////////////////////
		private void btnGET_Click(object sender, System.EventArgs e)
		{
			bRet = AddCommand(GP_GET, 2);							// AddComand(萔 )
			if (bRet == true)
			{
				CmdNumArray[CmdNumPos] = 2;
				CmdNumPos = CmdNumPos + 1;
				txtRet.Text = "";									// \̏
			}
			txtMsg.Text = DispCommandList();
		}

		// [ DCL ] ////////////////////////////////////////////////////////////////////
		private void btnDCL_Click(object sender, System.EventArgs e)
		{
			bRet = AddCommand(GP_DCL, 1);
			if (bRet == true)
			{
				CmdNumArray[CmdNumPos] = 1;
				CmdNumPos = CmdNumPos + 1;
				txtRet.Text = "";
			}
			txtMsg.Text = DispCommandList();
		}

		// [ LLO ] ////////////////////////////////////////////////////////////////////
		private void btnLLO_Click(object sender, System.EventArgs e)
		{
			bRet = AddCommand(GP_LLO, 1);
			if (bRet == true)
			{
				CmdNumArray[CmdNumPos] = 1;
				CmdNumPos = CmdNumPos + 1;
				txtRet.Text = "";
			}
			txtMsg.Text = DispCommandList();
		}

		// [ SDC ] ////////////////////////////////////////////////////////////////////
		private void btnSDC_Click(object sender, System.EventArgs e)
		{
			bRet = AddCommand(GP_SDC, 2);
			if (bRet == true)
			{
				CmdNumArray[CmdNumPos] = 2;
				CmdNumPos = CmdNumPos + 1;
				txtRet.Text = "";
			}
			txtMsg.Text = DispCommandList();
		}

		// [ UNL ] ////////////////////////////////////////////////////////////////////
		private void btnUNL_Click(object sender, System.EventArgs e)
		{
			bRet = AddCommand(GP_UNL, 1);
			if (bRet == true)
			{
				CmdNumArray[CmdNumPos] = 1;
				CmdNumPos = CmdNumPos + 1;
				txtRet.Text = "";
			}
			txtMsg.Text = DispCommandList();
		}

		// [ UNT ] ////////////////////////////////////////////////////////////////////
		private void btnUNT_Click(object sender, System.EventArgs e)
		{
			bRet = AddCommand(GP_UNT, 1);
			if (bRet == true)
			{
				CmdNumArray[CmdNumPos] = 1;
				CmdNumPos = CmdNumPos + 1;
				txtRet.Text = "";
			}
			txtMsg.Text = DispCommandList();
		}

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

		// [] /////////////////////////////////////////////////////////////////
		private void btnDel_Click(object sender, System.EventArgs e)
		{
			if (CmdNumPos == 0)
			{														// bZ[WȂꍇ
				txtRet.Text = "}`CbZ[W܂B";
			}
			else
			{
				if (CmdNumArray[CmdNumPos - 1] == 1)
				{
					SubFunc.Cmd[CmdNum] = 0;
					CmdNum = CmdNum - 1;
				} 
				else 
				{
					SubFunc.Cmd[CmdNum] = 0;
					SubFunc.Cmd[CmdNum - 1] = 0;
					CmdNum = CmdNum - 2;
				}
				CmdNumArray[CmdNumPos - 1] = 0;
				CmdNumPos = CmdNumPos - 1;
				txtRet.Text = "";
			}
			txtMsg.Text = DispCommandList();
		}

		// [SĎ] /////////////////////////////////////////////////////////////
		private void btnAllDel_Click(object sender, System.EventArgs e)
		{
			CmdNum = 1;
			CmdNumPos = 0;

			txtMsg.Text = DispCommandList();
		}

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