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 Oscillo
{
	/// <summary>
	/// Form1 ̊Tv̐łB
	/// </summary>
	public class Oscillo : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox2;
		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.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.ComboBox cboYrAdrBox;
		private System.Windows.Forms.RadioButton rdoBinaryBn;
		private System.Windows.Forms.RadioButton rdoAsciiBn;
		private System.Windows.Forms.Button btnInit;
		private System.Windows.Forms.Button btnSend;
		private System.Windows.Forms.Button btnReceive;
		private System.Windows.Forms.Button btnExt;
		private System.Windows.Forms.ComboBox cboListBox;
		private System.Windows.Forms.TextBox txtSendData;
		private System.Windows.Forms.TextBox txtReceiveData;
		private System.Windows.Forms.TextBox txtRet;
		private System.Windows.Forms.ComboBox cboDelimBox;
		private System.Windows.Forms.Button btnDelData;
		private System.Windows.Forms.Button btnDelRet;
		private System.Windows.Forms.PictureBox pictureBox;
		/// <summary>
		/// KvȃfUCiϐłB
		/// </summary>
		private System.ComponentModel.Container components = null;

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

		// ϐAz̐錾 ///////////////////////////////////////////////////////////
		string			szBuffer = new string(' ', 256);		// eLXg̃obt@
		uint			Ret;									// ߂l
		int				Count/*,Stppu*/;						// 񐔤PPUݒ
		int[]			Num_Buf = new int[4096];				// IVf[^󂯎邽߂̃obt@
		uint			Myadr,Yradr;							// }CAhX,@AhX
		uint			Delim,Eoi;								// f~^
		//uint			PDataLine;								// p|[߂l(DIOC܂)
		uint[]			Pstb = new uint[32];					// Xe[^XoCg̔z
		uint[]			Pstb2 = new uint[32];					// Xe[^XoCg̔z
		uint			Srlen;									// ̒
		string			srBuffer = new string(' ', 50000);		// Mf[^̃obt@
		string			csBuf;									// eLXg\
		StringBuilder	rvBuffer = new StringBuilder(50000);	// Mf[^̃obt@
		GCHandle		gChCmd;									// CmdzpGCHandle
		/////////////////////////////////////////////////////////////////////////////

		public Oscillo()
		{
			//
			// 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.label1 = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.rdoAsciiBn = new System.Windows.Forms.RadioButton();
			this.rdoBinaryBn = new System.Windows.Forms.RadioButton();
			this.btnInit = new System.Windows.Forms.Button();
			this.btnSend = new System.Windows.Forms.Button();
			this.btnReceive = new System.Windows.Forms.Button();
			this.btnExt = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.cboListBox = new System.Windows.Forms.ComboBox();
			this.txtSendData = new System.Windows.Forms.TextBox();
			this.txtReceiveData = new System.Windows.Forms.TextBox();
			this.txtRet = new System.Windows.Forms.TextBox();
			this.btnDelData = new System.Windows.Forms.Button();
			this.btnDelRet = new System.Windows.Forms.Button();
			this.cboDelimBox = new System.Windows.Forms.ComboBox();
			this.label7 = new System.Windows.Forms.Label();
			this.pictureBox = new System.Windows.Forms.PictureBox();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.cboYrAdrBox,
																					this.label1});
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(240, 54);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "^[Qbg";
			// 
			// 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, 20);
			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(16, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(160, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "@AhX =";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.rdoAsciiBn,
																					this.rdoBinaryBn});
			this.groupBox2.Location = new System.Drawing.Point(246, 8);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(330, 54);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Mf[^^Cv";
			// 
			// rdoAsciiBn
			// 
			this.rdoAsciiBn.Location = new System.Drawing.Point(184, 20);
			this.rdoAsciiBn.Name = "rdoAsciiBn";
			this.rdoAsciiBn.Size = new System.Drawing.Size(96, 24);
			this.rdoAsciiBn.TabIndex = 1;
			this.rdoAsciiBn.Text = "AXL[";
			this.rdoAsciiBn.CheckedChanged += new System.EventHandler(this.rdoAsciiBn_CheckedChanged);
			// 
			// rdoBinaryBn
			// 
			this.rdoBinaryBn.Location = new System.Drawing.Point(48, 20);
			this.rdoBinaryBn.Name = "rdoBinaryBn";
			this.rdoBinaryBn.Size = new System.Drawing.Size(112, 24);
			this.rdoBinaryBn.TabIndex = 0;
			this.rdoBinaryBn.Text = "oCi[";
			this.rdoBinaryBn.CheckedChanged += new System.EventHandler(this.rdoBinaryBn_CheckedChanged);
			// 
			// btnInit
			// 
			this.btnInit.Location = new System.Drawing.Point(454, 74);
			this.btnInit.Name = "btnInit";
			this.btnInit.Size = new System.Drawing.Size(122, 24);
			this.btnInit.TabIndex = 2;
			this.btnInit.Text = "(&I)";
			this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
			// 
			// btnSend
			// 
			this.btnSend.Location = new System.Drawing.Point(454, 104);
			this.btnSend.Name = "btnSend";
			this.btnSend.Size = new System.Drawing.Size(122, 24);
			this.btnSend.TabIndex = 3;
			this.btnSend.Text = "f[^M(&S)";
			this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			// 
			// btnReceive
			// 
			this.btnReceive.Location = new System.Drawing.Point(454, 134);
			this.btnReceive.Name = "btnReceive";
			this.btnReceive.Size = new System.Drawing.Size(122, 24);
			this.btnReceive.TabIndex = 4;
			this.btnReceive.Text = "f[^M(&R)";
			this.btnReceive.Click += new System.EventHandler(this.btnReceive_Click);
			// 
			// btnExt
			// 
			this.btnExt.Location = new System.Drawing.Point(454, 164);
			this.btnExt.Name = "btnExt";
			this.btnExt.Size = new System.Drawing.Size(122, 24);
			this.btnExt.TabIndex = 5;
			this.btnExt.Text = "I(&X)";
			this.btnExt.Click += new System.EventHandler(this.btnExt_Click);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 78);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(112, 16);
			this.label2.TabIndex = 6;
			this.label2.Text = "IV^Cv";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 108);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(88, 16);
			this.label3.TabIndex = 7;
			this.label3.Text = "Mf[^";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 138);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(88, 16);
			this.label4.TabIndex = 8;
			this.label4.Text = "Mf[^";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(8, 168);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(88, 16);
			this.label5.TabIndex = 9;
			this.label5.Text = "߂l";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(8, 198);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(80, 16);
			this.label6.TabIndex = 10;
			this.label6.Text = "f~^";
			// 
			// cboListBox
			// 
			this.cboListBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboListBox.Location = new System.Drawing.Point(112, 74);
			this.cboListBox.Name = "cboListBox";
			this.cboListBox.Size = new System.Drawing.Size(334, 24);
			this.cboListBox.TabIndex = 11;
			// 
			// txtSendData
			// 
			this.txtSendData.Location = new System.Drawing.Point(112, 105);
			this.txtSendData.Name = "txtSendData";
			this.txtSendData.Size = new System.Drawing.Size(334, 23);
			this.txtSendData.TabIndex = 12;
			this.txtSendData.Text = "";
			// 
			// txtReceiveData
			// 
			this.txtReceiveData.Location = new System.Drawing.Point(112, 135);
			this.txtReceiveData.Name = "txtReceiveData";
			this.txtReceiveData.Size = new System.Drawing.Size(334, 23);
			this.txtReceiveData.TabIndex = 13;
			this.txtReceiveData.Text = "";
			// 
			// txtRet
			// 
			this.txtRet.Location = new System.Drawing.Point(112, 165);
			this.txtRet.Name = "txtRet";
			this.txtRet.Size = new System.Drawing.Size(334, 23);
			this.txtRet.TabIndex = 14;
			this.txtRet.Text = "";
			// 
			// btnDelData
			// 
			this.btnDelData.Location = new System.Drawing.Point(327, 194);
			this.btnDelData.Name = "btnDelData";
			this.btnDelData.Size = new System.Drawing.Size(120, 24);
			this.btnDelData.TabIndex = 16;
			this.btnDelData.Text = "f[^(&D)";
			this.btnDelData.Click += new System.EventHandler(this.btnDelData_Click);
			// 
			// btnDelRet
			// 
			this.btnDelRet.Location = new System.Drawing.Point(454, 194);
			this.btnDelRet.Name = "btnDelRet";
			this.btnDelRet.Size = new System.Drawing.Size(122, 24);
			this.btnDelRet.TabIndex = 17;
			this.btnDelRet.Text = "߂l(&T)";
			this.btnDelRet.Click += new System.EventHandler(this.btnDelRet_Click);
			// 
			// 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(112, 192);
			this.cboDelimBox.Name = "cboDelimBox";
			this.cboDelimBox.Size = new System.Drawing.Size(80, 24);
			this.cboDelimBox.TabIndex = 18;
			this.cboDelimBox.SelectedIndexChanged += new System.EventHandler(this.cboDelimBox_SelectedIndexChanged);
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(513, 232);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(58, 16);
			this.label7.TabIndex = 19;
			this.label7.Text = "Ot";
			// 
			// pictureBox
			// 
			this.pictureBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.pictureBox.Location = new System.Drawing.Point(8, 232);
			this.pictureBox.Name = "pictureBox";
			this.pictureBox.Size = new System.Drawing.Size(504, 136);
			this.pictureBox.TabIndex = 20;
			this.pictureBox.TabStop = false;
			// 
			// Oscillo
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(8, 16);
			this.ClientSize = new System.Drawing.Size(586, 378);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.pictureBox,
																		  this.label7,
																		  this.cboDelimBox,
																		  this.btnDelRet,
																		  this.btnDelData,
																		  this.txtRet,
																		  this.txtReceiveData,
																		  this.txtSendData,
																		  this.cboListBox,
																		  this.label6,
																		  this.label5,
																		  this.label4,
																		  this.label3,
																		  this.label2,
																		  this.btnExt,
																		  this.btnReceive,
																		  this.btnSend,
																		  this.btnInit,
																		  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 = "Oscillo";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "IV";
			this.Load += new System.EventHandler(this.Oscillo_Load);
			this.Closed += new System.EventHandler(this.Oscillo_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 Oscillo());
		}

		private void Oscillo_Load(object sender, System.EventArgs e)
		{
			// [ݒ]
			cboYrAdrBox.Text = "0";
			rdoBinaryBn.Checked = true;
	
			cboListBox.Items.Add("[ Tektronics 2440 Oscilloscope ]");
			cboListBox.SelectedIndex = 0; 									// ݒ
			srBuffer = "DATA ENCDG:RPBINARY;CURVE?";
			txtSendData.Text = srBuffer;

			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 Oscillo_Closed(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();												// ExtGpib֐(Tu[`)R[Ă܂B

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

		// [[`] ///////////////////////////////////////////////////////////
		private void btnInit_Click(object sender, System.EventArgs e)
		{
			string	DelMyadr = new string(' ', 16);
	
			SubFunc.GpibInit(out csBuf);									// Init֐(Tu[`)R[Ă܂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;
				}
			}
			cboListBox.SelectedIndex = 0; 									// ݒ
			txtRet.Text = csBuf;											// _CAOɕ\

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

		// [M[`] /////////////////////////////////////////////////////////////
		private void btnSend_Click(object sender, System.EventArgs e)
		{
			srBuffer = txtSendData.Text;
			Srlen = (uint)srBuffer.Length;

			if (Srlen == 0x0)
			{
				txtRet.Text = "Mf[^܂B";
				return;
			}
			Yradr = Convert.ToUInt32(cboYrAdrBox.Text); 
			SubFunc.Cmd[0] = 2;												// R}h
			SubFunc.Cmd[1] = Myadr;											// g[JAhX
			SubFunc.Cmd[2] = Yradr;											// XiAhX
			Ret = gpib.Talk(SubFunc.pCmd, Srlen, srBuffer);					// M
			if (Ret !=0)
			{
				SubFunc.CheckRet("GpTalk", Ret, out csBuf);
				txtRet.Text = csBuf;
			}
			else
			{
				txtRet.Text = "MI܂BM{^ĂB";
			}
		}

		// [M[`] /////////////////////////////////////////////////////////////
		private void btnReceive_Click(object sender, System.EventArgs e)
		{
			int		rvBufferVal;
			string	rvbuf2str;

			Yradr = Convert.ToUInt32(cboYrAdrBox.Text); 
			SubFunc.Cmd[0] = 2;												// R}h
			SubFunc.Cmd[1] = Yradr;											// g[JAhX
			SubFunc.Cmd[2] = Myadr;											// XiAhX
			rvBuffer.Remove(0, rvBuffer.Length);							// Mobt@NA܂B
			Srlen = (uint)rvBuffer.Capacity;
			Ret = gpib.Listen(SubFunc.pCmd, ref Srlen, rvBuffer);
			if (SubFunc.CheckRet("GpListen", Ret, out csBuf) == 1)
			{
				txtRet.Text = csBuf;
				return;
			}
			szBuffer = string.Format("f[^MI܂B[ {0:d} ] oCg擾܂B", Srlen);
			txtRet.Text = szBuffer;
			SubFunc.Cmd[0] = 2;												// R}h
			SubFunc.Cmd[1] = 0x3f;											// AX(UNL)
			SubFunc.Cmd[2] = 0x5f;											// Ag[N(UNT)
			Ret = gpib.Comand(SubFunc.pCmd);								// R}h𑗐M
			if (Ret != 0)
			{
				SubFunc.CheckRet("GpComand", Ret, out csBuf);
				txtRet.Text = csBuf;
				return;
			}

			for (Count = 0; Count < 1024; Count++)
			{
				Num_Buf[Count] = 0;
			}
			if (rdoBinaryBn.Checked == true)
			{																// oCi[I
				txtReceiveData.Text = "";
				for (Count = 0; Count < 1024; Count++) 
				{															// f[^(0-255)-128128̃f[^ɕϊ
					try
					{
						rvbuf2str = rvBuffer.ToString();
						rvBufferVal = (int)rvbuf2str[10 + Count];
					}
					catch (System.IndexOutOfRangeException)
					{
						rvBufferVal = 0;
					}
					Num_Buf[Count] = rvBufferVal - 128;
				}
			} 
			else 
			{																// AXL[I
				txtReceiveData.Text = rvBuffer.ToString().Substring(0, (int)Srlen);
				if (Srlen >= 6)
				{
					string rvBuffer1 = rvBuffer.ToString().Substring(6);
					SubFunc.Str2Num(rvBuffer1, Srlen-6, ref Num_Buf, 1024);	// ̃obt@
				}
			}
			SubFunc.DrawGraph(pictureBox, Num_Buf, 1024, -128, 128);		// DrawGraph֐R[
		}

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

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

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

		// [f[^^CvAXL[ɕύX] /////////////////////////////////////////////
		private void rdoAsciiBn_CheckedChanged(object sender, System.EventArgs e)
		{
			txtSendData.Text = "DATA ENCDG:ASCII;CURVE?";
		}

		// [f[^^CvoCi[ɕύX] ///////////////////////////////////////////
		private void rdoBinaryBn_CheckedChanged(object sender, System.EventArgs e)
		{
			txtSendData.Text = "DATA ENCDG:RPBINARY;CURVE?";
		}

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

		private void cboYrAdrBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Yradr = uint.Parse(cboYrAdrBox.Text);
		}
	}
}
