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 Slave
{
	/// <summary>
	/// Form1 ̊Tv̐łB
	/// </summary>
	public class Slave : 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.Label label5;
		private System.Windows.Forms.ComboBox cboStsByte;
		private System.Windows.Forms.Button btnInit;
		private System.Windows.Forms.Button btnSrqOn;
		private System.Windows.Forms.Button btnSrqOff;
		private System.Windows.Forms.Button btnExt;
		private System.Windows.Forms.TextBox txtSendData;
		private System.Windows.Forms.Button btnSend;
		private System.Windows.Forms.TextBox txtReceiveData;
		private System.Windows.Forms.Button btnReceive;
		private System.Windows.Forms.Button btnDelData;
		private System.Windows.Forms.Button btnDelRet;
		private System.Windows.Forms.TextBox txtRet;
		private System.Windows.Forms.TextBox txtMyAddress;
		private System.Windows.Forms.ComboBox cboDelimBox;
		private System.Windows.Forms.Timer timer;
		private System.ComponentModel.IContainer components;

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

		// ϐ萔Ő錾Ă܂B////////////////////////////////
		string			szBuffer = new string(' ', 1024);		// eLXg̃obt@
		uint			Ret;									// ߂l
		uint			Myadr, /*Yradr,*/ Mode; 				// }CAhXA@AhXA}X^
		uint			Delim, Eoi; 							// f~^
		uint			/*Timeout, Ifctime,Dmamode, */Srlen;
		uint			P_stb, P_eoi;							// Xe[^XoCgAEOĨ|C^
		string			srBuffer = new string(' ', 10000);		// The buffer of send work
		//uint			uTimerTrm;								// ^C}̓`FbNp
		//uint			uTimerEvent;							// ^C}Cxgp
		string			csBuf;									// eLXg\p
		string			pStb;
		StringBuilder	rvBuffer = new StringBuilder(10000);	// The buffer of read work
		GCHandle		gChCmd;									// CmdzpGCHandle
		/////////////////////////////////////////////////////////////////////

		public Slave()
		{
			//
			// 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.components = new System.ComponentModel.Container();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label5 = new System.Windows.Forms.Label();
			this.txtMyAddress = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.cboStsByte = new System.Windows.Forms.ComboBox();
			this.btnInit = new System.Windows.Forms.Button();
			this.btnSrqOn = new System.Windows.Forms.Button();
			this.btnSrqOff = new System.Windows.Forms.Button();
			this.btnExt = new System.Windows.Forms.Button();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.btnReceive = new System.Windows.Forms.Button();
			this.txtReceiveData = new System.Windows.Forms.TextBox();
			this.btnSend = new System.Windows.Forms.Button();
			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.label4 = new System.Windows.Forms.Label();
			this.cboDelimBox = new System.Windows.Forms.ComboBox();
			this.timer = new System.Windows.Forms.Timer(this.components);
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.label5,
																					this.txtMyAddress,
																					this.label2,
																					this.label1,
																					this.cboStsByte});
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(430, 42);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(403, 16);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(16, 16);
			this.label5.TabIndex = 9;
			this.label5.Text = "H";
			// 
			// txtMyAddress
			// 
			this.txtMyAddress.Location = new System.Drawing.Point(136, 13);
			this.txtMyAddress.Name = "txtMyAddress";
			this.txtMyAddress.ReadOnly = true;
			this.txtMyAddress.Size = new System.Drawing.Size(32, 23);
			this.txtMyAddress.TabIndex = 3;
			this.txtMyAddress.Text = "";
			this.txtMyAddress.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(194, 16);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(160, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "Xe[^XoCg =";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(128, 16);
			this.label1.TabIndex = 1;
			this.label1.Text = "}CAhX =";
			// 
			// cboStsByte
			// 
			this.cboStsByte.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStsByte.Items.AddRange(new object[] {
															"40",
															"41",
															"42",
															"43",
															"44",
															"45",
															"46",
															"47",
															"48",
															"49",
															"4a",
															"4b",
															"4c",
															"4d",
															"4e",
															"4f",
															"50",
															"51",
															"52",
															"53",
															"54",
															"55",
															"56",
															"57",
															"58",
															"59",
															"5a",
															"5b",
															"5c",
															"5d",
															"5e",
															"5f",
															"60",
															"61",
															"62",
															"63",
															"64",
															"65",
															"66",
															"67",
															"68",
															"69",
															"6a",
															"6b",
															"6c",
															"6d",
															"6e",
															"6f",
															"70",
															"71",
															"72",
															"73",
															"74",
															"75",
															"76",
															"77",
															"78",
															"79",
															"7a",
															"7b",
															"7c",
															"7d",
															"7e",
															"7f",
															"80",
															"81",
															"82",
															"83",
															"84",
															"85",
															"86",
															"87",
															"88",
															"89",
															"8a",
															"8b",
															"8c",
															"8d",
															"8e",
															"8f",
															"90",
															"91",
															"92",
															"93",
															"94",
															"95",
															"96",
															"97",
															"98",
															"99",
															"9a",
															"9b",
															"9c",
															"9d",
															"9e",
															"9f",
															"a0",
															"a1",
															"a2",
															"a3",
															"a4",
															"a5",
															"a6",
															"a7",
															"a8",
															"a9",
															"aa",
															"ab",
															"ac",
															"ad",
															"ae",
															"af",
															"b0",
															"b1",
															"b2",
															"b3",
															"b4",
															"b5",
															"b6",
															"b7",
															"b8",
															"b9",
															"ba",
															"bb",
															"bc",
															"bd",
															"be",
															"bf",
															"c0",
															"c1",
															"c2",
															"c3",
															"c4",
															"c5",
															"c6",
															"c7",
															"c8",
															"c9",
															"ca",
															"cb",
															"cc",
															"cd",
															"ce",
															"cf",
															"d0",
															"d1",
															"d2",
															"d3",
															"d4",
															"d5",
															"d6",
															"d7",
															"d8",
															"d9",
															"da",
															"db",
															"dc",
															"dd",
															"de",
															"df",
															"e0",
															"e1",
															"e2",
															"e3",
															"e4",
															"e5",
															"e6",
															"e7",
															"e8",
															"e9",
															"ea",
															"eb",
															"ec",
															"ed",
															"ee",
															"ef",
															"f0",
															"f1",
															"f2",
															"f3",
															"f4",
															"f5",
															"f6",
															"f7",
															"f8",
															"f9",
															"fa",
															"fb",
															"fc",
															"fd",
															"fe",
															"ff"});
			this.cboStsByte.Location = new System.Drawing.Point(354, 12);
			this.cboStsByte.Name = "cboStsByte";
			this.cboStsByte.Size = new System.Drawing.Size(48, 24);
			this.cboStsByte.TabIndex = 0;
			// 
			// btnInit
			// 
			this.btnInit.Location = new System.Drawing.Point(448, 13);
			this.btnInit.Name = "btnInit";
			this.btnInit.Size = new System.Drawing.Size(104, 32);
			this.btnInit.TabIndex = 1;
			this.btnInit.Text = "(&I)";
			this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
			// 
			// btnSrqOn
			// 
			this.btnSrqOn.Location = new System.Drawing.Point(448, 127);
			this.btnSrqOn.Name = "btnSrqOn";
			this.btnSrqOn.Size = new System.Drawing.Size(104, 32);
			this.btnSrqOn.TabIndex = 2;
			this.btnSrqOn.Text = "SRQo(&Q)";
			this.btnSrqOn.Click += new System.EventHandler(this.btnSrqOn_Click);
			// 
			// btnSrqOff
			// 
			this.btnSrqOff.Location = new System.Drawing.Point(448, 168);
			this.btnSrqOff.Name = "btnSrqOff";
			this.btnSrqOff.Size = new System.Drawing.Size(104, 32);
			this.btnSrqOff.TabIndex = 3;
			this.btnSrqOff.Text = "SRQ~(&E)";
			this.btnSrqOff.Click += new System.EventHandler(this.btnSrqOff_Click);
			// 
			// btnExt
			// 
			this.btnExt.Location = new System.Drawing.Point(448, 208);
			this.btnExt.Name = "btnExt";
			this.btnExt.Size = new System.Drawing.Size(104, 32);
			this.btnExt.TabIndex = 4;
			this.btnExt.Text = "I(&X)";
			this.btnExt.Click += new System.EventHandler(this.btnExt_Click);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnReceive,
																					this.txtReceiveData,
																					this.btnSend,
																					this.txtSendData});
			this.groupBox2.Location = new System.Drawing.Point(8, 56);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(430, 104);
			this.groupBox2.TabIndex = 5;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Mf[^";
			// 
			// btnReceive
			// 
			this.btnReceive.Location = new System.Drawing.Point(320, 54);
			this.btnReceive.Name = "btnReceive";
			this.btnReceive.Size = new System.Drawing.Size(96, 26);
			this.btnReceive.TabIndex = 3;
			this.btnReceive.Text = "M(&R)";
			this.btnReceive.Click += new System.EventHandler(this.btnReceive_Click);
			// 
			// txtReceiveData
			// 
			this.txtReceiveData.Location = new System.Drawing.Point(10, 56);
			this.txtReceiveData.Name = "txtReceiveData";
			this.txtReceiveData.Size = new System.Drawing.Size(304, 23);
			this.txtReceiveData.TabIndex = 2;
			this.txtReceiveData.Text = "";
			// 
			// btnSend
			// 
			this.btnSend.Location = new System.Drawing.Point(320, 22);
			this.btnSend.Name = "btnSend";
			this.btnSend.Size = new System.Drawing.Size(96, 26);
			this.btnSend.TabIndex = 1;
			this.btnSend.Text = "M(&S)";
			this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			// 
			// txtSendData
			// 
			this.txtSendData.Location = new System.Drawing.Point(10, 24);
			this.txtSendData.Name = "txtSendData";
			this.txtSendData.Size = new System.Drawing.Size(304, 23);
			this.txtSendData.TabIndex = 0;
			this.txtSendData.Text = "";
			// 
			// btnDelData
			// 
			this.btnDelData.Location = new System.Drawing.Point(172, 168);
			this.btnDelData.Name = "btnDelData";
			this.btnDelData.Size = new System.Drawing.Size(130, 32);
			this.btnDelData.TabIndex = 6;
			this.btnDelData.Text = "f[^(&D)";
			this.btnDelData.Click += new System.EventHandler(this.btnDelData_Click);
			// 
			// btnDelRet
			// 
			this.btnDelRet.Location = new System.Drawing.Point(307, 168);
			this.btnDelRet.Name = "btnDelRet";
			this.btnDelRet.Size = new System.Drawing.Size(130, 32);
			this.btnDelRet.TabIndex = 7;
			this.btnDelRet.Text = "߂l(&C)";
			this.btnDelRet.Click += new System.EventHandler(this.btnDelRet_Click);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 196);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 16);
			this.label3.TabIndex = 8;
			this.label3.Text = "߂l";
			// 
			// txtRet
			// 
			this.txtRet.Location = new System.Drawing.Point(8, 213);
			this.txtRet.Name = "txtRet";
			this.txtRet.Size = new System.Drawing.Size(430, 23);
			this.txtRet.TabIndex = 9;
			this.txtRet.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(448, 61);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(72, 16);
			this.label4.TabIndex = 11;
			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(452, 79);
			this.cboDelimBox.Name = "cboDelimBox";
			this.cboDelimBox.Size = new System.Drawing.Size(96, 24);
			this.cboDelimBox.TabIndex = 12;
			this.cboDelimBox.SelectedIndexChanged += new System.EventHandler(this.cboDelimBox_SelectedIndexChanged);
			// 
			// timer
			// 
			this.timer.Tick += new System.EventHandler(this.timer_Tick);
			// 
			// Slave
			// 
			this.ClientSize = new System.Drawing.Size(562, 248);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.cboDelimBox,
																		  this.label4,
																		  this.txtRet,
																		  this.label3,
																		  this.btnDelRet,
																		  this.btnDelData,
																		  this.groupBox2,
																		  this.btnExt,
																		  this.btnSrqOff,
																		  this.btnSrqOn,
																		  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 = "Slave";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "X[u[h";
			this.Load += new System.EventHandler(this.Slave_Load);
			this.Closed += new System.EventHandler(this.Slave_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 Slave());
		}

		private void Slave_Load(object sender, System.EventArgs e)
		{
			// ݒ
			cboStsByte.Text = "40";									// XĝOԖڂ\܂B
			txtSendData.Text = "_0123456789_ Test Data / 33 Bytes";
			cboDelimBox.SelectedIndex = 1;

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

		private void Slave_Closed(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();										// I֐ĂяoĂ܂B

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

		// [[`] ///////////////////////////////////////////////////////////
		private void btnInit_Click(object sender, System.EventArgs e)
		{
			SubFunc.GpibInit(out csBuf);							// ֐ĂяoĂ܂B

			gpib.Boardsts(0x0a, out Mode);
			gpib.Boardsts(0x08, out Myadr);
			txtMyAddress.Text = Myadr.ToString();
	
			if (Mode == 0)
			{	 													// }X^̏ꍇ
				txtRet.Text = "̋@̓X[uł͂܂B";
				return;
			}
			else
			{
				txtRet.Text = csBuf;
			}

			cboDelimBox_SelectedIndexChanged(cboDelimBox, new System.EventArgs());
		}

		// [M[`] /////////////////////////////////////////////////////////////
		private void btnSend_Click(object sender, System.EventArgs e)
		{
			txtRet.Text = "f[^𑗐MĂ܂DDD";
			txtRet.Refresh();										// ʂĕ`悵Ă܂B

			srBuffer = txtSendData.Text;							// Mf[^srBufferɃRs[܂B
			Srlen = (uint)srBuffer.Length;							// MŁ[̒𑪂܂B
	
			if (Srlen != 0x0)
			{														// Mf[^OȊO͎̎s܂B
				SubFunc.Cmd[0] = 0;									// R}hłB
				Ret = gpib.Talk(SubFunc.pCmd, Srlen, srBuffer);		// ̕ۂ̃f[^𑗐M镔łB
				SubFunc.CheckRet("GpTalk", Ret , out csBuf);		// GpTalk̖߂l`FbN
			}
			else
				csBuf = "Mf[^܂B";

			txtRet.Text = csBuf;									// ʂ\
		}

		// [M[`] /////////////////////////////////////////////////////////////
		private void btnReceive_Click(object sender, System.EventArgs e)
		{
			int StartIndex=0;
			int Strlen=0;
			txtRet.Text = "f[^MĂ܂DDD";
			txtRet.Refresh();										// ʂĕ`悵Ă܂B

			SubFunc.Cmd[0] = 0x00;									// R}h(X[uł0Œ)
			rvBuffer.Remove(0, rvBuffer.Length);					// Mobt@NA܂B
			Srlen = (uint)rvBuffer.Capacity;						// Mf[^̒𑪂܂B
			Ret = gpib.Listen(SubFunc.pCmd, ref Srlen, rvBuffer);	// ۂɎMsƂłB
			if (SubFunc.CheckRet("GpListen", Ret, out csBuf) == 0)
			{														// GpListen̖߂l`FbN
				StartIndex=Convert.ToInt32(Srlen);
				Strlen=rvBuffer.Length-StartIndex;
				txtReceiveData.Text = rvBuffer.Remove(StartIndex,Strlen).ToString(); 		// srBuffer(Mf[^)\܂B
				szBuffer = string.Format("f[^M܂B [ {0:d} ] oCg擾܂B", Srlen);
				csBuf = szBuffer;									// szBuffer(MoCg)\܂B
			}
			txtRet.Text = csBuf;									// G[̕\܂B
		}

		// [SRQvON] ////////////////////////////////////////////////////////////////
		private void btnSrqOn_Click(object sender, System.EventArgs e)
		{
			pStb = cboStsByte.Text;									// oϐ̕擾܂B
			P_stb = SubFunc.chr2hex(pStb);							// 𐔓IȒlɕϊ܂B
			Ret = gpib.Ppollmode(2);								// p|[ɔݒłB
			SubFunc.CheckRet("GpPollmode", Ret, out csBuf);			// GpPpollmode̒l`FbN
			Ret = gpib.Stb(P_stb);									// Xe[^XoCgݒ肵܂B
			SubFunc.CheckRet("GpStb", Ret, out csBuf);				// GpStssĂۂɂ̓WX^ɏނłđo͍sĂ܂B
			Ret = gpib.Srq(1);										// SRQ(EOI ON)StsByteSRQ𑗏o܂B
			SubFunc.CheckRet("GpSrq", Ret, out csBuf);				// GpSrq̖߂l`FbN

			timer.Enabled = true;									// ^C}[`Ăяo܂B
	
			txtRet.Text = "}X^̃|[O҂Ă܂...";
			txtRet.Refresh();
		}

		// [SRQ~] //////////////////////////////////////////////////////////////////
		private void btnSrqOff_Click(object sender, System.EventArgs e)
		{
			timer.Enabled = false;
			txtRet.Text = "uSRQv̑o~܂B";
		}

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

		// [OnTimer֐] //////////////////////////////////////////////////////////////
		private void timer_Tick(object sender, System.EventArgs e)
		{
			Ret = gpib.Chkstb(out P_stb, out P_eoi);				// Xe[^XoCgEOImF܂B
			SubFunc.CheckRet("StatusByte", Ret, out csBuf);			// Xe[^XoCg̃`FbN

			if ((P_stb & 0x40) != 0x40)
			{														// 0x40(6rbg)`FbN
				txtRet.Text = "[SRQ] F܂B";
				timer.Enabled = false;								// ^C}~܂B
			}	
		}

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

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

		private void cboDelimBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Eoi = 1;												// EOI	Fgp
			Delim = (uint)cboDelimBox.SelectedIndex;
			Ret = gpib.Delim(Delim, Eoi);
		}
	}
}
