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>
	/// Summary description for Form1.
	/// </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.ComboBox cboYrAdrBox;
		private System.Windows.Forms.RadioButton rdoBinaryBn;
		private System.Windows.Forms.RadioButton rdoAsciiBn;
		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.ComboBox cboListBox;
		private System.Windows.Forms.TextBox txtSendData;
		private System.Windows.Forms.TextBox txtReceiveData;
		private System.Windows.Forms.TextBox txtRet;
		private System.Windows.Forms.Button btnInit;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.ComboBox cboDelimBox;
		private System.Windows.Forms.Button btnSend;
		private System.Windows.Forms.Button btnReceive;
		private System.Windows.Forms.Button btnDelRet;
		private System.Windows.Forms.Button btnDelData;
		private System.Windows.Forms.Button btnExt;
		private System.Windows.Forms.PictureBox pictureBox;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

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

		// Setting //////////////////////////////////////////////////////////
		string			szBuffer = new string(' ', 256);		// String buffer
		uint			Ret;									// Return code
		int				Count/*,Stppu*/;						// Count
		int[]			Num_Buf = new int[4096];				// Number buffer
		uint			Myadr,Yradr;							// MyAddress,Address of device
		uint			Delim, Eoi;								// Delimiter
		//uint			PDataLine;								// Parallel poll result
		uint[]			Pstb = new uint[32];					// Array of pollingStatus
		uint[]			Pstb2 = new uint[32];					// Array of pollingStatus
		uint			Srlen;									// Length of string
		string			srBuffer = new string(' ', 50000);		// Send buffer
		string			csBuf;									// String buffer
		StringBuilder	rvBuffer = new StringBuilder(50000);	// Receive buffer
		GCHandle     	gChCmd;                 				// GCHandle for cmd array
		/////////////////////////////////////////////////////////////////////

		public Oscillo()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </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.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.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.btnInit = new System.Windows.Forms.Button();
			this.label6 = new System.Windows.Forms.Label();
			this.cboDelimBox = new System.Windows.Forms.ComboBox();
			this.btnSend = new System.Windows.Forms.Button();
			this.btnReceive = new System.Windows.Forms.Button();
			this.btnDelRet = new System.Windows.Forms.Button();
			this.btnDelData = new System.Windows.Forms.Button();
			this.btnExt = new System.Windows.Forms.Button();
			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(9, 4);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(198, 48);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Address of device";
			// 
			// 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(102, 19);
			this.cboYrAdrBox.Name = "cboYrAdrBox";
			this.cboYrAdrBox.Size = new System.Drawing.Size(46, 24);
			this.cboYrAdrBox.TabIndex = 1;
			this.cboYrAdrBox.SelectedIndexChanged += new System.EventHandler(this.cboYrAdrBox_SelectedIndexChanged);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(33, 23);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(72, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Address =";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.rdoAsciiBn,
																					this.rdoBinaryBn});
			this.groupBox2.Location = new System.Drawing.Point(205, 4);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(198, 48);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Type of receive data";
			// 
			// rdoAsciiBn
			// 
			this.rdoAsciiBn.Location = new System.Drawing.Point(105, 19);
			this.rdoAsciiBn.Name = "rdoAsciiBn";
			this.rdoAsciiBn.Size = new System.Drawing.Size(64, 24);
			this.rdoAsciiBn.TabIndex = 1;
			this.rdoAsciiBn.Text = "&Ascii";
			this.rdoAsciiBn.CheckedChanged += new System.EventHandler(this.rdoAsciiBn_CheckedChanged);
			// 
			// rdoBinaryBn
			// 
			this.rdoBinaryBn.Location = new System.Drawing.Point(32, 19);
			this.rdoBinaryBn.Name = "rdoBinaryBn";
			this.rdoBinaryBn.Size = new System.Drawing.Size(64, 24);
			this.rdoBinaryBn.TabIndex = 0;
			this.rdoBinaryBn.Text = "&Binary";
			this.rdoBinaryBn.CheckedChanged += new System.EventHandler(this.rdoBinaryBn_CheckedChanged);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(9, 68);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(88, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "Oscillo Type";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(9, 100);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(88, 16);
			this.label3.TabIndex = 3;
			this.label3.Text = "Send Data";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(9, 131);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(88, 16);
			this.label4.TabIndex = 4;
			this.label4.Text = "Receive Data";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(9, 162);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(88, 16);
			this.label5.TabIndex = 5;
			this.label5.Text = "Return value";
			// 
			// cboListBox
			// 
			this.cboListBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboListBox.Location = new System.Drawing.Point(99, 64);
			this.cboListBox.Name = "cboListBox";
			this.cboListBox.Size = new System.Drawing.Size(304, 24);
			this.cboListBox.TabIndex = 6;
			// 
			// txtSendData
			// 
			this.txtSendData.Location = new System.Drawing.Point(99, 97);
			this.txtSendData.Name = "txtSendData";
			this.txtSendData.Size = new System.Drawing.Size(304, 22);
			this.txtSendData.TabIndex = 7;
			this.txtSendData.Text = "";
			// 
			// txtReceiveData
			// 
			this.txtReceiveData.Location = new System.Drawing.Point(99, 128);
			this.txtReceiveData.Name = "txtReceiveData";
			this.txtReceiveData.Size = new System.Drawing.Size(304, 22);
			this.txtReceiveData.TabIndex = 8;
			this.txtReceiveData.Text = "";
			// 
			// txtRet
			// 
			this.txtRet.Location = new System.Drawing.Point(99, 159);
			this.txtRet.Name = "txtRet";
			this.txtRet.Size = new System.Drawing.Size(304, 22);
			this.txtRet.TabIndex = 9;
			this.txtRet.Text = "";
			// 
			// btnInit
			// 
			this.btnInit.Location = new System.Drawing.Point(413, 9);
			this.btnInit.Name = "btnInit";
			this.btnInit.Size = new System.Drawing.Size(112, 24);
			this.btnInit.TabIndex = 10;
			this.btnInit.Text = "&Initialize";
			this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(413, 46);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(72, 16);
			this.label6.TabIndex = 11;
			this.label6.Text = "Delimiter";
			// 
			// cboDelimBox
			// 
			this.cboDelimBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboDelimBox.Items.AddRange(new object[] {
															 "Not Use",
															 "CR+LF",
															 "CR",
															 "LF"});
			this.cboDelimBox.Location = new System.Drawing.Point(413, 64);
			this.cboDelimBox.Name = "cboDelimBox";
			this.cboDelimBox.Size = new System.Drawing.Size(112, 24);
			this.cboDelimBox.TabIndex = 12;
			this.cboDelimBox.SelectedIndexChanged += new System.EventHandler(this.cboDelimBox_SelectedIndexChanged);
			// 
			// btnSend
			// 
			this.btnSend.Enabled = false;
			this.btnSend.Location = new System.Drawing.Point(413, 96);
			this.btnSend.Name = "btnSend";
			this.btnSend.Size = new System.Drawing.Size(112, 24);
			this.btnSend.TabIndex = 13;
			this.btnSend.Text = "&Send Data";
			this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			// 
			// btnReceive
			// 
			this.btnReceive.Enabled = false;
			this.btnReceive.Location = new System.Drawing.Point(413, 127);
			this.btnReceive.Name = "btnReceive";
			this.btnReceive.Size = new System.Drawing.Size(112, 24);
			this.btnReceive.TabIndex = 14;
			this.btnReceive.Text = "&Receive Data";
			this.btnReceive.Click += new System.EventHandler(this.btnReceive_Click);
			// 
			// btnDelRet
			// 
			this.btnDelRet.Location = new System.Drawing.Point(413, 158);
			this.btnDelRet.Name = "btnDelRet";
			this.btnDelRet.Size = new System.Drawing.Size(112, 24);
			this.btnDelRet.TabIndex = 15;
			this.btnDelRet.Text = "Del Retur&n";
			this.btnDelRet.Click += new System.EventHandler(this.btnDelRet_Click);
			// 
			// btnDelData
			// 
			this.btnDelData.Location = new System.Drawing.Point(413, 192);
			this.btnDelData.Name = "btnDelData";
			this.btnDelData.Size = new System.Drawing.Size(112, 24);
			this.btnDelData.TabIndex = 16;
			this.btnDelData.Text = "D&el Data";
			this.btnDelData.Click += new System.EventHandler(this.btnDelData_Click);
			// 
			// btnExt
			// 
			this.btnExt.Location = new System.Drawing.Point(413, 224);
			this.btnExt.Name = "btnExt";
			this.btnExt.Size = new System.Drawing.Size(112, 24);
			this.btnExt.TabIndex = 17;
			this.btnExt.Text = "E&xit";
			this.btnExt.Click += new System.EventHandler(this.btnExt_Click);
			// 
			// pictureBox
			// 
			this.pictureBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.pictureBox.Location = new System.Drawing.Point(9, 192);
			this.pictureBox.Name = "pictureBox";
			this.pictureBox.Size = new System.Drawing.Size(394, 160);
			this.pictureBox.TabIndex = 18;
			this.pictureBox.TabStop = false;
			// 
			// Oscillo
			// 
			this.ClientSize = new System.Drawing.Size(534, 360);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.pictureBox,
																		  this.btnExt,
																		  this.btnDelData,
																		  this.btnDelRet,
																		  this.btnReceive,
																		  this.btnSend,
																		  this.cboDelimBox,
																		  this.label6,
																		  this.btnInit,
																		  this.txtRet,
																		  this.txtReceiveData,
																		  this.txtSendData,
																		  this.cboListBox,
																		  this.label5,
																		  this.label4,
																		  this.label3,
																		  this.label2,
																		  this.groupBox2,
																		  this.groupBox1});
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			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 = "Oscillo Scope";
			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>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Oscillo());
		}

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

			Yradr = Convert.ToUInt32(cboYrAdrBox.Text);
			//*******************************************************************

			// Specify Pinned handle to Cmd
			if (gChCmd.IsAllocated == false) {
				gChCmd = GCHandle.Alloc(SubFunc.Cmd, GCHandleType.Pinned);
			}
			// Get address object by Pinned handle
			SubFunc.pCmd = gChCmd.AddrOfPinnedObject();
		}

		private void Oscillo_Closed(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();

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

		//////////////////////////////////////////// Initialize ///
		private void btnInit_Click(object sender, System.EventArgs e)
		{
			string	DelMyadr = new string(' ', 16);
	
			btnSend.Enabled = true;
			SubFunc.GpibInit(out csBuf);
			gpib.Boardsts(0x08, out Myadr);								// MyAddress is required
			DelMyadr = string.Format("{0:d}", Myadr);
			Ret = (uint)cboYrAdrBox.FindStringExact(DelMyadr, -1);
			if (Ret >= 0)
			{
				cboYrAdrBox.Items.Remove(Ret.ToString());
				if (Myadr == Yradr)
				{
					cboYrAdrBox.SelectedIndex = 0;
				}		
			}
			txtRet.Text = csBuf;

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

		////////////////////////////////////////////////// Send ///
		private void btnSend_Click(object sender, System.EventArgs e)
		{
			btnSend.Enabled = false;
			btnReceive.Enabled = true;

			srBuffer = txtSendData.Text;
			Srlen = (uint)srBuffer.Length;

			if (Srlen == 0x0)
			{
				txtRet.Text = "Data were transmitted.";
				return;
			}
			Yradr = Convert.ToUInt32(cboYrAdrBox.Text); 
			SubFunc.Cmd[0] = 2;											// MyAddress + Address of device
			SubFunc.Cmd[1] = Myadr;										// MyAddress
			SubFunc.Cmd[2] = Yradr;										// Address of device
			Ret = gpib.Talk(SubFunc.pCmd, Srlen, srBuffer);
			if (Ret != 0)
			{
				btnReceive.Enabled = false;
				SubFunc.CheckRet("GpTalk", Ret, out csBuf);
				txtRet.Text = csBuf;
			}
			else
			{
				txtRet.Text = "Data were transmitted.";
			}
		}

		/////////////////////////////////////////////// Receive ///
		private void btnReceive_Click(object sender, System.EventArgs e)
		{
			int		rvBufferVal;
			string	rvbuf2str;
			btnSend.Enabled = true;
			btnReceive.Enabled = false;

			Yradr = Convert.ToUInt32(cboYrAdrBox.Text);
			SubFunc.Cmd[0] = 2;
			SubFunc.Cmd[1] = Yradr;
			SubFunc.Cmd[2] = Myadr;
			rvBuffer.Remove(0, rvBuffer.Length);						// Clear receive buffer
			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("Data was received.Get [ {0:d} ] Byte.", Srlen);
			txtRet.Text = szBuffer;
			SubFunc.Cmd[0] = 2;											// Total command
			SubFunc.Cmd[1] = 0x3f;										// Unlisten(UNL)
			SubFunc.Cmd[2] = 0x5f;										// Untalken(UNT)
			Ret = gpib.Comand(SubFunc.pCmd);							// Send command
			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)
			{															// In case of BINARY
				txtReceiveData.Text = "";
				for (Count = 0; Count < 1024; Count++)
				{														// Data are changed on [0 to 256] from [-128 to 128].
					try
					{
						rvbuf2str = rvBuffer.ToString();
						rvBufferVal = (int)rvbuf2str[10 + Count];
					}
					catch (System.IndexOutOfRangeException)
					{
						rvBufferVal = 0;
					}
					Num_Buf[Count] = rvBufferVal - 128;
				}
			} 
			else 
			{
				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);
				}
			}
			SubFunc.DrawGraph(pictureBox, Num_Buf, 1024, -128, 128);	// Draw Graphic
		}

		///////////////////////////////////////// Delete Return ///
		private void btnDelRet_Click(object sender, System.EventArgs e)
		{
			txtRet.Text = "";
		}

		/////////////////////////////////////////// Delete Data ///
		private void btnDelData_Click(object sender, System.EventArgs e)
		{
			txtReceiveData.Text = "";
		}

		////////////////////////////////////////// ASCII Select ///
		private void rdoAsciiBn_CheckedChanged(object sender, System.EventArgs e)
		{
			txtSendData.Text = "DATA ENCDG:ASCII;CURVE?";
		}

		///////////////////////////////////////// BINARY Select /// 
		private void rdoBinaryBn_CheckedChanged(object sender, System.EventArgs e)
		{
			txtSendData.Text = "DATA ENCDG:RPBINARY;CURVE?";
		}

		////////////////////////////// Change Address of device ///
		private void cboYrAdrBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Yradr = Convert.ToUInt32(cboYrAdrBox.Text);
		}

		////////////////////////////////////////////////// Exit ///
		private void btnExt_Click(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();
			this.Close();
		}

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