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>
	/// Form1 ̊Tv̐łB
	/// </summary>
	public class VoltCtrl : 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.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 cboYrAdrBox;
		private System.Windows.Forms.ComboBox cboTargetName;
		private System.Windows.Forms.TextBox txtSendData;
		private System.Windows.Forms.Button btnDelRet;
		private System.Windows.Forms.TextBox txtRet;
		private System.Windows.Forms.Button btnDataStart;
		private System.Windows.Forms.Button btnDataStop;
		private System.Windows.Forms.ComboBox cboDelimBox;
		private System.Windows.Forms.Button btnInit;
		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();

		// ϐ̐錾 ///////////////////////////////////////////////////////////////
		string		szBuffer = new string(' ', 1024);	// eLXg̃obt@
		uint		Ret;								// ߂l
		//int		Count;								// ߂l
		//int		SetT;								// ^C}̏(0:~ 1:s)
		uint		Yradr, Myadr;						// @AhXA}CAhX
		uint		Delim, Eoi;							// f~^
		uint		DvsCnt;
		uint		Dmamode = 0, Srlen;
		//uint		P_stb, P_eoi;						// Xe[^XoCgAEOĨ|C^
		uint[]		Pstb = new uint[32];				// Xe[^XoCg̔z
		string		srBuffer = new string(' ', 10000);	// Mf[^̃obt@/read work
		string		csBuf;								// eLXg\
		GCHandle	gChCmd;								// CmdzpGCHandle

		public VoltCtrl()
		{
			//
			// 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.components = new System.ComponentModel.Container();
			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.cboTargetName = new System.Windows.Forms.ComboBox();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.cboDelimBox = new System.Windows.Forms.ComboBox();
			this.btnDataStop = new System.Windows.Forms.Button();
			this.btnDataStart = new System.Windows.Forms.Button();
			this.txtRet = new System.Windows.Forms.TextBox();
			this.btnDelRet = new System.Windows.Forms.Button();
			this.txtSendData = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.btnInit = new System.Windows.Forms.Button();
			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.cboYrAdrBox,
																					this.label1});
			this.groupBox1.Location = new System.Drawing.Point(378, 6);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(160, 58);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.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(102, 24);
			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(6, 28);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(104, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "AhX =";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.cboTargetName});
			this.groupBox2.Location = new System.Drawing.Point(8, 6);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(372, 58);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "@햼";
			// 
			// 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(354, 24);
			this.cboTargetName.TabIndex = 0;
			this.cboTargetName.SelectedIndexChanged += new System.EventHandler(this.cboTargetName_SelectedIndexChanged);
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.cboDelimBox,
																					this.btnDataStop,
																					this.btnDataStart,
																					this.txtRet,
																					this.btnDelRet,
																					this.txtSendData,
																					this.label4,
																					this.label3,
																					this.label2});
			this.groupBox3.Location = new System.Drawing.Point(8, 71);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(530, 169);
			this.groupBox3.TabIndex = 2;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "f[^M";
			// 
			// 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(79, 130);
			this.cboDelimBox.Name = "cboDelimBox";
			this.cboDelimBox.Size = new System.Drawing.Size(89, 24);
			this.cboDelimBox.TabIndex = 8;
			this.cboDelimBox.SelectedIndexChanged += new System.EventHandler(this.cboDelimBox_SelectedIndexChanged);
			// 
			// btnDataStop
			// 
			this.btnDataStop.Location = new System.Drawing.Point(359, 127);
			this.btnDataStop.Name = "btnDataStop";
			this.btnDataStop.Size = new System.Drawing.Size(162, 30);
			this.btnDataStop.TabIndex = 7;
			this.btnDataStop.Text = "f[^M~(&E)";
			this.btnDataStop.Click += new System.EventHandler(this.btnDataStop_Click);
			// 
			// btnDataStart
			// 
			this.btnDataStart.Location = new System.Drawing.Point(190, 127);
			this.btnDataStart.Name = "btnDataStart";
			this.btnDataStart.Size = new System.Drawing.Size(162, 30);
			this.btnDataStart.TabIndex = 6;
			this.btnDataStart.Text = "f[^MJn(&S)";
			this.btnDataStart.Click += new System.EventHandler(this.btnDataStart_Click);
			// 
			// txtRet
			// 
			this.txtRet.Location = new System.Drawing.Point(8, 99);
			this.txtRet.Name = "txtRet";
			this.txtRet.Size = new System.Drawing.Size(512, 23);
			this.txtRet.TabIndex = 5;
			this.txtRet.Text = "";
			// 
			// btnDelRet
			// 
			this.btnDelRet.Location = new System.Drawing.Point(417, 68);
			this.btnDelRet.Name = "btnDelRet";
			this.btnDelRet.Size = new System.Drawing.Size(104, 26);
			this.btnDelRet.TabIndex = 4;
			this.btnDelRet.Text = "߂l";
			this.btnDelRet.Click += new System.EventHandler(this.btnDelRet_Click);
			// 
			// txtSendData
			// 
			this.txtSendData.Location = new System.Drawing.Point(8, 40);
			this.txtSendData.Name = "txtSendData";
			this.txtSendData.Size = new System.Drawing.Size(512, 23);
			this.txtSendData.TabIndex = 3;
			this.txtSendData.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 134);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(80, 16);
			this.label4.TabIndex = 2;
			this.label4.Text = "f~^";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 71);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(160, 16);
			this.label3.TabIndex = 1;
			this.label3.Text = "߂l(Xe[^X)";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 22);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(88, 16);
			this.label2.TabIndex = 0;
			this.label2.Text = "Mf[^";
			// 
			// btnInit
			// 
			this.btnInit.Location = new System.Drawing.Point(8, 248);
			this.btnInit.Name = "btnInit";
			this.btnInit.Size = new System.Drawing.Size(120, 30);
			this.btnInit.TabIndex = 7;
			this.btnInit.Text = "(&I)";
			this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
			// 
			// btnYrDeviceIni
			// 
			this.btnYrDeviceIni.Location = new System.Drawing.Point(136, 248);
			this.btnYrDeviceIni.Name = "btnYrDeviceIni";
			this.btnYrDeviceIni.Size = new System.Drawing.Size(146, 30);
			this.btnYrDeviceIni.TabIndex = 8;
			this.btnYrDeviceIni.Text = "@̏(&B)";
			this.btnYrDeviceIni.Click += new System.EventHandler(this.btnYrDeviceIni_Click);
			// 
			// btnExt
			// 
			this.btnExt.Location = new System.Drawing.Point(418, 248);
			this.btnExt.Name = "btnExt";
			this.btnExt.Size = new System.Drawing.Size(120, 30);
			this.btnExt.TabIndex = 10;
			this.btnExt.Text = "I(&X)";
			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.btnInit,
																		  this.groupBox3,
																		  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 = "VoltCtrl";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "dRg[";
			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>
		/// AvP[ṼC Gg |CgłB
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new VoltCtrl());
		}

		private void VoltCtrl_Load(object sender, System.EventArgs e)
		{
			// [ݒ]
			DvsCnt = 0x00;
			// @햼̒ǉs܂B
			// P
			cboTargetName.Items.Add("[ YOKOGAWA Digital Voltage Source TYPE 2553 ]");
			// Q
			cboTargetName.Items.Add("[ Advantest Voltage Source TR6150 ]");
			// ݒ(PڑI)
			cboTargetName.SelectedIndex = 0;

			txtSendData.Text = "V3P0S00000O1";					// Mf[^̏ݒ

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

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

		// [[`] ///////////////////////////////////////////////////////////
		private void btnInit_Click(object sender, System.EventArgs e)
		{
			string	DelMyadr = new string(' ', 8);
			uint	Mode;

			SubFunc.GpibInit(out csBuf);						// ֐̌ĂяoĂ܂B
			gpib.Boardsts(0x0a, out Mode);						// }X^(0)X[u(1)
			if (Mode != 0)
			{
				txtRet.Text = "}X^ł͂܂";			// }X^łȂ̏
				return;
			}
			// ̓}CAhXIłȂ悤ɂ镔łB////////////////////////
			gpib.Boardsts(0x08, out Myadr);						// }CAhX擾܂B
			DelMyadr = string.Format("{0:d}", Myadr);			// 𕶎ɕϊ
			Ret = (uint)cboYrAdrBox.FindStringExact(DelMyadr, -1);	// DelMyadrT܂B
			if (Ret >= 0)
			{													// vCfbNXΎs
				cboYrAdrBox.Items.Remove(Ret.ToString());		// }CAhXI{bNX폜B
				if (Myadr == Yradr)
				{
					cboYrAdrBox.SelectedIndex = 0;
				}
			}

			SubFunc.Cmd[0] = 1;
			SubFunc.Cmd[1] = 0x14;								// DCLZbg
			Ret = gpib.Comand(SubFunc.pCmd);					// R}h(bZ[W)𑗐M
			if ((Ret & 0xFF) != 0)
			{													// G[̏
				SubFunc.CheckRet("GpComand", Ret, out csBuf);
				Ret = gpib.Dma(Dmamode, 3);
				return;
			}
			txtRet.Text = "I܂B@햼AhXݒ肵ĂB";

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

		// [MJn[`] /////////////////////////////////////////////////////////
		private void btnDataStart_Click(object sender, System.EventArgs e)
		{
			timer.Enabled = true;
		}

		// [M~[`] /////////////////////////////////////////////////////////
		private void btnDataStop_Click(object sender, System.EventArgs e)
		{
			timer.Enabled = false;
			SubFunc.Cmd[0] = 2;									// R}h
			SubFunc.Cmd[1] = 0x3f;								// AX(UNL)^Xi
			SubFunc.Cmd[2] = 0x5f;								// Ag[N(UNT)^g[J
			Ret = gpib.Comand(SubFunc.pCmd);					// R}hM
			if ((Ret & 0xFF) != 0)
			{													// G[̏
				SubFunc.CheckRet("GpComand", Ret, out csBuf);
			}
			else
			{
				csBuf = "f[^M~܂B";			// ɑMI
			}
			txtRet.Text = csBuf;
		}

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

					Srlen = (uint)srBuffer.Length;				// Mf[^̒𑪒
					if (Srlen != 0x0)
					{
						SubFunc.Cmd[0] = 2;
						SubFunc.Cmd[1] = Myadr;
						SubFunc.Cmd[2] = Yradr;
						Ret = gpib.Talk(SubFunc.pCmd, Srlen, srBuffer);	// R}hM
						if ((Ret & 0xFF) != 0)
						{
							SubFunc.CheckRet("GpTalk", Ret, out csBuf);
							txtRet.Text = csBuf;
							timer.Enabled = false;				// G[̎̓^C}~
							return;
						}
					}
					Ret = gpib.Get(Yradr);						// GETR}hM
					if ((Ret & 0xFF) != 0)
					{
						SubFunc.CheckRet("GpGet", Ret, out csBuf);
						txtRet.Text = csBuf;
						timer.Enabled = false;
						return;
					}
					txtRet.Text = "f[^𑗐M܂B";
					break;

				case 1:											// Advantest I
					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 = "f[^𑗐M܂B";
					break;
				default:
					break;
			}
			SubFunc.Cmd[0] = 2;
			SubFunc.Cmd[1] = 0x3f;
			SubFunc.Cmd[2] = 0x5f;
			Ret = gpib.Comand(SubFunc.pCmd);
		}

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

			this.Close();										// _CAO܂B(_CAO̔j)
		}

		// [@ύXɂ鑗Mf[^̕ύX] ///////////////////////////////////////
		private void cboTargetName_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			switch (cboTargetName.SelectedIndex)
			{													// @̑I
				case 0:// P
					szBuffer = "V3P0S00000O0";					// YOKOGAWA I
					break;
				case 1:// Q
					szBuffer = "D00E";							// Advantest I
					break;
				default:
					break;
			}
			txtSendData.Text = szBuffer;
		}

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

		// [@AhXύX] ///////////////////////////////////////////////////////
		private void cboYrAdrBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Yradr = Convert.ToUInt32(cboYrAdrBox.Text);			// @̃AhX擾܂B
		}

		// [@̏] /////////////////////////////////////////////////////////
		private void btnYrDeviceIni_Click(object sender, System.EventArgs e)
		{
			switch (cboTargetName.SelectedIndex)
			{													// @̑I
				case 0:											// YokogawaI
					srBuffer = "V3P0";
					break;
				case 1:											// AdovantestI
					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);		// R}h𑗐M
			if ((Ret & 0xFF) != 0)
			{													// G[̏
				SubFunc.CheckRet("GpTalk", Ret, out csBuf);
			}
			if ((Ret == 0) | ((Ret & 0xFF) == 0))
			{
				txtRet.Text = "@܂B";
			}
			else
			{
				txtRet.Text = csBuf;
			}
			// @̌듮邽߂Ɏs܂B
			SubFunc.Cmd[0] = 2;
			SubFunc.Cmd[1] = 0x3f;								// AX(UNL)R}h / Xi
			SubFunc.Cmd[2] = 0x5f;								// Ag[N(UNT)R}h / g[J
			Ret = gpib.Comand(SubFunc.pCmd);					// R}hM
			if ((Ret & 0xFF) != 0)
			{													// G[̏
				SubFunc.CheckRet("GpTalk", Ret, out csBuf);
				txtRet.Text = csBuf;
			}
		}

		//[f~^ύX]////////////////////////////////////////////////////////////
		private void cboDelimBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Eoi = 1;											// EOI	Fgp
			Delim = (uint)cboDelimBox.SelectedIndex;
			Ret = gpib.Delim(Delim, Eoi);
		}
	}
}
