using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using CGpCmDeclCs;

namespace GpibTest
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class GpibTest : System.Windows.Forms.Form
	{
		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.Button btnInit;
		private System.Windows.Forms.Button btnMaster;
		private System.Windows.Forms.Button btnSlave;
		private System.Windows.Forms.Button btnSPoll;
		private System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.TextBox txtAddress;
		private System.Windows.Forms.TextBox txtMaster;
		private System.Windows.Forms.TextBox txtSlave;
		private System.Windows.Forms.TextBox txtSPoll;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		CGpCmDecl	GpCmDecl = new CGpCmDecl();

		int			Dev;
		int			Ret;

		public GpibTest()
		{
			//
			// 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.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.txtAddress = new System.Windows.Forms.TextBox();
			this.txtMaster = new System.Windows.Forms.TextBox();
			this.txtSlave = new System.Windows.Forms.TextBox();
			this.txtSPoll = new System.Windows.Forms.TextBox();
			this.btnInit = new System.Windows.Forms.Button();
			this.btnMaster = new System.Windows.Forms.Button();
			this.btnSlave = new System.Windows.Forms.Button();
			this.btnSPoll = new System.Windows.Forms.Button();
			this.btnExit = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 34);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(72, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Address";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 70);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(72, 16);
			this.label2.TabIndex = 1;
			this.label2.Text = "SendData";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 106);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(72, 16);
			this.label3.TabIndex = 2;
			this.label3.Text = "ReceiveData";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 142);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(72, 16);
			this.label4.TabIndex = 3;
			this.label4.Text = "SPoll";
			// 
			// txtAddress
			// 
			this.txtAddress.Location = new System.Drawing.Point(88, 32);
			this.txtAddress.Name = "txtAddress";
			this.txtAddress.Size = new System.Drawing.Size(80, 20);
			this.txtAddress.TabIndex = 4;
			this.txtAddress.Text = "";
			// 
			// txtMaster
			// 
			this.txtMaster.Location = new System.Drawing.Point(88, 68);
			this.txtMaster.Name = "txtMaster";
			this.txtMaster.Size = new System.Drawing.Size(200, 20);
			this.txtMaster.TabIndex = 5;
			this.txtMaster.Text = "";
			// 
			// txtSlave
			// 
			this.txtSlave.Location = new System.Drawing.Point(88, 104);
			this.txtSlave.Name = "txtSlave";
			this.txtSlave.Size = new System.Drawing.Size(200, 20);
			this.txtSlave.TabIndex = 6;
			this.txtSlave.Text = "";
			// 
			// txtSPoll
			// 
			this.txtSPoll.Location = new System.Drawing.Point(88, 140);
			this.txtSPoll.Name = "txtSPoll";
			this.txtSPoll.Size = new System.Drawing.Size(80, 20);
			this.txtSPoll.TabIndex = 7;
			this.txtSPoll.Text = "";
			// 
			// btnInit
			// 
			this.btnInit.Location = new System.Drawing.Point(312, 29);
			this.btnInit.Name = "btnInit";
			this.btnInit.Size = new System.Drawing.Size(85, 26);
			this.btnInit.TabIndex = 8;
			this.btnInit.Text = "Initialize ";
			this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
			// 
			// btnMaster
			// 
			this.btnMaster.Location = new System.Drawing.Point(312, 65);
			this.btnMaster.Name = "btnMaster";
			this.btnMaster.Size = new System.Drawing.Size(85, 26);
			this.btnMaster.TabIndex = 9;
			this.btnMaster.Text = "Send";
			this.btnMaster.Click += new System.EventHandler(this.btnMaster_Click);
			// 
			// btnSlave
			// 
			this.btnSlave.Location = new System.Drawing.Point(312, 101);
			this.btnSlave.Name = "btnSlave";
			this.btnSlave.Size = new System.Drawing.Size(85, 26);
			this.btnSlave.TabIndex = 10;
			this.btnSlave.Text = "Receive";
			this.btnSlave.Click += new System.EventHandler(this.btnSlave_Click);
			// 
			// btnSPoll
			// 
			this.btnSPoll.Location = new System.Drawing.Point(312, 137);
			this.btnSPoll.Name = "btnSPoll";
			this.btnSPoll.Size = new System.Drawing.Size(85, 26);
			this.btnSPoll.TabIndex = 11;
			this.btnSPoll.Text = "SPoll";
			this.btnSPoll.Click += new System.EventHandler(this.btnSPoll_Click);
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(312, 173);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(85, 26);
			this.btnExit.TabIndex = 12;
			this.btnExit.Text = "Exit";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// GpibTest
			// 
			this.ClientSize = new System.Drawing.Size(418, 216);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnExit,
																		  this.btnSPoll,
																		  this.btnSlave,
																		  this.btnMaster,
																		  this.btnInit,
																		  this.txtSPoll,
																		  this.txtSlave,
																		  this.txtMaster,
																		  this.txtAddress,
																		  this.label4,
																		  this.label3,
																		  this.label2,
																		  this.label1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "GpibTest";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "GpibTest";
			this.Load += new System.EventHandler(this.GpibTest_Load);
			this.Closed += new System.EventHandler(this.GpibTest_Closed);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void GpibTest_Load(object sender, System.EventArgs e)
		{
			txtAddress.Text = "1";
			txtMaster.Text = "*IDN?";
			txtSlave.Text = "";
			txtSPoll.Text = "0";
		}

		private void GpibTest_Closed(object sender, System.EventArgs e)
		{
			GpCmDecl.Pkibonl(Dev, 0);
		}

		private void btnInit_Click(object sender, System.EventArgs e)
		{
			string	address = new string(' ', 100);
			int		pad;

			address = txtAddress.Text;
			try
			{
				pad = Convert.ToInt32(address);
			}
			catch (System.FormatException)
			{
				pad = 0;
			}

			Dev = GpCmDecl.Pkibdev (0, pad, 0, (int)CGpCmDeclConst.T10s, 1, 0);

			Ret = GpCmDecl.PkThreadIbsta();
			if ((Ret & (int)CGpCmDeclConst.ERR) != 0)
			{
				MessageBox.Show("Unable to open device");
			}
		}

		private void btnMaster_Click(object sender, System.EventArgs e)
		{
			string	wrtbuf = new string(' ', 100);
			int		nCnt;

			wrtbuf = txtMaster.Text;

			nCnt = wrtbuf.Length;
			GpCmDecl.Pkibwrt(Dev, wrtbuf, nCnt);

			Ret = GpCmDecl.PkThreadIbsta();
			if ((Ret & (int)CGpCmDeclConst.ERR) != 0) 
			{
				MessageBox.Show("Unable to write to device");
			}
		}

		private void btnSlave_Click(object sender, System.EventArgs e)
		{
			StringBuilder rdbuf = new StringBuilder("", 100);
		
			GpCmDecl.Pkibrd(Dev, rdbuf, rdbuf.Capacity);
			int	nCnt = GpCmDecl.PkThreadIbcntl();

			Ret = GpCmDecl.PkThreadIbsta();
			if ((Ret & (int)CGpCmDeclConst.ERR) != 0) 
			{
				MessageBox.Show("Unable to read from device");
			}
	
			txtSlave.Text = rdbuf.ToString(0, nCnt);
		}

		private void btnSPoll_Click(object sender, System.EventArgs e)
		{
			char spr;

			GpCmDecl.Pkibrsp(Dev, out spr);
	
			Ret = GpCmDecl.PkThreadIbsta();
			if ((Ret & (int)CGpCmDeclConst.ERR) != 0) 
			{
				MessageBox.Show("Unable to serial poll");
			}

			txtSPoll.Text = string.Format("{0:x} H", (int)spr.ToString()[0]);
		}

		private void btnExit_Click(object sender, System.EventArgs e)
		{
			GpCmDecl.Pkibonl(Dev, 0);
			this.Close();
		}
	}
}
