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 MltMeter
{
	/// <summary>
	/// Form1 ̊Tv̐łB
	/// </summary>
	public class MltMeter : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.ComboBox cboSelectDevice;
		private System.Windows.Forms.TextBox txtSendData;
		private System.Windows.Forms.TextBox txtReceiveData;
		private System.Windows.Forms.TextBox txtRet;
		private System.Windows.Forms.ComboBox cboYrAdrBox;
		private System.Windows.Forms.Button btnDelRet;
		private System.Windows.Forms.RadioButton rdoTimer;
		private System.Windows.Forms.RadioButton btnOnSrq;
		private System.Windows.Forms.Button btnReceiveStart;
		private System.Windows.Forms.Button btnReceiveStop;
		private System.Windows.Forms.Button btnInit;
		private System.Windows.Forms.Button btnExt;
		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(); 

		// zAϐ̐錾 ///////////////////////////////////////////////////////////
		//UINT	nIDTimerEvent;									// ^C}CxgŎgp
		string			csBuf;									// G[Ȃǂ̃eLXgp
		uint			Yradr,Myadr;							// }CAhXA@AhX
		uint			Delim,Eoi;								// f~^
		uint			regvalue;								// SRQrbg̗L
		uint			Srlen, Ret;								// M̃oCgAꂼ̖߂l
		uint[]			Pstb = new uint[32];					// bZ[W̔z
		uint			SrqOn;									// SRQ̗L
		ushort			wMsg;									// EBhEbZ[W
		string			SendBuf = new string(' ', 70000);		// Mobt@/read work
		bool			bTimerOn;								// ^C}̏(1:^C}~ 2:^C}s)
		bool			ErrorOccured;							// G[̗L
		int				Device;									// @̃CfbNX(ԍFԖڂ͂O)
		//int			Count;									// JEg^[v񐔂Ȃ
		//int			SetT;									// KillTimer()̊mF(0:~ 1:s)
		string			szBuffer = new string(' ', 4096);		// eLXgobt@
		StringBuilder	ReceiveBuf = new StringBuilder(70000);	// Mobt@/read work
		GCHandle		gChCmd;									// CmdzpGCHandle
		GCHandle 		gChPstb; 								// PstbzpGCHandle
		IntPtr 			pPstb;									// PstbzpIntPtr
		///////////////////////////////////////////////////////////////////////////////

		public MltMeter()
		{
			//
			// 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.label1 = new System.Windows.Forms.Label();
			this.cboSelectDevice = new System.Windows.Forms.ComboBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.btnReceiveStop = new System.Windows.Forms.Button();
			this.btnReceiveStart = new System.Windows.Forms.Button();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.btnOnSrq = new System.Windows.Forms.RadioButton();
			this.rdoTimer = new System.Windows.Forms.RadioButton();
			this.btnDelRet = new System.Windows.Forms.Button();
			this.cboYrAdrBox = new System.Windows.Forms.ComboBox();
			this.txtRet = new System.Windows.Forms.TextBox();
			this.txtReceiveData = new System.Windows.Forms.TextBox();
			this.txtSendData = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.btnInit = new System.Windows.Forms.Button();
			this.btnExt = new System.Windows.Forms.Button();
			this.label6 = 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();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 12);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(104, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "@햼";
			// 
			// cboSelectDevice
			// 
			this.cboSelectDevice.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboSelectDevice.Location = new System.Drawing.Point(96, 8);
			this.cboSelectDevice.Name = "cboSelectDevice";
			this.cboSelectDevice.Size = new System.Drawing.Size(296, 24);
			this.cboSelectDevice.TabIndex = 1;
			this.cboSelectDevice.SelectedIndexChanged += new System.EventHandler(this.cboSelectDevice_SelectedIndexChanged);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnReceiveStop,
																					this.btnReceiveStart,
																					this.groupBox2,
																					this.btnDelRet,
																					this.cboYrAdrBox,
																					this.txtRet,
																					this.txtReceiveData,
																					this.txtSendData,
																					this.label5,
																					this.label4,
																					this.label3,
																					this.label2});
			this.groupBox1.Location = new System.Drawing.Point(8, 40);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(530, 208);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "I(&X)";
			// 
			// btnReceiveStop
			// 
			this.btnReceiveStop.Location = new System.Drawing.Point(352, 146);
			this.btnReceiveStop.Name = "btnReceiveStop";
			this.btnReceiveStop.Size = new System.Drawing.Size(168, 48);
			this.btnReceiveStop.TabIndex = 12;
			this.btnReceiveStop.Text = "f[^M~(&E)";
			this.btnReceiveStop.Click += new System.EventHandler(this.btnReceiveStop_Click);
			// 
			// btnReceiveStart
			// 
			this.btnReceiveStart.Location = new System.Drawing.Point(176, 146);
			this.btnReceiveStart.Name = "btnReceiveStart";
			this.btnReceiveStart.Size = new System.Drawing.Size(168, 48);
			this.btnReceiveStart.TabIndex = 11;
			this.btnReceiveStart.Text = "f[^MJn(&S)";
			this.btnReceiveStart.Click += new System.EventHandler(this.btnReceiveStart_Click);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnOnSrq,
																					this.rdoTimer});
			this.groupBox2.Location = new System.Drawing.Point(7, 146);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(154, 47);
			this.groupBox2.TabIndex = 10;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "M^C~O";
			// 
			// btnOnSrq
			// 
			this.btnOnSrq.Location = new System.Drawing.Point(72, 20);
			this.btnOnSrq.Name = "btnOnSrq";
			this.btnOnSrq.Size = new System.Drawing.Size(80, 24);
			this.btnOnSrq.TabIndex = 1;
			this.btnOnSrq.Text = "On SRQ";
			// 
			// rdoTimer
			// 
			this.rdoTimer.Location = new System.Drawing.Point(8, 20);
			this.rdoTimer.Name = "rdoTimer";
			this.rdoTimer.Size = new System.Drawing.Size(72, 24);
			this.rdoTimer.TabIndex = 0;
			this.rdoTimer.Text = "Timer";
			// 
			// btnDelRet
			// 
			this.btnDelRet.Location = new System.Drawing.Point(352, 110);
			this.btnDelRet.Name = "btnDelRet";
			this.btnDelRet.Size = new System.Drawing.Size(168, 28);
			this.btnDelRet.TabIndex = 9;
			this.btnDelRet.Text = "߂l(&D)";
			this.btnDelRet.Click += new System.EventHandler(this.btnDelRet_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(176, 112);
			this.cboYrAdrBox.Name = "cboYrAdrBox";
			this.cboYrAdrBox.Size = new System.Drawing.Size(56, 24);
			this.cboYrAdrBox.TabIndex = 8;
			this.cboYrAdrBox.SelectedIndexChanged += new System.EventHandler(this.cboYrAdrBox_SelectedIndexChanged);
			// 
			// txtRet
			// 
			this.txtRet.Location = new System.Drawing.Point(176, 81);
			this.txtRet.Name = "txtRet";
			this.txtRet.Size = new System.Drawing.Size(344, 23);
			this.txtRet.TabIndex = 7;
			this.txtRet.Text = "";
			// 
			// txtReceiveData
			// 
			this.txtReceiveData.Location = new System.Drawing.Point(176, 50);
			this.txtReceiveData.Name = "txtReceiveData";
			this.txtReceiveData.Size = new System.Drawing.Size(344, 23);
			this.txtReceiveData.TabIndex = 6;
			this.txtReceiveData.Text = "";
			// 
			// txtSendData
			// 
			this.txtSendData.Location = new System.Drawing.Point(176, 19);
			this.txtSendData.Name = "txtSendData";
			this.txtSendData.Size = new System.Drawing.Size(344, 23);
			this.txtSendData.TabIndex = 5;
			this.txtSendData.Text = "";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(7, 116);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(176, 16);
			this.label5.TabIndex = 4;
			this.label5.Text = "@AhX";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(7, 84);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(176, 16);
			this.label4.TabIndex = 3;
			this.label4.Text = "߂l(Xe[^X)";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(7, 53);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(176, 16);
			this.label3.TabIndex = 2;
			this.label3.Text = "Mf[^";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(7, 22);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(176, 16);
			this.label2.TabIndex = 1;
			this.label2.Text = "Mf[^(e)";
			// 
			// btnInit
			// 
			this.btnInit.Location = new System.Drawing.Point(8, 256);
			this.btnInit.Name = "btnInit";
			this.btnInit.Size = new System.Drawing.Size(168, 28);
			this.btnInit.TabIndex = 10;
			this.btnInit.Text = "(&I)";
			this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
			// 
			// btnExt
			// 
			this.btnExt.Location = new System.Drawing.Point(370, 256);
			this.btnExt.Name = "btnExt";
			this.btnExt.Size = new System.Drawing.Size(168, 28);
			this.btnExt.TabIndex = 12;
			this.btnExt.Text = "I(&X)";
			this.btnExt.Click += new System.EventHandler(this.btnExt_Click);
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(394, 12);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(72, 16);
			this.label6.TabIndex = 13;
			this.label6.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(464, 8);
			this.cboDelimBox.Name = "cboDelimBox";
			this.cboDelimBox.Size = new System.Drawing.Size(72, 24);
			this.cboDelimBox.TabIndex = 14;
			this.cboDelimBox.SelectedIndexChanged += new System.EventHandler(this.cboDelimBox_SelectedIndexChanged);
			// 
			// timer
			// 
			this.timer.Tick += new System.EventHandler(this.timer_Tick);
			// 
			// MltMeter
			// 
			this.ClientSize = new System.Drawing.Size(548, 296);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.cboDelimBox,
																		  this.label6,
																		  this.btnExt,
																		  this.btnInit,
																		  this.groupBox1,
																		  this.cboSelectDevice,
																		  this.label1});
			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 = "MltMeter";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "}`[^";
			this.Load += new System.EventHandler(this.MltMeter_Load);
			this.Closed += new System.EventHandler(this.MltMeter_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 MltMeter());
		}

		private void MltMeter_Load(object sender, System.EventArgs e)
		{
			// [ݒ]
			cboYrAdrBox.SelectedIndex = 0;
			cboDelimBox.SelectedIndex = 1;
			// I@̒ǉ
			cboSelectDevice.Items.Add("[ ADVANTEST R6871E Digital Multimeter ]");
			cboSelectDevice.Items.Add("[ Sanwa Digital Multimeter 9300MF ]");
			cboSelectDevice.Items.Add("[ Iwatsu Digital Multimeter SC-7404 ]");
			cboSelectDevice.SelectedIndex = 0;						// ݒ

			SendBuf = "F1R5M2IT1DL0RE7BZ1S0";						/* Initialize Send Data */
			rdoTimer.Checked = true;								/* Select timer */
			txtSendData.Text = SendBuf;
			wMsg = 0x0400 + 0x30;									// WM_USER + 0x30
			bTimerOn = false;

			// 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 MltMeter_Closed(object sender, System.EventArgs e)
		{
			SrqOn = 0;												// Event

			Ret = gpib.SetEvent(SrqOn);								// SRQ event It
			timer.Enabled = false;
			SubFunc.GpibExit();										// GpibExit֐R[(SubClass.h)

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

		// [SrqEvent֐] /////////////////////////////////////////////////////////////
		protected override void WndProc(ref Message m)
		{
			if (m.Msg == wMsg)
			{
				ErrorOccured = false;								// G[̗L`FbN
				SubFunc.Cmd[0] = 1;									// @ɑ΂ă|[O
				SubFunc.Cmd[1] = Yradr;
				Ret = gpib.Poll(SubFunc.pCmd, pPstb);				// |[Os
				if ((Ret & 0xFF) != 0)
				{													// G[̏
					SubFunc.CheckRet("GpPoll", Ret, out csBuf);
					ErrorOccured = true;
					goto ErrorOnSrq;
				}
				SubFunc.Cmd[0] = 2;
				SubFunc.Cmd[1] = Yradr;
				SubFunc.Cmd[2] = Myadr;
				ReceiveBuf.Remove(0, ReceiveBuf.Length);			// Mobt@NA܂B
				Srlen = (uint)ReceiveBuf.Capacity;					// ReceiveBuf̃TCY𑪂
				Ret = gpib.Listen(SubFunc.pCmd, ref Srlen, ReceiveBuf);		// M̎s
				if ((Ret & 0xFF) >= 3)
				{
					SubFunc.CheckRet("GpListen", Ret, out csBuf);
					ErrorOccured = true;
					goto ErrorOnSrq;
				}
				SubFunc.Cmd[0] = 2;
				SubFunc.Cmd[1] = 0x3f;								// AX(Xi)
				SubFunc.Cmd[2] = 0x5f;								// Ag[N(g[J)
				Ret = gpib.Comand(SubFunc.pCmd);					// R}h(bZ[W)M
				if ((Ret & 0xFF) != 0)
				{													// G[̏
					SubFunc.CheckRet("GpComand", Ret, out csBuf);
					ErrorOccured = true;
					goto ErrorOnSrq;
				}
				Ret = gpib.Get(Yradr);								// S@ɑ΂[GpGet]s
				if ((Ret & 0xFF) != 0)
				{													// G[̏
					SubFunc.CheckRet("GpGet", Ret, out csBuf);
					ErrorOccured = true;
					goto ErrorOnSrq;
				}
				txtRet.Text = csBuf;								// G[Ε\

			ErrorOnSrq:
				if (ErrorOccured != false)
				{													// G[̗L
					SrqOn = 0;
					Ret = gpib.SetEventSrq(this.Handle.ToInt32(), wMsg, SrqOn);	// SRQ Event̏I
					return;
				}
				txtReceiveData.Text = ReceiveBuf.ToString().Substring(0, (int)Srlen);		// _CAOɕ\
				szBuffer = string.Format("f[^M܂B[ {0:d} ]oCg擾܂B", Srlen);
				txtRet.Text = szBuffer;

				Ret = gpib.EnableNextEvent();						// GpEnableNextEvent֐̃R[
			}
			base.WndProc(ref m);
		}

		// [] ///////////////////////////////////////////////////////////////////
		private void btnInit_Click(object sender, System.EventArgs e)
		{
			string	DelMyadr = new string(' ', 16);					// }CAhX폜p
	
			Ret = (uint)SubFunc.GpibInit(out csBuf);				// Init֐̌ĂяoĂ܂B
			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;
				}
			}

			//	Ret = gpib.Dcl();									// Device CleaŕA@̓Ԃň莞ԑ҂Kv

			SendBuf = txtSendData.Text;
			Srlen = (uint)SendBuf.Length;
			SubFunc.Cmd[0] = 2;										// R}h(bZ[W)̐
			SubFunc.Cmd[1] = Myadr;									// g[JAhX
			SubFunc.Cmd[2] = Yradr;									// XiAhX
			Ret = gpib.Talk(SubFunc.pCmd, Srlen, SendBuf);			// bZ[W𑗐M܂B
			SubFunc.CheckRet("Initialize", Ret, out csBuf);
			if ((Ret & 0xFF) == 0)
			{
				csBuf = "܂B";
			}
			txtRet.Text = csBuf;

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

		// [@ւ̑Mf[^ύX] ///////////////////////////////////////////////
		private void cboSelectDevice_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Device = cboSelectDevice.SelectedIndex;					// @햼̃CfbNX
			switch (Device)
			{														// M@̐ݒ
				case 0:
					szBuffer = "F1R5M2IT1DL0RE7BZ1S0";				// AhoeXg/Advantest
					break;
				case 1:
					szBuffer = "F1R0I0C1";							// T/Sanwa
					break;
				case 2:
					szBuffer = "R0";								// /Iwatsu
					break;
				default:
					break;
			}
			txtSendData.Text = szBuffer;
		}

		// [MJn] /////////////////////////////////////////////////////////////////
		private void btnReceiveStart_Click(object sender, System.EventArgs e)
		{
			uint	SrqOn;											// SRQ Event ̏

			SrqOn = 1;												// s()

			if (rdoTimer.Checked == true)
			{														// M^C~O
				if (!bTimerOn)
				{													// ^C}I
					timer.Enabled = true;
					bTimerOn = true;
				}
			}
			else
			{
				SrqOn = 1;											// Cxg(On SRQ)I
				Ret = gpib.SetEventSrq(this.Handle.ToInt32(), wMsg, SrqOn);	// SRQ event I
				if ((Ret & 0xFF) != 0)
				{													// GpSetEventSrq̖߂l`FbN
					SubFunc.CheckRet("GpSetEventSrq", Ret, out csBuf);
					txtRet.Text = csBuf;
					return;
				}
				Ret = gpib.SetEvent(SrqOn);							// GpSetEvent()֐̃R[
				if ((Ret & 0xFF) != 0)
				{						
					SubFunc.CheckRet("GpSetEvent", Ret, out csBuf);
					txtRet.Text = csBuf;
					return;
				}
				Ret = gpib.Get(Yradr);								// [GET]R}hs
				if ((Ret & 0xFF) != 0)
				{													// GpGet̖߂l`FbN
					SubFunc.CheckRet("GpGet", Ret, out csBuf);
					SrqOn = 0;										// ~()
					Ret = gpib.SetEvent(SrqOn);
					txtRet.Text = csBuf;
					return;
				}
			}
			txtRet.Text = "MJn܂B";
		}

		// [M~] /////////////////////////////////////////////////////////////////
		private void btnReceiveStop_Click(object sender, System.EventArgs e)
		{
			txtRet.Text = "f[^M~܂B";
			if (rdoTimer.Checked == true)
			{														// M^C~O
				timer.Enabled = false;								// ^C}~
				bTimerOn = false;
			}
			else
			{
				SrqOn = 0;
				Ret = gpib.SetEvent(SrqOn);							// SRQ event It
			}
		}

		// [I] /////////////////////////////////////////////////////////////////////
		private void btnExt_Click(object sender, System.EventArgs e)
		{
			SrqOn = 0;												// Event

			Ret = gpib.SetEvent(SrqOn);								// SRQ event It
			timer.Enabled = false;
			SubFunc.GpibExit();										// GpibExit֐R[(SubClass.h)

			this.Close();											// _CAO܂B(vOI)
		}

		// [^C}] ///////////////////////////////////////////////////////////////////
		private void timer_Tick(object sender, System.EventArgs e)
		{
			ErrorOccured = false;
			Yradr = Convert.ToUInt32(cboYrAdrBox.Text);

			Ret = gpib.Get(Yradr);
			if ((Ret & 0xFF) != 0)
			{														// [GpGet]̖߂l`FbN
				SubFunc.CheckRet("GpGet", Ret, out csBuf);
				ErrorOccured = true;
				goto ErrorOnTimer;
			}
			regvalue = 0;
			while (regvalue == 0)
			{														// SRQ rbg`FbN
				if (EscCheck() == 1)
				{
					ErrorOccured = true;
					csBuf = "[Esc] L[܂B";
					goto ErrorOnTimer;
				}
				Ret = gpib.Boardsts(0x12, out regvalue);			// WX^̃AhXXe[^Xǂݍ݂܂B
				if ((Ret & 0xFF) != 0)
				{
					SubFunc.CheckRet("GpBoardsts", Ret, out csBuf);
					ErrorOccured = true;
					goto ErrorOnTimer;
				}
			}

			do
			{
				SubFunc.Cmd[0] = 1;
				SubFunc.Cmd[1] = Yradr;
				Ret = gpib.Poll(SubFunc.pCmd, pPstb);
				gpib.Waittime(10);
				if (bTimerOn == false)
				{
					break;
				}
			}while (Ret == 128);
						
			if ((Ret & 0xFF) != 0)
			{														// [GpPoll]̖߂l`FbN
				SubFunc.CheckRet("GpPoll", Ret, out csBuf);
				ErrorOccured = true;
				goto ErrorOnTimer;
			}
			SubFunc.Cmd[0] = 2;
			SubFunc.Cmd[1] = Yradr;
			SubFunc.Cmd[2] = Myadr;
			ReceiveBuf.Remove(0, ReceiveBuf.Length);				// Mobt@NA܂B
			Srlen = (uint)ReceiveBuf.Capacity;
			Ret = gpib.Listen(SubFunc.pCmd, ref Srlen, ReceiveBuf);
			if ((Ret & 0xFF) >= 3)
			{														// [GpListen]̖߂l`FbN
				SubFunc.CheckRet("GpListen", Ret, out csBuf);
				ErrorOccured = true;
				goto ErrorOnTimer;
			}
			SubFunc.Cmd[0] = 2;
			SubFunc.Cmd[1] = 0x3f;
			SubFunc.Cmd[2] = 0x5f;
			Ret = gpib.Comand(SubFunc.pCmd);
			if ((Ret & 0xFF) != 0)
			{														// [GpComand]̖߂l`FbN
				SubFunc.CheckRet("GpComand", Ret, out csBuf);
				ErrorOccured = true;
				goto ErrorOnTimer;
			}

			ErrorOnTimer:											// G[ꍇ̏
				if (ErrorOccured != false)
				{
					timer.Enabled = false;
					txtRet.Text = csBuf;
					return;
				}
			txtReceiveData.Text = ReceiveBuf.ToString().Substring(0, (int)Srlen);
			szBuffer = string.Format("f[^MB[ {0:d} ]oCgM܂B", Srlen);
			txtRet.Text = szBuffer;
		}

		// [߂l] /////////////////////////////////////////////////////////////
		private void btnDelRet_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擾
		}

		///// [ESC]L[̐ //////////////////////////////////////////////////////////////////
		private int EscCheck()
		{
			int	vKey;

			vKey = 0x1B;											// ESC̃AXL[R[h
			if (gpib.gpGetAsyncKeyState(vKey) != 0)
			{
				return 1;											// ESCL[o(߂l)
			}
			else
			{
				return 0;											// ESCL[Ȃ(߂l)
			}
		}

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