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

namespace Master
{
	/// <summary>
	/// Form1 ̊Tv̐łB
	/// </summary>
	public class Master : 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.GroupBox groupBox2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox txtMyAddress;
		private System.Windows.Forms.TextBox txtSendData;
		private System.Windows.Forms.TextBox txtReceiveData;
		private System.Windows.Forms.Button btnSend;
		private System.Windows.Forms.Button btnReceive;
		private System.Windows.Forms.Button btnDelData;
		private System.Windows.Forms.Button btnDelRet;
		private System.Windows.Forms.Button btnInit;
		private System.Windows.Forms.ComboBox cboDelimBox;
		private System.Windows.Forms.Button btnPolling;
		private System.Windows.Forms.Button btnGet;
		private System.Windows.Forms.Button btnExt;
		private System.Windows.Forms.ComboBox cboYrAdrBox;
		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(); 

		// ݒ ///////////////////////////////////////////////////////////////
		string			szBuffer = new string(' ', 1024);		// eLXg̃obt@
		uint			Ret,Count;								// ߂l
		uint			Myadr, Yradr, Mode;						// }CAhXA@AhXAX[u`FbN
		uint			Delim, Eoi;								// f~^
		uint			Srlen;									// f[^̒
		//uint			P_stb, P_eoi;							// Xe[^XoCgAEOĨ|C^
		uint[]			Pstb = new uint[32];					// Xe[^XoCg̔z
		string			srBuffer = new string(' ', 10000);		// Mf[^̃obt@
		string			csBuf;									// eLXg\
		StringBuilder	rvBuffer = new StringBuilder(10000);	// Mf[^̃obt@
		GCHandle		gChCmd;									// CmdzpGCHandle
		GCHandle 		gChPstb; 								// PstbzpGCHandle
		IntPtr 			pPstb;									// PstbzpIntPtr
		/////////////////////////////////////////////////////////////////////////////

