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 Parallel
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Parallel : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Button btnInit;
		private System.Windows.Forms.Button btnExt;
		private System.Windows.Forms.Button btnPollDel;
		private System.Windows.Forms.TextBox txtPollSet;
		private System.Windows.Forms.TextBox txtPollStart;
		private System.Windows.Forms.TextBox txtPollSts;
		private System.Windows.Forms.TextBox txtRet;
		private System.Windows.Forms.ComboBox cboYrAdrBox1;
		private System.Windows.Forms.ComboBox cboYrAdrBox2;
		private System.Windows.Forms.Button btnPolling;
		private System.Windows.Forms.Button btnRetDel;
		/// <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);
		uint		Ret;
		int			Count,Stppu;
		uint		Madr;
		uint		Yradr1, Yradr2;
		uint		PDataLine;
		uint[]		Cmd2 = new uint[32];
		uint[]		Pstb = new uint[32];
		uint[]		Pstb2 = new uint[32];
		string		csBuf;
		GCHandle    gChCmd;                 				// GCHandle for cmd array
    	GCHandle    gChPstb;             					// GCHandle for Pstb array
    	IntPtr      pPstb;                   				// IntPtr for Pstb array
		GCHandle    gChCmd2;                 				// GCHandle for cmd2 array
    	GCHandle    gChPstb2;             					// GCHandle for Pstb2 array
    	IntPtr      pPstb2;                   				// IntPtr for Pstb2 array
		IntPtr      pCmd2;                   				// IntPtr for cmd2 array
		/////////////////////////////////////////////////////////////////////

		public Parallel()
		{
			//
			// 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.btnInit = new System.Windows.Forms.Button();
			this.btnExt = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.txtPollSts = new System.Windows.Forms.TextBox();
			this.txtPollStart = new System.Windows.Forms.TextBox();
			this.txtPollSet = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.btnPollDel = new System.Windows.Forms.Button();
			this.label4 = new System.Windows.Forms.Label();
			this.txtRet = new System.Windows.Forms.TextBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.cboYrAdrBox1 = new System.Windows.Forms.ComboBox();
			this.label5 = new System.Windows.Forms.Label();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.cboYrAdrBox2 = new System.Windows.Forms.ComboBox();
			this.label6 = new System.Windows.Forms.Label();
			this.btnPolling = new System.Windows.Forms.Button();
			this.btnRetDel = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnInit
			// 
			this.btnInit.Location = new System.Drawing.Point(464, 18);
			this.btnInit.Name = "btnInit";
			this.btnInit.Size = new System.Drawing.Size(88, 32);
			this.btnInit.TabIndex = 0;
			this.btnInit.Text = "&Initialize";
			this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
			// 
			// btnExt
			// 
			this.btnExt.Location = new System.Drawing.Point(464, 65);
			this.btnExt.Name = "btnExt";
			this.btnExt.Size = new System.Drawing.Size(88, 32);
			this.btnExt.TabIndex = 1;
			this.btnExt.Text = "E&xit";
			this.btnExt.Click += new System.EventHandler(this.btnExt_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtPollSts,
																					this.txtPollStart,
																					this.txtPollSet,
																					this.label3,
																					this.label2,
																					this.label1,
																					this.btnPollDel});
			this.groupBox1.Location = new System.Drawing.Point(11, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(445, 170);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Information (Return Code)";
			// 
			// txtPollSts
			// 
			this.txtPollSts.Location = new System.Drawing.Point(10, 138);
			this.txtPollSts.Name = "txtPollSts";
			this.txtPollSts.Size = new System.Drawing.Size(424, 22);
			this.txtPollSts.TabIndex = 6;
			this.txtPollSts.Text = "";
			// 
			// txtPollStart
			// 
			this.txtPollStart.Location = new System.Drawing.Point(10, 93);
			this.txtPollStart.Name = "txtPollStart";
			this.txtPollStart.Size = new System.Drawing.Size(424, 22);
			this.txtPollStart.TabIndex = 5;
			this.txtPollStart.Text = "";
			// 
			// txtPollSet
			// 
			this.txtPollSet.Location = new System.Drawing.Point(10, 48);
			this.txtPollSet.Name = "txtPollSet";
			this.txtPollSet.Size = new System.Drawing.Size(424, 22);
			this.txtPollSet.TabIndex = 4;
			this.txtPollSet.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(10, 121);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(136, 16);
			this.label3.TabIndex = 3;
			this.label3.Text = "Polling Status";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(10, 75);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(136, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "Parallel Poll Result";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(10, 27);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(136, 16);
			this.label1.TabIndex = 1;
			this.label1.Text = "Polling Set";
			// 
			// btnPollDel
			// 
			this.btnPollDel.Location = new System.Drawing.Point(358, 13);
			this.btnPollDel.Name = "btnPollDel";
			this.btnPollDel.Size = new System.Drawing.Size(76, 28);
			this.btnPollDel.TabIndex = 0;
			this.btnPollDel.Text = "&Delete";
			this.btnPollDel.Click += new System.EventHandler(this.btnPollDel_Click);
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(11, 192);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(88, 16);
			this.label4.TabIndex = 3;
			this.label4.Text = "Return value";
			// 
			// txtRet
			// 
			this.txtRet.Location = new System.Drawing.Point(96, 192);
			this.txtRet.Name = "txtRet";
			this.txtRet.Size = new System.Drawing.Size(456, 22);
			this.txtRet.TabIndex = 7;
			this.txtRet.Text = "";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.cboYrAdrBox1,
																					this.label5});
			this.groupBox2.Location = new System.Drawing.Point(11, 224);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(184, 56);
			this.groupBox2.TabIndex = 8;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Address of device 1";
			// 
			// cboYrAdrBox1
			// 
			this.cboYrAdrBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboYrAdrBox1.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.cboYrAdrBox1.Location = new System.Drawing.Point(104, 24);
			this.cboYrAdrBox1.Name = "cboYrAdrBox1";
			this.cboYrAdrBox1.Size = new System.Drawing.Size(48, 24);
			this.cboYrAdrBox1.TabIndex = 1;
			this.cboYrAdrBox1.SelectedIndexChanged += new System.EventHandler(this.cboYrAdrBox1_SelectedIndexChanged);
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(32, 24);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(72, 16);
			this.label5.TabIndex = 0;
			this.label5.Text = "Address =";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.cboYrAdrBox2,
																					this.label6});
			this.groupBox3.Location = new System.Drawing.Point(208, 224);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(184, 56);
			this.groupBox3.TabIndex = 9;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Address of device 2";
			// 
			// cboYrAdrBox2
			// 
			this.cboYrAdrBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboYrAdrBox2.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.cboYrAdrBox2.Location = new System.Drawing.Point(104, 24);
			this.cboYrAdrBox2.Name = "cboYrAdrBox2";
			this.cboYrAdrBox2.Size = new System.Drawing.Size(48, 24);
			this.cboYrAdrBox2.TabIndex = 1;
			this.cboYrAdrBox2.SelectedIndexChanged += new System.EventHandler(this.cboYrAdrBox2_SelectedIndexChanged);
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(32, 24);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(72, 16);
			this.label6.TabIndex = 0;
			this.label6.Text = "Address =";
			// 
			// btnPolling
			// 
			this.btnPolling.Enabled = false;
			this.btnPolling.Location = new System.Drawing.Point(400, 240);
			this.btnPolling.Name = "btnPolling";
			this.btnPolling.Size = new System.Drawing.Size(152, 32);
			this.btnPolling.TabIndex = 10;
			this.btnPolling.Text = "&Parallel Poll";
			this.btnPolling.Click += new System.EventHandler(this.btnPolling_Click);
			// 
			// btnRetDel
			// 
			this.btnRetDel.Location = new System.Drawing.Point(464, 112);
			this.btnRetDel.Name = "btnRetDel";
			this.btnRetDel.Size = new System.Drawing.Size(88, 32);
			this.btnRetDel.TabIndex = 11;
			this.btnRetDel.Text = "D&elete";
			this.btnRetDel.Click += new System.EventHandler(this.btnRetDel_Click);
			// 
			// Parallel
			// 
			this.ClientSize = new System.Drawing.Size(562, 288);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnRetDel,
																		  this.btnPolling,
																		  this.groupBox3,
																		  this.groupBox2,
																		  this.txtRet,
																		  this.label4,
																		  this.groupBox1,
																		  this.btnExt,
																		  this.btnInit});
			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 = "Parallel";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Parallel Poll";
			this.Load += new System.EventHandler(this.Parallel_Load);
			this.Closed += new System.EventHandler(this.Parallel_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 Parallel());
		}

		private void Parallel_Load(object sender, System.EventArgs e)
		{
			//******************************************************* Setting ***
			cboYrAdrBox1.Text = "1";
			cboYrAdrBox2.Text = "2";
			//*******************************************************************

			// Specify Pinned handle to Cmd
			if (gChCmd.IsAllocated == false) {
				gChCmd = GCHandle.Alloc(SubFunc.Cmd, GCHandleType.Pinned);
			}
			if (gChCmd2.IsAllocated == false) {
				gChCmd2 = GCHandle.Alloc(Cmd2, GCHandleType.Pinned);
			}
			// Specify Pinned handle to Pstb
			if (gChPstb.IsAllocated == false) {
				gChPstb = GCHandle.Alloc(Pstb, GCHandleType.Pinned);
			}
			if (gChPstb2.IsAllocated == false) {
				gChPstb2 = GCHandle.Alloc(Pstb2, GCHandleType.Pinned);
			}
			// Get address object by Pinned handle
			SubFunc.pCmd = gChCmd.AddrOfPinnedObject();
			pCmd2 = gChCmd2.AddrOfPinnedObject();
			pPstb = gChPstb.AddrOfPinnedObject();
			pPstb2 = gChPstb2.AddrOfPinnedObject();
		}

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

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

		//////////////////////////////////////////// Initialize ///
		private void btnInit_Click(object sender, System.EventArgs e)
		{
			string	DelMadr = new string(' ', 16);
			
			SubFunc.GpibInit(out csBuf);
			gpib.Boardsts(0x08, out Madr);
			DelMadr = string.Format("{0:d}", Madr);
			Ret = (uint)cboYrAdrBox1.FindStringExact(DelMadr, -1);
			if (Ret >= 0 && Ret <= 30)		
			{							
				cboYrAdrBox1.Items.Remove(Ret.ToString());
				cboYrAdrBox1.SelectedIndex = 0;
				Yradr1 = Convert.ToUInt32(cboYrAdrBox1.Text);
				cboYrAdrBox2.Items.Remove(Ret.ToString());
				cboYrAdrBox2.SelectedIndex = 1;
			}
			txtRet.Text = csBuf;

			cboYrAdrBox1.Enabled = true;
			cboYrAdrBox2.Enabled = true;
			btnPolling.Enabled = true;

			Yradr1 = Convert.ToUInt32(cboYrAdrBox1.Text);
			Yradr2 = Convert.ToUInt32(cboYrAdrBox2.Text);
		}

		/////////////////////////////////////////////// Polling ///
		private void btnPolling_Click(object sender, System.EventArgs e)
		{
			string	csBuf1,csBuf2;

			for (Count = 0; Count < 16; Count++)
			{	// Pstb[],Pstb2[]Initialize
				Pstb[Count] = 0x00;
				Pstb2[Count] = 0x00;
			}
			txtPollSet.Text = "";
			txtPollStart.Text = "";
			txtPollSts.Text = "";

			SubFunc.Cmd[0] = 2;							// Number of Address of device 
			SubFunc.Cmd[1] = Yradr1;					// 1st
			SubFunc.Cmd[2] = 0x69;						// Line 69H = DIO2
			SubFunc.Cmd[3] = Yradr2;					// 2nd
			SubFunc.Cmd[4] = 0x6a;						// Line 6aH = DIO3
			Stppu = 1;									// PPU Setting
	
			Ret = gpib.Stppoll(SubFunc.pCmd, (uint)Stppu);		// Parallel Poll
			SubFunc.CheckRet("GpStppoll", Ret, out csBuf);
			txtPollSet.Text = csBuf;
			if ((Ret & 0xFF) != 0)
			{
				return;
			}
			Ret = gpib.Exppoll(out PDataLine);			// Serial Poll
			SubFunc.CheckRet("GpExppoll", Ret, out csBuf);
			txtPollStart.Text = csBuf;
			if ((Ret & 0xFF) != 0)
			{
				return;
			}

			switch (PDataLine)
			{											// Select value of PDataLine
				case 2:									// [ 00000010(DIO2) ]
					SubFunc.Cmd[0] = 1;
					SubFunc.Cmd[1] = Yradr1;
					Ret = gpib.Poll(SubFunc.pCmd, pPstb);
					SubFunc.CheckRet("GpPoll", Ret, out csBuf);	
					if (((Ret & 0xFF) != 0) && ((Ret & 0xFF) != 1))
					{
						txtPollSts.Text = csBuf;
						return;
					}
					szBuffer = string.Format("Talker = [ {0:d} ] StatusByte =[ {1:x} ]H",SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]]);
					txtPollSts.Text = szBuffer;
					break;
				case 4:									// [ 00000100(DIO3) ]
					SubFunc.Cmd[0] = 1;
					SubFunc.Cmd[1] = Yradr2;
					Ret = gpib.Poll(SubFunc.pCmd, pPstb);
					SubFunc.CheckRet("GpPoll", Ret, out csBuf);
					if (((Ret & 0xFF) != 0) && ((Ret & 0xFF) != 1))
					{
						txtPollSts.Text = csBuf;
						return;
					}
					szBuffer = string.Format("Talker = [ {0:d} ] StatusByte = [ {1:x} ]H",SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]]);
					txtPollSts.Text = szBuffer;
					break;
				case 6:									// [ 00000110(DIO2 | DIO3) ]
					SubFunc.Cmd[0] = 1;
					SubFunc.Cmd[1] = Yradr1;
					Ret = gpib.Poll(SubFunc.pCmd, pPstb);
					SubFunc.CheckRet("Target1", Ret, out csBuf1);
					if (((Ret & 0xFF) != 0) && ((Ret & 0xFF) != 1))
					{
						txtPollSts.Text = csBuf1;
						return;
					}

					Cmd2[0] = 1;
					Cmd2[1] = Yradr2;
					Ret = gpib.Poll(pCmd2, pPstb2);
					SubFunc.CheckRet("Target2", Ret, out csBuf2);
					if (((Ret & 0xFF) != 0) && ((Ret & 0xFF) != 1))
					{
						txtPollSts.Text = csBuf2;
						return;
					}

					szBuffer = string.Format("Talker = [ {0:d} ] StatusByte = [ {1:x} ]H / Talker = [ {2:d} ] StatusByte =[ {3:x} ]H",
						SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]], Cmd2[Pstb2[0]], Pstb2[Pstb2[0]]);
					txtPollSts.Text = szBuffer;
					break;
				default:
					csBuf = "[SRQ] is not found out.";
					txtPollSts.Text = csBuf;
					break;
			}
		}

		/////////////////////////////////////// Address of device 1 Change ///
		private void cboYrAdrBox1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Yradr1 = Convert.ToUInt32(cboYrAdrBox1.Text);
		}

		/////////////////////////////////////// Address of device 2 Change ///
		private void cboYrAdrBox2_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Yradr2 = Convert.ToUInt32(cboYrAdrBox2.Text);
		}

		/////////////////////////////////////////// Delete Poll ///
		private void btnPollDel_Click(object sender, System.EventArgs e)
		{
			txtPollSet.Text = "";
			txtPollStart.Text = "";
			txtPollSts.Text = "";
		}

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

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