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 VoltCtrl
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class VoltCtrl : 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.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.ComboBox cboTargetName;
		private System.Windows.Forms.ComboBox cboYrAdrBox;
		private System.Windows.Forms.TextBox txtSendData;
		private System.Windows.Forms.TextBox txtRet;
		private System.Windows.Forms.Button btnDelRet;
		private System.Windows.Forms.Button btnDataStart;
		private System.Windows.Forms.Button btnDataStop;
		private System.Windows.Forms.Button btnInit;
		private System.Windows.Forms.ComboBox cboDelimBox;
		private System.Windows.Forms.Button btnYrDeviceIni;
		private System.Windows.Forms.Button btnExt;
		private System.Windows.Forms.Timer timer; 
		private System.ComponentModel.IContainer components;		

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

		///////////////////////////////////////////////////////// Setting ///
		string		szBuffer = new string(' ', 1024);
		uint		Ret;
		//int		Count;
		//int		SetT;
		uint		Yradr, Myadr;
		uint		Delim, Eoi;
		uint		DvsCnt;
		uint		Dmamode = 0, Srlen;
		//uint		P_stb, P_eoi;
		uint[]		Pstb = new uint[32];
		string		srBuffer = new string(' ', 10000);
		string		csBuf;
		GCHandle    gChCmd;                 				// GCHandle for cmd array						
		/////////////////////////////////////////////////////////////////////

		public VoltCtrl()
		{
			//
			// 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.components = new System.ComponentModel.Container();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.cboTargetName = new System.Windows.Forms.ComboBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.cboYrAdrBox = new System.Windows.Forms.ComboBox();
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.btnDataStop = new System.Windows.Forms.Button();
			this.btnDataStart = new System.Windows.Forms.Button();
			this.btnDelRet = new System.Windows.Forms.Button();
			this.txtRet = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.txtSendData = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.btnInit = new System.Windows.Forms.Button();
			this.label4 = new System.Windows.Forms.Label();
			this.cboDelimBox = new System.Windows.Forms.ComboBox();
			this.btnYrDeviceIni = new System.Windows.Forms.Button();
			this.btnExt = new System.Windows.Forms.Button();
			this.timer = new System.Windows.Forms.Timer(this.components);
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.cboTargetName});
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(360, 58);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Device name";
			// 
			// cboTargetName
			// 
			this.cboTargetName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboTargetName.Location = new System.Drawing.Point(8, 24);
			this.cboTargetName.Name = "cboTargetName";
			this.cboTargetName.Size = new System.Drawing.Size(344, 24);
			this.cboTargetName.TabIndex = 0;
			this.cboTargetName.SelectedIndexChanged += new System.EventHandler(this.cboTargetName_SelectedIndexChanged);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.cboYrAdrBox,
																					this.label1});
			this.groupBox2.Location = new System.Drawing.Point(366, 8);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(170, 58);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			// 
			// 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(98, 24);
			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(29, 28);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(72, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Address =";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnDataStop,
																					this.btnDataStart,
																					this.btnDelRet,
																					this.txtRet,
																					this.label3,
																					this.txtSendData,
																					this.label2});
			this.groupBox3.Location = new System.Drawing.Point(8, 72);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(528, 164);
			this.groupBox3.TabIndex = 2;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Send / Receive";
			// 
			// btnDataStop
			// 
			this.btnDataStop.Enabled = false;
			this.btnDataStop.Location = new System.Drawing.Point(392, 129);
			this.btnDataStop.Name = "btnDataStop";
			this.btnDataStop.Size = new System.Drawing.Size(128, 26);
			this.btnDataStop.TabIndex = 6;
			this.btnDataStop.Text = "S&end Stop";
			this.btnDataStop.Click += new System.EventHandler(this.btnDataStop_Click);
			// 
			// btnDataStart
			// 
			this.btnDataStart.Enabled = false;
			this.btnDataStart.Location = new System.Drawing.Point(256, 129);
			this.btnDataStart.Name = "btnDataStart";
			this.btnDataStart.Size = new System.Drawing.Size(128, 26);
			this.btnDataStart.TabIndex = 5;
			this.btnDataStart.Text = "&Send Start";
			this.btnDataStart.Click += new System.EventHandler(this.btnDataStart_Click);
			// 
			// btnDelRet
			// 
			this.btnDelRet.Location = new System.Drawing.Point(416, 67);
			this.btnDelRet.Name = "btnDelRet";
			this.btnDelRet.Size = new System.Drawing.Size(104, 26);
			this.btnDelRet.TabIndex = 4;
			this.btnDelRet.Text = "De&lete";
			this.btnDelRet.Click += new System.EventHandler(this.btnDelRet_Click);
			// 
			// txtRet
			// 
			this.txtRet.Location = new System.Drawing.Point(8, 100);
			this.txtRet.Name = "txtRet";
			this.txtRet.Size = new System.Drawing.Size(512, 22);
			this.txtRet.TabIndex = 3;
			this.txtRet.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 82);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(136, 16);
			this.label3.TabIndex = 2;
			this.label3.Text = "Return value (Status)";
			// 
			// txtSendData
			// 
			this.txtSendData.Location = new System.Drawing.Point(8, 38);
			this.txtSendData.Name = "txtSendData";
			this.txtSendData.Size = new System.Drawing.Size(512, 22);
			this.txtSendData.TabIndex = 1;
			this.txtSendData.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 21);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(120, 16);
			this.label2.TabIndex = 0;
			this.label2.Text = "SendData";
			// 
			// btnInit
			// 
			this.btnInit.Location = new System.Drawing.Point(8, 248);
			this.btnInit.Name = "btnInit";
			this.btnInit.Size = new System.Drawing.Size(112, 26);
			this.btnInit.TabIndex = 6;
			this.btnInit.Text = "&Initialize";
			this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(132, 253);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(60, 16);
			this.label4.TabIndex = 7;
			this.label4.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(192, 249);
			this.cboDelimBox.Name = "cboDelimBox";
			this.cboDelimBox.Size = new System.Drawing.Size(96, 24);
			this.cboDelimBox.TabIndex = 8;
			this.cboDelimBox.SelectedIndexChanged += new System.EventHandler(this.cboDelimBox_SelectedIndexChanged);
			// 
			// btnYrDeviceIni
			// 
			this.btnYrDeviceIni.Enabled = false;
			this.btnYrDeviceIni.Location = new System.Drawing.Point(300, 248);
			this.btnYrDeviceIni.Name = "btnYrDeviceIni";
			this.btnYrDeviceIni.Size = new System.Drawing.Size(112, 26);
			this.btnYrDeviceIni.TabIndex = 9;
			this.btnYrDeviceIni.Text = "&Device Initialize";
			this.btnYrDeviceIni.Click += new System.EventHandler(this.btnYrDeviceIni_Click);
			// 
			// btnExt
			// 
			this.btnExt.Location = new System.Drawing.Point(424, 248);
			this.btnExt.Name = "btnExt";
			this.btnExt.Size = new System.Drawing.Size(112, 26);
			this.btnExt.TabIndex = 10;
			this.btnExt.Text = "E&xit";
			this.btnExt.Click += new System.EventHandler(this.btnExt_Click);
			// 
			// timer
			// 
			this.timer.Tick += new System.EventHandler(this.timer_Tick);
			// 
			// VoltCtrl
			// 
			this.ClientSize = new System.Drawing.Size(546, 288);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnExt,
																		  this.btnYrDeviceIni,
																		  this.cboDelimBox,
																		  this.label4,
																		  this.btnInit,
																		  this.groupBox3,
																		  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 = "VoltCtrl";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Voltage source control";
			this.Load += new System.EventHandler(this.VoltCtrl_Load);
			this.Closed += new System.EventHandler(this.VoltCtrl_Closed);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void VoltCtrl_Load(object sender, System.EventArgs e)
		{
			//******************************************************* Setting ***
			DvsCnt = 0x00;

			cboTargetName.Items.Add("[ YOKOGAWA Digital Voltage Source TYPE 2553 ]");
			cboTargetName.Items.Add("[ Advantest Voltage Source TR6150 ]");
			cboTargetName.SelectedIndex = 0;

			txtSendData.Text = "V3P0S00000O1";

			cboYrAdrBox.SelectedIndex = 8;
			cboDelimBox.SelectedIndex = 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 VoltCtrl_Closed(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();
			timer.Enabled = false;

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

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

			btnYrDeviceIni.Enabled = true;
			SubFunc.GpibInit(out csBuf);
			gpib.Boardsts(0x08, out Myadr);
			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;
				}
		
			}

			SubFunc.Cmd[0] = 1;
			SubFunc.Cmd[1] = 0x14;
			Ret = gpib.Comand(SubFunc.pCmd);
			if ((Ret & 0xFF) != 0)
			{
				SubFunc.CheckRet("GpComand", Ret, out csBuf);
				Ret = gpib.Dma(Dmamode, 3);
				txtRet.Text = csBuf;
				return;
			}	
			txtRet.Text = "Initialized.";

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

		//////////////////////////////////// Address of device Initialize ///
		private void btnYrDeviceIni_Click(object sender, System.EventArgs e)
		{
			switch (cboTargetName.SelectedIndex)
			{
				case 0:									// Yokogawa
					srBuffer = "V3P0";
					break;
				case 1:									// Adovantest
					srBuffer = "V5L3L7";
					break;
				default:
					break;
			}
			SubFunc.Cmd[0] = 2;
			SubFunc.Cmd[1] = Myadr;
			SubFunc.Cmd[2] = Yradr;
			Srlen = (uint)srBuffer.Length;
			Ret = gpib.Talk(SubFunc.pCmd, Srlen, srBuffer);
			if ((Ret & 0xFF) != 0)
			{
				SubFunc.CheckRet("GpTalk", Ret, out csBuf);
			}
			if ((Ret == 0) | ((Ret & 0xFF) == 0))
			{
				btnDataStart.Enabled = true;
				btnDataStop.Enabled = false;
				txtRet.Text = "Address of device Initialized.";
			}
			else
			{
				txtRet.Text = csBuf;
			}
			SubFunc.Cmd[0] = 2;
			SubFunc.Cmd[1] = 0x3f;
			SubFunc.Cmd[2] = 0x5f;
			Ret = gpib.Comand(SubFunc.pCmd);
			if ((Ret & 0xFF) != 0)
			{
				SubFunc.CheckRet("GpTalk", Ret, out csBuf);
				txtRet.Text = csBuf;
			}
		}

		//////////////////////////////////////////// Data Start ///
		private void btnDataStart_Click(object sender, System.EventArgs e)
		{
			btnDataStart.Enabled = false;
			btnDataStop.Enabled = true;
			timer.Enabled = true;
		}

		///////////////////////////////////////////// Data Stop ///
		private void btnDataStop_Click(object sender, System.EventArgs e)
		{
			btnDataStop.Enabled = false;
			btnDataStart.Enabled = true;
			timer.Enabled = false;
			SubFunc.Cmd[0] = 2;
			SubFunc.Cmd[1] = 0x3f;
			SubFunc.Cmd[2] = 0x5f;
			Ret = gpib.Comand(SubFunc.pCmd);
			if ((Ret & 0xFF) != 0)
			{
				SubFunc.CheckRet("GpComand", Ret, out csBuf);
			}
			else
			{
				csBuf = "Data transmission was stopped.";
			}
			txtRet.Text = csBuf;
		}

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

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

		///////////////////////////////////////////////// Timer ///
		private void timer_Tick(object sender, System.EventArgs e)
		{
			switch (cboTargetName.SelectedIndex)
			{
				case 0:									// YOKOGAWA 
					if (DvsCnt == 0)
					{
						srBuffer = string.Format("V3P0S00000O0");
					}
					else
					{
						srBuffer = string.Format("V3P0S{0:d2}000O1", DvsCnt);
					}
					if (DvsCnt >= 12)					// Stop loop 12
					{
						DvsCnt = 0x00;
					}
					else
					{
						DvsCnt++;
					}
					txtSendData.Text = srBuffer;

					Srlen = (uint)srBuffer.Length;
					if (Srlen != 0x0)
					{
						SubFunc.Cmd[0] = 2;
						SubFunc.Cmd[1] = Myadr;
						SubFunc.Cmd[2] = Yradr;
						Ret = gpib.Talk(SubFunc.pCmd, Srlen, srBuffer);
						if ((Ret & 0xFF) != 0)
						{
							SubFunc.CheckRet("GpTalk", Ret, out csBuf);
							txtRet.Text = csBuf;
							timer.Enabled = false;
							return;
						}
					}
					Ret = gpib.Get(Yradr);
					if ((Ret & 0xFF) != 0)
					{
						SubFunc.CheckRet("GpGet", Ret, out csBuf);
						txtRet.Text = csBuf;
						timer.Enabled = false;
						return;
					}
					txtRet.Text = "Data was sended.";
					break;

				case 1:									// Advantest
					srBuffer = string.Format("D{0:d2}E", DvsCnt);
					if (DvsCnt >= 10)
					{
						DvsCnt=0;
					}
					else
					{
						DvsCnt++;
					}
					txtSendData.Text = srBuffer;

					Srlen = (uint)srBuffer.Length;
					if (Srlen != 0x0) 
					{
						SubFunc.Cmd[0] = 2;	
						SubFunc.Cmd[1] = Myadr;
						SubFunc.Cmd[2] = Yradr;
						Ret = gpib.Talk(SubFunc.pCmd, Srlen, srBuffer);
						if ((Ret & 0xFF) != 0)
						{
							SubFunc.CheckRet("GpTalk", Ret, out csBuf);
							txtRet.Text = csBuf;
							timer.Enabled = false;
							return;
						}
					}
					Ret = gpib.Get(Yradr);
					if ((Ret & 0xFF) != 0)
					{
						SubFunc.CheckRet("GpGet", Ret, out csBuf);
						txtRet.Text = csBuf;
						timer.Enabled = false;
						return;
					}
					txtRet.Text = "Data was transmission.";
					break;
				default:
					break;
			}
			SubFunc.Cmd[0] = 2;
			SubFunc.Cmd[1] = 0x3f;
			SubFunc.Cmd[2] = 0x5f;
			Ret = gpib.Comand(SubFunc.pCmd);
		}

		//////////////////////////////////// Change Target Name ///
		private void cboTargetName_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			switch (cboTargetName.SelectedIndex)
			{						// Target Name
				case 0:
					szBuffer = "V3P0S00000O0";			// YOKOGAWA
					break;
				case 1:
					szBuffer = "D00E";					// Advantest
					break;
				default:
					break;
			}
			txtSendData.Text = szBuffer;
		}

		////////////////////////////////////////////////// Exit ///
		private void btnExt_Click(object sender, System.EventArgs e)
		{
			SubFunc.GpibExit();
			timer.Enabled = false;

			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);
		}
	}
}
