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

namespace Oscillo2
{
	/// <summary>
	/// Form1 ̊Tv̐łB
	/// </summary>
	public class TEKTRO : 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.GroupBox groupBox2;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.GroupBox groupBox5;
		private System.Windows.Forms.ComboBox cboDevice;
		private System.Windows.Forms.TextBox txtVolts;
		private System.Windows.Forms.TextBox txtSec;
		private System.Windows.Forms.Button btnAutoSet;
		private System.Windows.Forms.Button btnSetWaveform;
		private System.Windows.Forms.Button btnChgWaveform;
		private System.Windows.Forms.ComboBox cboDelimBox;
		private System.Windows.Forms.Button btnEnd;
		/// <summary>
		/// KvȃfUCiϐłB
		/// </summary>
		private System.ComponentModel.Container components = null;

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

		// O[oϐ ***********************************************************************
		int			i;									// ėpϐ
		int			Ret, Ret2;							// ߂l1,2
		uint		MyAddr;								// }CAhX(GPIB{[hAhX)
		uint		Delim,Eoi;							// f~^
		uint[]		Fstb = new uint[32];				// ̒ɌfoCXƃfoCXAhX܂B
		string		MsgText, ErrText;					// ̃bZ[WłBG[̃bZ[WłB
		GCHandle	gChCmd;								// CmdzpGCHandle
		GCHandle 	gChFstb; 							// FstbzpGCHandle
		IntPtr 		pFstb;								// FstbzpIntPtr
		// **************************************************************************************

