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

namespace MltLine
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MltLine : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.GroupBox groupBox5;
		private System.Windows.Forms.ComboBox cboYrAdrBox;
		private System.Windows.Forms.Button btnGTL;
		private System.Windows.Forms.Button btnSDC;
		private System.Windows.Forms.Button btnGET;
		private System.Windows.Forms.Button btnInit;
		private System.Windows.Forms.Button btnLLO;
		private System.Windows.Forms.Button btnDCL;
		private System.Windows.Forms.Button btnUNT;
		private System.Windows.Forms.Button btnUNL;
		private System.Windows.Forms.TextBox txtMsg;
		private System.Windows.Forms.Button btnAllDel;
		private System.Windows.Forms.Button btnDel;
		private System.Windows.Forms.TextBox txtRet;
		private System.Windows.Forms.Button btnSend;
		private System.Windows.Forms.Button btnExt;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

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

		// Static //////////////////////////////////////////////////////////
		const int	GP_GTL = 0x01;
		const int	GP_SDC = 0x04;
		const int	GP_PPC = 0x05;
		const int	GP_GET = 0x08;
		const int	GP_TCT = 0x09;
		const int	GP_LLO = 0x11;
		const int	GP_DCL = 0x14;
		const int	GP_PPU = 0x15;
		const int	GP_SPE = 0x18;
		const int	GP_SPD = 0x19;
		const int	GP_MLA = 0x20;
		const int	GP_UNL = 0x3F;
		const int	GP_MTA = 0x40;
		const int	GP_UNT = 0x5F;
		// Setting /////////////////////////////////////////////////////////////
		uint		Myadr,Yradr;					// MyAddress,Address of device
		string		csBuf;							// Character string
		bool		bRet;							// BOOL value in AddComand function 
		int			CmdNum,CmdNumPos;				// Number of command,Position of command 
		uint		Ret;							// Return code
		int[]		CmdNumArray = new int[50];		// Command Array(2)
		string		DelMyadr = new string(' ', 8);	// For delete MyAddress in listbox
		GCHandle    gChCmd;                 		// GCHandle for cmd array
		/////////////////////////////////////////////////////////////////////////////

		public MltLine()
		{
			//
			// 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.groupBox3 = new System.Windows.Forms.GroupBox();
			this.btnUNL = new System.Windows.Forms.Button();
			this.btnUNT = new System.Windows.Forms.Button();
			this.btnDCL = new System.Windows.Forms.Button();
			this.btnLLO = new System.Windows.Forms.Button();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.btnGET = new System.Windows.Forms.Button();
			this.btnSDC = new System.Windows.Forms.Button();
			this.btnGTL = new System.Windows.Forms.Button();
			this.cboYrAdrBox = new System.Windows.Forms.ComboBox();
			this.label1 = new System.Windows.Forms.Label();
			this.btnInit = new System.Windows.Forms.Button();
			this.btnExt = new System.Windows.Forms.Button();
			this.groupBox4 = new System.Windows.Forms.GroupBox();
			this.btnSend = new System.Windows.Forms.Button();
			this.btnDel = new System.Windows.Forms.Button();
			this.btnAllDel = new System.Windows.Forms.Button();
			this.txtMsg = new System.Windows.Forms.TextBox();
			this.groupBox5 = new System.Windows.Forms.GroupBox();
			this.txtRet = new System.Windows.Forms.TextBox();
			this.groupBox1.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox4.SuspendLayout();
			this.groupBox5.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.groupBox3,
																					this.groupBox2});
			this.groupBox1.Location = new System.Drawing.Point(7, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(401, 122);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Command Editor";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnUNL,
																					this.btnUNT,
																					this.btnDCL,
																					this.btnLLO});
			this.groupBox3.Location = new System.Drawing.Point(235, 24);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(154, 88);
			this.groupBox3.TabIndex = 1;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Only Command";
			// 
			// btnUNL
			// 
			this.btnUNL.Location = new System.Drawing.Point(80, 53);
			this.btnUNL.Name = "btnUNL";
			this.btnUNL.Size = new System.Drawing.Size(66, 25);
			this.btnUNL.TabIndex = 6;
			this.btnUNL.Text = "U&NL";
			this.btnUNL.Click += new System.EventHandler(this.btnUNL_Click);
			// 
			// btnUNT
			// 
			this.btnUNT.Location = new System.Drawing.Point(8, 53);
			this.btnUNT.Name = "btnUNT";
			this.btnUNT.Size = new System.Drawing.Size(66, 25);
			this.btnUNT.TabIndex = 5;
			this.btnUNT.Text = "&UNT";
			this.btnUNT.Click += new System.EventHandler(this.btnUNT_Click);
			// 
			// btnDCL
			// 
			this.btnDCL.Location = new System.Drawing.Point(80, 20);
			this.btnDCL.Name = "btnDCL";
			this.btnDCL.Size = new System.Drawing.Size(66, 25);
			this.btnDCL.TabIndex = 4;
			this.btnDCL.Text = "&DCL";
			this.btnDCL.Click += new System.EventHandler(this.btnDCL_Click);
			// 
			// btnLLO
			// 
			this.btnLLO.Location = new System.Drawing.Point(8, 20);
			this.btnLLO.Name = "btnLLO";
			this.btnLLO.Size = new System.Drawing.Size(66, 25);
			this.btnLLO.TabIndex = 3;
			this.btnLLO.Text = "&LLO";
			this.btnLLO.Click += new System.EventHandler(this.btnLLO_Click);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnGET,
																					this.btnSDC,
																					this.btnGTL,
																					this.cboYrAdrBox,
																					this.label1});
			this.groupBox2.Location = new System.Drawing.Point(11, 24);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(226, 88);
			this.groupBox2.TabIndex = 0;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "With Address";
			// 
			// btnGET
			// 
			this.btnGET.Location = new System.Drawing.Point(152, 53);
			this.btnGET.Name = "btnGET";
			this.btnGET.Size = new System.Drawing.Size(66, 25);
			this.btnGET.TabIndex = 4;
			this.btnGET.Text = "G&ET";
			this.btnGET.Click += new System.EventHandler(this.btnGET_Click);
			// 
			// btnSDC
			// 
			this.btnSDC.Location = new System.Drawing.Point(80, 53);
			this.btnSDC.Name = "btnSDC";
			this.btnSDC.Size = new System.Drawing.Size(66, 25);
			this.btnSDC.TabIndex = 3;
			this.btnSDC.Text = "SD&C";
			this.btnSDC.Click += new System.EventHandler(this.btnSDC_Click);
			// 
			// btnGTL
			// 
			this.btnGTL.Location = new System.Drawing.Point(8, 53);
			this.btnGTL.Name = "btnGTL";
			this.btnGTL.Size = new System.Drawing.Size(66, 25);
			this.btnGTL.TabIndex = 2;
			this.btnGTL.Text = "&GTL";
			this.btnGTL.Click += new System.EventHandler(this.btnGTL_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(135, 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(8, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(128, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Address of device =";
			// 
			// btnInit
			// 
			this.btnInit.Location = new System.Drawing.Point(416, 32);
			this.btnInit.Name = "btnInit";
			this.btnInit.Size = new System.Drawing.Size(88, 28);
			this.btnInit.TabIndex = 1;
			this.btnInit.Text = "&Initialize";
			this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
			// 
			// btnExt
			// 
			this.btnExt.Location = new System.Drawing.Point(416, 72);
			this.btnExt.Name = "btnExt";
			this.btnExt.Size = new System.Drawing.Size(88, 28);
			this.btnExt.TabIndex = 2;
			this.btnExt.Text = "E&xit";
			this.btnExt.Click += new System.EventHandler(this.btnExt_Click);
			// 
			// groupBox4
			// 
			this.groupBox4.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnSend,
																					this.btnDel,
																					this.btnAllDel,
																					this.txtMsg});
			this.groupBox4.Location = new System.Drawing.Point(7, 137);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(497, 104);
			this.groupBox4.TabIndex = 3;
			this.groupBox4.TabStop = false;
			this.groupBox4.Text = "Output message (MAX=30)";
			// 
			// btnSend
			// 
			this.btnSend.Enabled = false;
			this.btnSend.Location = new System.Drawing.Point(420, 24);
			this.btnSend.Name = "btnSend";
			this.btnSend.Size = new System.Drawing.Size(64, 68);
			this.btnSend.TabIndex = 3;
			this.btnSend.Text = "&Send";
			this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			// 
			// btnDel
			// 
			this.btnDel.Location = new System.Drawing.Point(337, 62);
			this.btnDel.Name = "btnDel";
			this.btnDel.Size = new System.Drawing.Size(74, 30);
			this.btnDel.TabIndex = 2;
			this.btnDel.Text = "&One Del";
			this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
			// 
			// btnAllDel
			// 
			this.btnAllDel.Location = new System.Drawing.Point(337, 24);
			this.btnAllDel.Name = "btnAllDel";
			this.btnAllDel.Size = new System.Drawing.Size(74, 30);
			this.btnAllDel.TabIndex = 1;
			this.btnAllDel.Text = "&All Del";
			this.btnAllDel.Click += new System.EventHandler(this.btnAllDel_Click);
			// 
			// txtMsg
			// 
			this.txtMsg.Location = new System.Drawing.Point(11, 24);
			this.txtMsg.Multiline = true;
			this.txtMsg.Name = "txtMsg";
			this.txtMsg.Size = new System.Drawing.Size(317, 68);
			this.txtMsg.TabIndex = 0;
			this.txtMsg.Text = "";
			// 
			// groupBox5
			// 
			this.groupBox5.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtRet});
			this.groupBox5.Location = new System.Drawing.Point(7, 248);
			this.groupBox5.Name = "groupBox5";
			this.groupBox5.Size = new System.Drawing.Size(497, 58);
			this.groupBox5.TabIndex = 4;
			this.groupBox5.TabStop = false;
			this.groupBox5.Text = "Return value";
			// 
			// txtRet
			// 
			this.txtRet.Location = new System.Drawing.Point(8, 24);
			this.txtRet.Name = "txtRet";
			this.txtRet.Size = new System.Drawing.Size(476, 22);
			this.txtRet.TabIndex = 0;
			this.txtRet.Text = "";
			// 
			// MltLine
			// 
			this.ClientSize = new System.Drawing.Size(514, 314);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox5,
																		  this.groupBox4,
																		  this.btnExt,
																		  this.btnInit,
																		  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 = "MltLine";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "MultiLine Message";
			this.Load += new System.EventHandler(this.MltLine_Load);
			this.Closed += new System.EventHandler(this.MltLine_Closed);
			this.groupBox1.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox4.ResumeLayout(false);
			this.groupBox5.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MltLine());
		}

		private void MltLine_Load(object sender, System.EventArgs e)
		{
			// ****************************************************** Setting *** 
			CmdNum = 1;
			CmdNumPos = 0;
			cboYrAdrBox.Text = "1";
			// ******************************************************************

			// 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 MltLine_Closed(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();									// SubClass.h is called
			// GCHandle free
			if (gChCmd.IsAllocated == true) {
				gChCmd.Free();
			}
		}

		//////////////////////////// [DispCommandList] function ///
		private string DispCommandList()
		{
			uint	TxtRet;
			int		Count;										// For Text	of Return code
			string	Text = "", TmpString;						// Making message,All message

			for (Count = 1; Count < CmdNum; Count++)
			{
				TxtRet = SubFunc.Cmd[Count];				
				TmpString = string.Format(" [{0:x}H]", TxtRet); 
				Text = Text + TmpString;		
			}
			return	Text;
		}

		///////////////////////////////// [AddCommand] function ///
		private bool AddCommand(int CommandType, int CommandNum)
		{
			string	ErrMsg;										// For Error Message

			if (CmdNum > 30)
			{													// In terms of a message in the case of 30 or more
				ErrMsg = "Message is over!!";
				MessageBox.Show(ErrMsg);
				return false;
			}
			if (CommandNum == 2)
			{													// In case of necessary two message
				SubFunc.Cmd[CmdNum] = (Yradr | 0x20);			// Add Address of device to 0x20
				CmdNum = CmdNum + 1;
			}
			SubFunc.Cmd[CmdNum] = (uint)CommandType;
			CmdNum = CmdNum + 1;

			return true;										// A normal end
		}

		//////////////////////////////////////////// Initialize ///
		private void btnInit_Click(object sender, System.EventArgs e)
		{
			string	DelMyadr = new string(' ', 8);

			SubFunc.GpibInit(out csBuf);						// SubFunc.h is called
			gpib.Boardsts(0x08, out Myadr);						// My Address is acquired
			txtRet.Text = csBuf;
			btnSend.Enabled = true;
	
			DelMyadr = string.Format("{0:d}", Myadr);			// Show MyAddress
			Ret = (uint)cboYrAdrBox.FindStringExact(DelMyadr, -1);
			if (Ret >= 0)
			{													// Seaching MyAddress
				cboYrAdrBox.Items.Remove(Ret.ToString());		// Delete MyAddress 
				if (Myadr == Yradr)
				{
					cboYrAdrBox.SelectedIndex = 0;
				}
			}

			Yradr = Convert.ToUInt32(cboYrAdrBox.Text);
		}

		////////////////////////////////////////////////// Send ///
		private void btnSend_Click(object sender, System.EventArgs e)
		{
			SubFunc.Cmd[0] = (uint)CmdNum - 1;

			if (CmdNum <= 1)
			{
				csBuf = "There are not transmission data.";
			}
			else
			{
				Ret = gpib.Comand(SubFunc.pCmd);				// Send command
				SubFunc.CheckRet("GpComand", Ret, out csBuf);	// Error check
			}
			txtRet.Text = csBuf;	
		}

		////////////////////////////////////////////////// DCL ////
		private void btnDCL_Click(object sender, System.EventArgs e)
		{
			bRet = AddCommand(GP_DCL, 1);
			if (bRet == true)
			{
				CmdNumArray[CmdNumPos] = 1;
				CmdNumPos = CmdNumPos + 1;
				txtRet.Text = "";
			}
			txtMsg.Text = DispCommandList();
		}

		/////////////////////////////////////////////////// GET ///
		private void btnGET_Click(object sender, System.EventArgs e)
		{
			bRet = AddCommand(GP_GET, 2);
			if (bRet == true)
			{
				CmdNumArray[CmdNumPos] = 2;
				CmdNumPos = CmdNumPos + 1;
				txtRet.Text = "";
			}
			txtMsg.Text = DispCommandList();
		}

		/////////////////////////////////////////////////// GTL ///
		private void btnGTL_Click(object sender, System.EventArgs e)
		{
			bRet = AddCommand(GP_GTL, 2);						// AddCommand(Static, Command number)
			if (bRet == true)
			{
				CmdNumArray[CmdNumPos] = 2;
				CmdNumPos = CmdNumPos + 1;
				txtRet.Text = "";
			}
			txtMsg.Text = DispCommandList();
		}

		/////////////////////////////////////////////////// LLO ///
		private void btnLLO_Click(object sender, System.EventArgs e)
		{
			bRet = AddCommand(GP_LLO, 1);
			if (bRet == true)
			{
				CmdNumArray[CmdNumPos] = 1;
				CmdNumPos = CmdNumPos + 1;
				txtRet.Text = "";
			}
			txtMsg.Text = DispCommandList();
		}

		/////////////////////////////////////////////////// SDC ///
		private void btnSDC_Click(object sender, System.EventArgs e)
		{
			bRet = AddCommand(GP_SDC, 2);
			if (bRet == true)
			{
				CmdNumArray[CmdNumPos] = 2;
				CmdNumPos = CmdNumPos + 1;
				txtRet.Text = "";
			}
			txtMsg.Text = DispCommandList();
		}

		/////////////////////////////////////////////////// UNL ///
		private void btnUNL_Click(object sender, System.EventArgs e)
		{
			bRet = AddCommand(GP_UNL, 1);
			if (bRet == true)
			{
				CmdNumArray[CmdNumPos] = 1;
				CmdNumPos = CmdNumPos + 1;
				txtRet.Text = "";
			}
			txtMsg.Text = DispCommandList();
		}

		/////////////////////////////////////////////////// UNT ///
		private void btnUNT_Click(object sender, System.EventArgs e)
		{
			bRet = AddCommand(GP_UNT, 1);
			if (bRet == true)
			{
				CmdNumArray[CmdNumPos] = 1;
				CmdNumPos = CmdNumPos + 1;
				txtRet.Text = "";
			}
			txtMsg.Text = DispCommandList();
		}

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

		//////////////////////////////////////////// ALL DELETE ///
		private void btnAllDel_Click(object sender, System.EventArgs e)
		{
			CmdNum = 1;
			CmdNumPos = 0;

			txtMsg.Text = DispCommandList();
		}

		//////////////////////////////////////////// ONE DELETE ///
		private void btnDel_Click(object sender, System.EventArgs e)
		{
			if (CmdNumPos == 0)	
			{							// In case of Command is no
				txtRet.Text = "There is not a message.";
			} 
			else 
			{
				if (CmdNumArray[CmdNumPos - 1] == 1)
				{
					SubFunc.Cmd[CmdNum] = 0;
					CmdNum = CmdNum - 1;
				} 
				else 
				{
					SubFunc.Cmd[CmdNum] = 0;
					SubFunc.Cmd[CmdNum - 1] = 0;
					CmdNum = CmdNum - 2;
				}
				CmdNumArray[CmdNumPos - 1] = 0;
				CmdNumPos = CmdNumPos - 1;
				txtRet.Text = "";
			}
			txtMsg.Text = DispCommandList();
		}

		////////////////////////////////////////////////// Exit ///
		private void btnExt_Click(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();									// SubClass.h is called
			this.Close();										// End of program
		}
	}
}