		public Master()
		{
			//
			// 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.txtMyAddress = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.btnReceive = new System.Windows.Forms.Button();
			this.btnSend = new System.Windows.Forms.Button();
			this.txtReceiveData = new System.Windows.Forms.TextBox();
			this.txtSendData = new System.Windows.Forms.TextBox();
			this.btnDelData = new System.Windows.Forms.Button();
			this.btnDelRet = new System.Windows.Forms.Button();
			this.label3 = new System.Windows.Forms.Label();
			this.txtRet = new System.Windows.Forms.TextBox();
			this.btnInit = new System.Windows.Forms.Button();
			this.label4 = new System.Windows.Forms.Label();
			this.cboDelimBox = new System.Windows.Forms.ComboBox();
			this.btnPolling = new System.Windows.Forms.Button();
			this.btnGet = 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.label2,
																					this.txtMyAddress,
																					this.label1});
			this.groupBox1.Location = new System.Drawing.Point(12, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(440, 48);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			// 
			// 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(353, 17);
			this.cboYrAdrBox.Name = "cboYrAdrBox";
			this.cboYrAdrBox.Size = new System.Drawing.Size(56, 24);
			this.cboYrAdrBox.TabIndex = 3;
			this.cboYrAdrBox.SelectionChangeCommitted += new System.EventHandler(this.cboYrAdrBox_SelectionChangeCommitted);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(219, 20);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(124, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "@AhX =";
			// 
			// txtMyAddress
			// 
			this.txtMyAddress.Location = new System.Drawing.Point(146, 17);
			this.txtMyAddress.Name = "txtMyAddress";
			this.txtMyAddress.Size = new System.Drawing.Size(38, 23);
			this.txtMyAddress.TabIndex = 1;
			this.txtMyAddress.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 20);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(124, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "}CAhX =";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnReceive,
																					this.btnSend,
																					this.txtReceiveData,
																					this.txtSendData});
			this.groupBox2.Location = new System.Drawing.Point(12, 64);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(440, 96);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Mf[^";
			// 
			// btnReceive
			// 
			this.btnReceive.Location = new System.Drawing.Point(344, 53);
			this.btnReceive.Name = "btnReceive";
			this.btnReceive.Size = new System.Drawing.Size(80, 26);
			this.btnReceive.TabIndex = 3;
			this.btnReceive.Text = "M(&R)";
			this.btnReceive.Click += new System.EventHandler(this.btnReceive_Click);
			// 
			// btnSend
			// 
			this.btnSend.Location = new System.Drawing.Point(344, 21);
			this.btnSend.Name = "btnSend";
			this.btnSend.Size = new System.Drawing.Size(80, 26);
			this.btnSend.TabIndex = 2;
			this.btnSend.Text = "M(&S)";
			this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			// 
			// txtReceiveData
			// 
			this.txtReceiveData.Location = new System.Drawing.Point(10, 56);
			this.txtReceiveData.Name = "txtReceiveData";
			this.txtReceiveData.Size = new System.Drawing.Size(320, 23);
			this.txtReceiveData.TabIndex = 1;
			this.txtReceiveData.Text = "";
			// 
			// txtSendData
			// 
			this.txtSendData.Location = new System.Drawing.Point(10, 24);
			this.txtSendData.Name = "txtSendData";
			this.txtSendData.Size = new System.Drawing.Size(320, 23);
			this.txtSendData.TabIndex = 0;
			this.txtSendData.Text = "";
			// 
			// btnDelData
			// 
			this.btnDelData.Location = new System.Drawing.Point(204, 168);
			this.btnDelData.Name = "btnDelData";
			this.btnDelData.Size = new System.Drawing.Size(121, 26);
			this.btnDelData.TabIndex = 4;
			this.btnDelData.Text = "f[^(&D)";
			this.btnDelData.Click += new System.EventHandler(this.btnDelData_Click);
			// 
			// btnDelRet
			// 
			this.btnDelRet.Location = new System.Drawing.Point(331, 168);
			this.btnDelRet.Name = "btnDelRet";
			this.btnDelRet.Size = new System.Drawing.Size(121, 26);
			this.btnDelRet.TabIndex = 5;
			this.btnDelRet.Text = "߂l(&C)";
			this.btnDelRet.Click += new System.EventHandler(this.btnDelRet_Click);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 176);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(56, 16);
			this.label3.TabIndex = 6;
			this.label3.Text = "߂l";
			// 
			// txtRet
			// 
			this.txtRet.Location = new System.Drawing.Point(12, 200);
			this.txtRet.Name = "txtRet";
			this.txtRet.Size = new System.Drawing.Size(440, 23);
			this.txtRet.TabIndex = 7;
			this.txtRet.Text = "";
			// 
			// btnInit
			// 
			this.btnInit.Location = new System.Drawing.Point(460, 18);
			this.btnInit.Name = "btnInit";
			this.btnInit.Size = new System.Drawing.Size(129, 29);
			this.btnInit.TabIndex = 8;
			this.btnInit.Text = "(&I)";
			this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(460, 70);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(80, 24);
			this.label4.TabIndex = 9;
			this.label4.Text = "f~^";
			// 
			// cboDelimBox
			// 
			this.cboDelimBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboDelimBox.Items.AddRange(new object[] {
															 "gp",
															 "CR{LF",
															 "CR",
															 "LF"});
			this.cboDelimBox.Location = new System.Drawing.Point(460, 89);
			this.cboDelimBox.Name = "cboDelimBox";
			this.cboDelimBox.Size = new System.Drawing.Size(129, 24);
			this.cboDelimBox.TabIndex = 10;
			this.cboDelimBox.SelectionChangeCommitted += new System.EventHandler(this.cboDelimBox_SelectionChangeCommitted);
			// 
			// btnPolling
			// 
			this.btnPolling.Location = new System.Drawing.Point(460, 120);
			this.btnPolling.Name = "btnPolling";
			this.btnPolling.Size = new System.Drawing.Size(129, 29);
			this.btnPolling.TabIndex = 11;
			this.btnPolling.Text = "|[O(&P)";
			this.btnPolling.Click += new System.EventHandler(this.btnPolling_Click);
			// 
			// btnGet
			// 
			this.btnGet.Location = new System.Drawing.Point(460, 156);
			this.btnGet.Name = "btnGet";
			this.btnGet.Size = new System.Drawing.Size(129, 29);
			this.btnGet.TabIndex = 12;
			this.btnGet.Text = "GET(&G)";
			this.btnGet.Click += new System.EventHandler(this.btnGet_Click);
			// 
			// btnExt
			// 
			this.btnExt.Location = new System.Drawing.Point(460, 192);
			this.btnExt.Name = "btnExt";
			this.btnExt.Size = new System.Drawing.Size(129, 29);
			this.btnExt.TabIndex = 13;
			this.btnExt.Text = "I(&X)";
			this.btnExt.Click += new System.EventHandler(this.btnExt_Click);
			// 
			// Master
			// 
			this.ClientSize = new System.Drawing.Size(602, 238);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnExt,
																		  this.btnGet,
																		  this.btnPolling,
																		  this.cboDelimBox,
																		  this.label4,
																		  this.btnInit,
																		  this.txtRet,
																		  this.label3,
																		  this.btnDelRet,
																		  this.btnDelData,
																		  this.groupBox2,
																		  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 = "Master";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "}X^[h";
			this.Load += new System.EventHandler(this.Master_Load);
			this.Closed += new System.EventHandler(this.Master_Closed);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void Master_Load(object sender, System.EventArgs e)
		{
			txtMyAddress.Text = "0";
			cboYrAdrBox.SelectedIndex = 1;								// lݒ
			cboDelimBox.SelectedIndex = 1;								// ݒ
			// 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 Master_Closed(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();											// ExtGpib֐(Tu[`)R[Ă܂B
			// 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(' ', 8);
			SubFunc.GpibInit(out csBuf);								// Init֐(Tu[`)R[Ă܂B
			gpib.Boardsts(0x08, out Myadr);								// }CAhX擾
			gpib.Boardsts(0x0a, out Mode);								// X[uǂ`FbN
	
			if (Mode == 1)
			{															// X[ũ`FbN
				txtRet.Text = "̋@̓}X^ł͂܂B";
				return;
			}
			else
			{
				txtMyAddress.Text = Myadr.ToString();
				DelMyadr = Myadr.ToString();							// 𕶎ɕϊ
				Ret = (uint)cboYrAdrBox.FindStringExact(DelMyadr, -1);
				if (Ret >= 0)
				{														// vCfbNXΎs
					try
					{
						Yradr = Convert.ToUInt32(cboYrAdrBox.Text);
					}
					catch(System.FormatException)
					{
						Yradr = 0;
					}
					cboYrAdrBox.Items.Remove(DelMyadr);					// }CAhXI{bNX폜B
					
					if (Myadr == Yradr)
					{
						cboYrAdrBox.SelectedIndex = 0;
					}
				}
				txtRet.Text = csBuf;									// _CAOɕ\
			}
			cboDelimBox_SelectionChangeCommitted(cboDelimBox, new System.EventArgs());
		}

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

		//	[M[`]
		private void btnSend_Click(object sender, System.EventArgs e)
		{
			srBuffer = txtSendData.Text;								// Mf[^srBufferɃRs[܂B
			Srlen = (uint)srBuffer.Length;								// Mf[^̒𑪂܂B
			if (Srlen != 0x0)
			{															// Mf[^OȊO͎̎s܂B
				txtRet.Text = "MĂ܂DDD";
				txtRet.Refresh();

				SubFunc.Cmd[0] = 2;										// R}hłB
				SubFunc.Cmd[1] = Myadr;									// g[JAhXłB
				SubFunc.Cmd[2] = Yradr;									// XiAhXłB
				Ret = gpib.Talk(SubFunc.pCmd, Srlen, srBuffer);			// ̕ۂ̃f[^𑗐M镔łB
				SubFunc.CheckRet("GpTalk", (Ret & 0xFF), out csBuf);	// GpTalkɍs`FbN܂B
				txtRet.Text = csBuf;
			}
			else
				txtRet.Text = "Mf[^܂B";
		}

		//	[M[`]
		private void btnReceive_Click(object sender, System.EventArgs e)
		{
			int StartIndex=0;
			int Strlen=0;
			
			txtRet.Text = "MĂ܂DDD";
			txtRet.Refresh();
			Yradr = Convert.ToUInt32(cboYrAdrBox.Text);					// @̃AhX擾܂B
			SubFunc.Cmd[0] = 2;											// R}h(bZ[W)
			SubFunc.Cmd[1] = Yradr;										// g[JAhX
			SubFunc.Cmd[2] = Myadr;										// XiAhX
			rvBuffer.Remove(0, rvBuffer.Length);						// Mobt@NA܂B
			Srlen = (uint)rvBuffer.Capacity;							// Mobt@̃TCYw肵܂B
			Ret = gpib.Listen(SubFunc.pCmd, ref Srlen, rvBuffer);		// ۂɎMs܂B
			if (SubFunc.CheckRet("GpListen", (Ret & 0xFF), out csBuf) == 0)
			{															// GpListenɍs`FbN܂B
				
				StartIndex=Convert.ToInt32(Srlen);
				Strlen=rvBuffer.Length-StartIndex;
				txtReceiveData.Text = rvBuffer.Remove(StartIndex,Strlen).ToString(); 		// rvBuffer(Mf[^)\܂B
				csBuf = string.Format("f[^ [ {0:d} ] oCg擾܂B", Srlen);
				txtRet.Text = csBuf;														// szBuffer(MoCg)\܂B
			}
			else
			{
				txtRet.Text = csBuf.ToString();
			}
		}

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

		//	[f[^̏]
		private void btnDelData_Click(object sender, System.EventArgs e)
		{
			txtSendData.Text = "";
			txtReceiveData.Text = "";
		}

		//	[GET(}`CbZ[W)]
		private void btnGet_Click(object sender, System.EventArgs e)
		{
			Yradr = Convert.ToUInt32(cboYrAdrBox.Text);					// @̃AhX擾܂B
			Ret = gpib.Get(Yradr);										// @ɑ΂[Get]R}hsĂ܂
			Ret = (uint)SubFunc.CheckRet("GpGet", Ret, out csBuf);		// GpGet̖߂l`FbN
			txtRet.Text = csBuf;
		}

		//	[|[O]
		private void btnPolling_Click(object sender, System.EventArgs e)
		{
			Yradr = Convert.ToUInt32(cboYrAdrBox.Text);					// @̃AhX擾܂B
			txtRet.Text = "|[OĂ܂DDD";
			txtRet.Refresh();											// ʂXVȂɍĕ`悵܂B
			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)
			{
				csBuf = string.Format("[SRQ] F܂Bg[J=[ {0:x} ]:Xe[^XoCg=[ {1:x} ]H", SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]]);
			}
			else if ((Ret & 0xFF) == 1)									// [SRQ] [EOI]mF
			{
				csBuf = string.Format("[SRQ]  [EOI]tŊmFBg[J=[ {0:x} ]:Xe[^XoCg=[ {1:x} ]H", SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]]);
			}
			else if ((Ret & 0xFF) == 128)								// mFȂ
			{
				csBuf = "[SRQ] mFł܂łB";
			}
			else
			{															// 0,1,128ȊȌꍇ
				SubFunc.CheckRet("GpPoll", Ret, out csBuf);
			}
			txtRet.Text = csBuf;										// bZ[W\܂B
		}

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

		//	[f~^ύX]
		private void cboDelimBox_SelectionChangeCommitted(object sender, System.EventArgs e)
		{
			Eoi = 1;													// EOI	Fgp
			Delim = (uint)cboDelimBox.SelectedIndex;
			Ret = gpib.Delim(Delim, Eoi);
		}
	}
}