		public TEKTRO()
		{
			//
			// 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.label1 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.cboDevice = new System.Windows.Forms.ComboBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.btnAutoSet = new System.Windows.Forms.Button();
			this.txtSec = new System.Windows.Forms.TextBox();
			this.txtVolts = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.btnSetWaveform = new System.Windows.Forms.Button();
			this.label7 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.groupBox4 = new System.Windows.Forms.GroupBox();
			this.btnChgWaveform = new System.Windows.Forms.Button();
			this.label9 = new System.Windows.Forms.Label();
			this.groupBox5 = new System.Windows.Forms.GroupBox();
			this.cboDelimBox = new System.Windows.Forms.ComboBox();
			this.btnEnd = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.groupBox4.SuspendLayout();
			this.groupBox5.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(34, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(112, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Tektronix TDS3000";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.cboDevice,
																					this.label2});
			this.groupBox1.Location = new System.Drawing.Point(12, 27);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(156, 48);
			this.groupBox1.TabIndex = 1;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "@ݒ";
			// 
			// cboDevice
			// 
			this.cboDevice.Location = new System.Drawing.Point(80, 18);
			this.cboDevice.Name = "cboDevice";
			this.cboDevice.Size = new System.Drawing.Size(64, 20);
			this.cboDevice.TabIndex = 1;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(7, 20);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 16);
			this.label2.TabIndex = 0;
			this.label2.Text = "AhX = ";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(12, 86);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(240, 16);
			this.label3.TabIndex = 2;
			this.label3.Text = "<<< XP[ݒTv >>>";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnAutoSet,
																					this.txtSec,
																					this.txtVolts,
																					this.label5,
																					this.label4});
			this.groupBox2.Location = new System.Drawing.Point(12, 98);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(360, 56);
			this.groupBox2.TabIndex = 3;
			this.groupBox2.TabStop = false;
			// 
			// btnAutoSet
			// 
			this.btnAutoSet.Location = new System.Drawing.Point(232, 16);
			this.btnAutoSet.Name = "btnAutoSet";
			this.btnAutoSet.Size = new System.Drawing.Size(112, 32);
			this.btnAutoSet.TabIndex = 5;
			this.btnAutoSet.Text = "ݒ(&A)";
			this.btnAutoSet.Click += new System.EventHandler(this.btnAutoSet_Click);
			// 
			// txtSec
			// 
			this.txtSec.Location = new System.Drawing.Point(120, 29);
			this.txtSec.Name = "txtSec";
			this.txtSec.Size = new System.Drawing.Size(80, 19);
			this.txtSec.TabIndex = 4;
			this.txtSec.Text = "";
			// 
			// txtVolts
			// 
			this.txtVolts.Location = new System.Drawing.Point(16, 29);
			this.txtVolts.Name = "txtVolts";
			this.txtVolts.Size = new System.Drawing.Size(80, 19);
			this.txtVolts.TabIndex = 3;
			this.txtVolts.Text = "";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(120, 12);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(80, 16);
			this.label5.TabIndex = 2;
			this.label5.Text = "SEC/DIVl";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 12);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(80, 16);
			this.label4.TabIndex = 1;
			this.label4.Text = "VOLTS/DIVl";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(12, 165);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(240, 16);
			this.label6.TabIndex = 4;
			this.label6.Text = "<<< g`f[^擾Tv >>>";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnSetWaveform,
																					this.label7});
			this.groupBox3.Location = new System.Drawing.Point(12, 177);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(360, 56);
			this.groupBox3.TabIndex = 5;
			this.groupBox3.TabStop = false;
			// 
			// btnSetWaveform
			// 
			this.btnSetWaveform.Location = new System.Drawing.Point(232, 15);
			this.btnSetWaveform.Name = "btnSetWaveform";
			this.btnSetWaveform.Size = new System.Drawing.Size(112, 32);
			this.btnSetWaveform.TabIndex = 6;
			this.btnSetWaveform.Text = "f[^擾(&D)";
			this.btnSetWaveform.Click += new System.EventHandler(this.btnSetWaveform_Click);
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(8, 12);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(192, 38);
			this.label7.TabIndex = 0;
			this.label7.Text = "g`f[^̓JgpXɢ Wfm_data.csv  Ƃt@CŋL^Ă܂B";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(12, 244);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(272, 16);
			this.label8.TabIndex = 6;
			this.label8.Text = "<<< g`tH[̏Ԃ𑪒肷Tv >>>";
			// 
			// groupBox4
			// 
			this.groupBox4.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnChgWaveform,
																					this.label9});
			this.groupBox4.Location = new System.Drawing.Point(12, 256);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(360, 56);
			this.groupBox4.TabIndex = 7;
			this.groupBox4.TabStop = false;
			// 
			// btnChgWaveform
			// 
			this.btnChgWaveform.Location = new System.Drawing.Point(232, 16);
			this.btnChgWaveform.Name = "btnChgWaveform";
			this.btnChgWaveform.Size = new System.Drawing.Size(112, 32);
			this.btnChgWaveform.TabIndex = 6;
			this.btnChgWaveform.Text = "Jn(&S)";
			this.btnChgWaveform.Click += new System.EventHandler(this.btnChgWaveform_Click);
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(8, 24);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(144, 16);
			this.label9.TabIndex = 0;
			this.label9.Text = "g`̐ݒύX܂B";
			// 
			// groupBox5
			// 
			this.groupBox5.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.cboDelimBox});
			this.groupBox5.Location = new System.Drawing.Point(180, 27);
			this.groupBox5.Name = "groupBox5";
			this.groupBox5.Size = new System.Drawing.Size(192, 48);
			this.groupBox5.TabIndex = 8;
			this.groupBox5.TabStop = false;
			this.groupBox5.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(14, 18);
			this.cboDelimBox.Name = "cboDelimBox";
			this.cboDelimBox.Size = new System.Drawing.Size(165, 20);
			this.cboDelimBox.TabIndex = 0;
			this.cboDelimBox.SelectedIndexChanged += new System.EventHandler(this.cboDelimBox_SelectedIndexChanged);
			// 
			// btnEnd
			// 
			this.btnEnd.Location = new System.Drawing.Point(12, 320);
			this.btnEnd.Name = "btnEnd";
			this.btnEnd.Size = new System.Drawing.Size(360, 32);
			this.btnEnd.TabIndex = 9;
			this.btnEnd.Text = "I(&X)";
			this.btnEnd.Click += new System.EventHandler(this.btnEnd_Click);
			// 
			// TEKTRO
			// 
			this.ClientSize = new System.Drawing.Size(386, 364);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnEnd,
																		  this.groupBox5,
																		  this.groupBox4,
																		  this.label8,
																		  this.groupBox3,
																		  this.label6,
																		  this.groupBox2,
																		  this.label3,
																		  this.groupBox1,
																		  this.label1});
			this.Font = new System.Drawing.Font("lr oSVbN", 9F, 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 = "TEKTRO";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "TDS3000V[YTv";
			this.Load += new System.EventHandler(this.TEKTRO_Load);
			this.Closed += new System.EventHandler(this.TEKTRO_Closed);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.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 TEKTRO());
		}

		// TvN̏ ***************************************************************
		private void TEKTRO_Load(object sender, System.EventArgs e)
		{
			string	Addr = new string(' ', 8);
			uint	Loop;
	
			// PinnednhCmdɕz
			if (gChCmd.IsAllocated == false) {
				gChCmd = GCHandle.Alloc(SubFunc.Cmd, GCHandleType.Pinned);
			}
			// PinnednhFstbɕz
			if (gChFstb.IsAllocated == false) {
				gChFstb = GCHandle.Alloc(Fstb, GCHandleType.Pinned);
			}
			// PinnednhŃIuWFNg̃AhX擾
			SubFunc.pCmd = gChCmd.AddrOfPinnedObject();
			pFstb = gChFstb.AddrOfPinnedObject();

			Ret = SubFunc.GpibInit(out MsgText);							// GPIB̏܂B
			if (Ret > 3)
			{																// Retl0ȊOł΃G[
				Ret2 = SubFunc.CheckRet("GpinInit", (uint)Ret, out ErrText);
				MessageBox.Show(ErrText, null, MessageBoxButtons.OK);
			}

			// @ƃf~^ĂȂꍇ͐Ƀf[^Mł܂B
			// GpDelim(X, Y); X -> 0:Non/1:CR+LF/2:CR/3:LF , Y -> 0:Non/1:Use
			// ܂EOIȂǂŃf~^ݒ肵Ăꍇ̓f[^̐͑܂B
			Ret = (int)gpib.Delim(3, 1);									// f~^킹܂B
			Ret = (int)gpib.DevFind(pFstb);									// ڑĂ@{
			Ret =(int) gpib.Boardsts(0x08, out MyAddr);						// }CAhX擾

			for (Loop = 0; 30 >= Loop; Loop++)
			{																// Xg{bNX0`30ǉ
				if (MyAddr != Loop)
				{															// }CAhX폜
					Addr = string.Format("{0:d}", Loop);					// lɕϊ
					cboDevice.Items.Add(Addr);
				}
			}

			cboDelimBox.SelectedIndex = 1;

			if (Fstb[0] == 0xFF)
			{
				cboDevice.SelectedIndex = 0;
			}
			else
			{
				cboDevice.Text = Fstb[0].ToString();						// @̐ݒ
			}

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

		private void TEKTRO_Closed(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();												// ExtGpib֐(Tu[`)R[Ă܂B

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

		// XP[ݒ vO //////////////////////////////////////////////////////////
		private void btnAutoSet_Click(object sender, System.EventArgs e)
		{
			StringBuilder	ReadVolts = new StringBuilder(10000);			// d̕ϐ
			StringBuilder	ReadHoriz = new StringBuilder(10000);			// XP[̕ϐ
			//string	GetStr = "";										// 擾
			uint	Device;													// @

			try
			{
				Device = Convert.ToUInt32(cboDevice.Text);					// @擾܂B
			}
			catch (System.FormatException)
			{
				Device = 0;
			}
			if (Device != Fstb[0])
			{
				Fstb[0] = Device;
			}

			Ret = SubFunc.GpibPrint(Fstb[0], "CLEARMENU");					// eNg̃j[NA܂B
			if (Ret == 1)
			{
				return;
			}
			Ret = SubFunc.GpibPrint(Fstb[0], "FACTORY");					// Ho׎̏Ԃɖ߂܂B
			if (Ret == 1)
			{
				return;
			}
			Ret = SubFunc.GpibPrint(Fstb[0], "LOCK ALL");
			if (Ret == 1)
			{
				return;
			}
			MessageBox.Show("`lP̃v[uɐڑĂM␮܂B", "s", MessageBoxButtons.OK);

			SubFunc.WaitOPC(Fstb[0]);										// Ƃ`FbN

			Ret = SubFunc.GpibPrint(Fstb[0], "HEADER 0");					// wb_tȂݒɂ܂B
			if (Ret == 1)
			{
				return;
			}

			Ret = SubFunc.GpibPrint(Fstb[0], "AUTOSET EXECUTE");			// ݒ̎s܂B
			if (Ret == 1)
			{
				return;
			}
			MessageBox.Show("`lP WaveForm ݒ肵܂B", "TDS3000 -> s", MessageBoxButtons.OK);

			Ret = SubFunc.GpibPrint(Fstb[0], "CH1:SCALE?");					// `lP̃XP[mF܂B
			if (Ret == 1)
			{
				return;
			}
			Ret = SubFunc.GpibInput(Fstb[0], ReadVolts);					// ϐReadVoltsɎ荞݂܂B
			if (Ret == 1)
			{
				return;
			}
			Ret = SubFunc.GpibPrint(Fstb[0], "HORIZONTAL:MAIN:SCALE?");		// {ƂȂ鎞Ԏ̋敪XP[mF܂B
			if (Ret == 1)
			{
				return;
			}
			Ret = SubFunc.GpibInput(Fstb[0], ReadHoriz);					// ϐReadHorizɎ荞݂܂B
			if (Ret == 1)
			{
				return;
			}

			txtVolts.Text = ReadVolts.ToString();							// d\܂B
			txtSec.Text = ReadHoriz.ToString();								// ̃XP[\܂B

			Ret = SubFunc.GpibPrint(Fstb[0], "UNLOCK ALL");					// bN܂B
			if (Ret == 1)
			{
				return;
			}
		}

		// g`f[^t@CɃZ[u܂B//////////////////////////////////////////////////
		private void btnSetWaveform_Click(object sender, System.EventArgs e)
		{
			string			DATSRC = "", StrData = "";						// gpCH, Mf[^
			StringBuilder	RD = new StringBuilder(10000);					// f[^Mp
			string			SendData = "";				
			StringBuilder	YUNIT = new StringBuilder(10000);				// YP
			StringBuilder	XUNIT = new StringBuilder(10000);				// XP
			string			FileName = "", WfmData = ""/*, Kannma = ","*/;	// Z[u, g`f[^, Z[u񎞎gp
			string			zeroStr = new String('0', 1024);
			StringBuilder	RecvData = new StringBuilder(zeroStr, 0, 1024, 1024);	// GpListenB()ł̎Mf[^
			int[]			IntData = new int[1024];						// Int^f[^
			int				DataMax, DSTART, DSTOP;							// őf[^, Jn|Cg, I|Cg
			float[]			TIMEARR = new float[1024];						// 
			float[]			VOLTARR = new float[1024];						// d
			float			YOFF, YMULT, PtOff, XINCR;						// IVf[^p
			TextWriter		m_repFile;										// t@C֘Ap

			Fstb[0] = uint.Parse(cboDevice.Text);							// L@AhX

			//	Ret = SubFunc.GpibInit(out ErrText);						// ĂȂƂ͎sĉB
			//	Ret = gpib.Delim(3, 1);										// ĂȂƂ͎sĉB
			Ret = SubFunc.GpibPrint(Fstb[0], "HEADER OFF");					// Mf[^Ƀwb_tȂ悤ɂ܂B
			if (Ret == 1)
			{
				return;
			}

			DATSRC = "CH1";
			SendData = "DATA:SOURCE " + DATSRC;
			Ret = SubFunc.GpibPrint(Fstb[0], SendData);						// CH1gp܂B
			if (Ret == 1)
			{
				return;
			}
			Ret = SubFunc.GpibPrint(Fstb[0], "DATA:ENCDG RIBINARY;WIDTH 1");// f[^-128`127AoCi
			if (Ret == 1)
			{
				return;
			}
			DataMax = 500;

			SendData = "HORIZONTAL:RECORDLENGTH " + DataMax.ToString();
			Ret = SubFunc.GpibPrint(Fstb[0], SendData);						// f[^500ɂ܂B
			if (Ret == 1)
			{
				return;
			}

			DSTART = 1; DSTOP = DataMax;

			StrData = "DATA:START " + DSTART.ToString();
			Ret = SubFunc.GpibPrint(Fstb[0], StrData);						// X^[g1|Cg
			if (Ret == 1)
			{
				return;
			}

			StrData = "DATA:STOP "+ DSTOP.ToString();
			Ret = SubFunc.GpibPrint(Fstb[0], StrData);						// I500|Cg
			if (Ret == 1)
			{
				return;
			}

			StrData = "HEADER OFF";
			Ret = SubFunc.GpibPrint(Fstb[0], StrData);
			if (Ret == 1)
			{
				return;
			}

			Ret = SubFunc.GpibPrint(Fstb[0], "ACQUIRE:STATE RUN");			// g` f[^̊ln߂ƓɁAlJEgԍ0ɃZbg܂B
			if (Ret == 1)
			{
				return;
			}
			SubFunc.WaitOPC(Fstb[0]);										// Ƃ`FbN
			Ret = SubFunc.GpibPrint(Fstb[0], "CURVE?");						// g`̃f[^TDS3000Ŏ擾܂B
			if (Ret == 1)
			{
				return;
			}

			Ret = SubFunc.GpibInputB(Fstb[0], RecvData);
			if (Ret == 1)
			{
				return;
			}

			for (i = 0; DataMax >= i; i++)
			{																// char^Int^ɕϊ܂B
				IntData[i] = (int)RecvData[i];
				if (IntData[i] > 128)
				{
					IntData[i] = -(256 - IntData[i]);
				}
			}

			StrData = "WFMPRE:" + DATSRC + ":YOFF?";
			Ret = SubFunc.GpibPrint(Fstb[0], StrData);
			if (Ret == 1)
			{
				return;
			}
			Ret = SubFunc.GpibInput(Fstb[0], RD);
			if (Ret == 1)
			{
				return;
			}
			try
			{
				YOFF = float.Parse(RD.ToString());
			}
			catch (System.FormatException)
			{
				YOFF = 0;
			}

			StrData = "WFMPRE:" + DATSRC + ":YMULT?";
			Ret = SubFunc.GpibPrint(Fstb[0], StrData);
			if (Ret == 1)
			{
				return;
			}
			Ret = SubFunc.GpibInput(Fstb[0], RD);
			if (Ret == 1)
			{
				return;
			}
			try
			{
				YMULT = float.Parse(RD.ToString());
			}
			catch (System.FormatException)
			{
				YMULT = 0;
			}

			StrData = "WFMPRE:" + DATSRC + ":YUNIT?";
			Ret = SubFunc.GpibPrint(Fstb[0], StrData);
			if (Ret == 1)
			{
				return;
			}
			Ret = SubFunc.GpibInput(Fstb[0], YUNIT);
			if (Ret == 1)
			{
				return;
			}

			StrData = "WFMPRE:" + DATSRC + ":PT_Off?";
			Ret = SubFunc.GpibPrint(Fstb[0], StrData);
			if (Ret == 1)
			{
				return;
			}
			Ret = SubFunc.GpibInput(Fstb[0], RD);
			if (Ret == 1)
			{
				return;
			}
			try
			{
				PtOff = float.Parse(RD.ToString());
			}
			catch (System.FormatException)
			{
				PtOff = 0;
			}

			StrData = "WFMPRE:" + DATSRC + ":XINCR?";
			Ret = SubFunc.GpibPrint(Fstb[0], StrData);
			if (Ret == 1)
			{
				return;
			}
			Ret = SubFunc.GpibInput(Fstb[0], RD);
			if (Ret == 1)
			{
				return;
			}
			try
			{
				XINCR = float.Parse(RD.ToString());
			}
			catch (System.FormatException)
			{
				XINCR = 0;
			}

			StrData = "WFMPRE:" + DATSRC + ":XUNIT?";
			Ret = SubFunc.GpibPrint(Fstb[0], StrData);
			if (Ret == 1)
			{
				return;
			}
			Ret = SubFunc.GpibInput(Fstb[0], XUNIT);
			if (Ret == 1)
			{
				return;
			}

			FileName = ".\\WFM_DATA.csv";									// Z[u

			try
			{
				m_repFile = File.CreateText(FileName);
			}
			catch (System.IO.IOException)
			{
				MessageBox.Show("t@CI[vĂ܂B", "t@CI[vs", MessageBoxButtons.OK);
				return;
			}

			for (i = 0; (DataMax - 1) >= i; i++)
			{
				TIMEARR[i] = (i - PtOff) * XINCR;
				VOLTARR[i] = (IntData[i] - YOFF) * YMULT;
				WfmData = string.Format("{0:f6},{1:f6}", TIMEARR[i], VOLTARR[i]);
				m_repFile.WriteLine(WfmData);								// t@Cɏ
			}
			m_repFile.Close();
			MessageBox.Show("JghCuɕۑ܂B", "t@C̕ۑ", MessageBoxButtons.OK);
		}

		// g`f[^ω܂B////////////////////////////////////////////////////////////
		private void btnChgWaveform_Click(object sender, System.EventArgs e)
		{
			string	SendData = "";
			string	MEASSRC = "CH1", MEASTYPE = "AMPLITUDE";

			Fstb[0] = uint.Parse(cboDevice.Text);

			MessageBox.Show(" CH1ɍޗƂMv[uɐڑĉB", "v[u̐ڑmF", MessageBoxButtons.OK);

			Ret = SubFunc.GpibPrint(Fstb[0], "HEADER OFF");					// 擾f[^Ƀwb_tȂݒɂ܂B
			if (Ret == 1)
			{
				return;
			}

			SendData = ":CH1:VOLTS 2E-1;POSITION 0;OFFSET 0;COUPLING AC";	// ʂ̉ݒ肵܂B
			Ret = SubFunc.GpibPrint(Fstb[0], SendData);
			if (Ret == 1)
			{
				return;
			}
			SendData = ":CH1:IMPEDANCE MEG;BANDWIDTH FULL";
			Ret = SubFunc.GpibPrint(Fstb[0], SendData);
			if (Ret == 1)
			{
				return;
			}
			SendData = ":HORIZONTAL:MAIN:SCALE 2E-2";
			Ret = SubFunc.GpibPrint(Fstb[0], SendData);
			if (Ret == 1)
			{
				return;
			}
			SendData = ":SELECT:CH1 1";
			Ret = SubFunc.GpibPrint(Fstb[0], SendData);
			if (Ret == 1)
			{
				return;
			}
			// CH1ɑ肵@ZbgĉB
			// ܂Aʂ̔g`ƑlZbg܂B
			SendData = ":MEASUREMENT:MEAS1:SOURCE1 " + MEASSRC + ";TYPE " + MEASTYPE + ";STATE 1";
			Ret = SubFunc.GpibPrint(Fstb[0], SendData);
			if (Ret == 1)
			{
				return;
			}

			SubFunc.WaitOPC(Fstb[0]);										// Ƃ`FbN

			MessageBox.Show("XR[vCH1̐U\悤ɐݒ肵܂B", "ʒm", MessageBoxButtons.OK);

			Ret = SubFunc.GpibPrint(Fstb[0], "UNLOCK ALL");
			if (Ret == 1)
			{
				return;
			}
		}

		// vȌI /////////////////////////////////////////////////////////////////////
		private void btnEnd_Click(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();												// ExtGpib֐(Tu[`)R[Ă܂B
			this.Close();													// _CAO܂B(_CAO̔j)	
		}

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