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

namespace Pti4
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Pti4 : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.Button btnOK;
		internal System.Windows.Forms.Button btnExit;
		internal System.Windows.Forms.Button btnStatus;
		internal System.Windows.Forms.Button btnInput;
		internal System.Windows.Forms.Button btnStop;
		internal System.Windows.Forms.Button btnStart;
		internal System.Windows.Forms.Button btnASet;
		internal System.Windows.Forms.Button btnMSet;
		internal System.Windows.Forms.Button btnInit;
		internal System.Windows.Forms.TextBox txtRet;
		internal System.Windows.Forms.Label Label18;
		internal System.Windows.Forms.GroupBox GroupBox3;
		internal System.Windows.Forms.Label Label14;
		internal System.Windows.Forms.Label Label13;
		internal System.Windows.Forms.Label Label12;
		internal System.Windows.Forms.Label Label17;
		internal System.Windows.Forms.Label Label16;
		internal System.Windows.Forms.Label Label15;
		internal System.Windows.Forms.TextBox txtStsLAlarm;
		internal System.Windows.Forms.TextBox txtStsUAlarm;
		internal System.Windows.Forms.TextBox txtStsDc;
		internal System.Windows.Forms.GroupBox GroupBox2;
		internal System.Windows.Forms.Label Label11;
		internal System.Windows.Forms.Label Label10;
		internal System.Windows.Forms.Label Label9;
		internal System.Windows.Forms.Label Label8;
		internal System.Windows.Forms.TextBox txtCh3;
		internal System.Windows.Forms.TextBox txtCh2;
		internal System.Windows.Forms.TextBox txtCh1;
		internal System.Windows.Forms.TextBox txtCh0;
		internal System.Windows.Forms.Label Label7;
		internal System.Windows.Forms.Label Label6;
		internal System.Windows.Forms.Label Label5;
		internal System.Windows.Forms.Label Label4;
		internal System.Windows.Forms.GroupBox GroupBox1;
		internal System.Windows.Forms.Label lblErr;
		internal System.Windows.Forms.Label lblStart;
		internal System.Windows.Forms.Label Label3;
		internal System.Windows.Forms.Label Label2;
		internal System.Windows.Forms.ComboBox cboDevice;
		internal System.Windows.Forms.Label Label1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		short	m_Id;		// ID

		public Pti4()
		{
			//
			// 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.btnOK = new System.Windows.Forms.Button();
			this.btnExit = new System.Windows.Forms.Button();
			this.btnStatus = new System.Windows.Forms.Button();
			this.btnInput = new System.Windows.Forms.Button();
			this.btnStop = new System.Windows.Forms.Button();
			this.btnStart = new System.Windows.Forms.Button();
			this.btnASet = new System.Windows.Forms.Button();
			this.btnMSet = new System.Windows.Forms.Button();
			this.btnInit = new System.Windows.Forms.Button();
			this.txtRet = new System.Windows.Forms.TextBox();
			this.Label18 = new System.Windows.Forms.Label();
			this.GroupBox3 = new System.Windows.Forms.GroupBox();
			this.Label14 = new System.Windows.Forms.Label();
			this.Label13 = new System.Windows.Forms.Label();
			this.Label12 = new System.Windows.Forms.Label();
			this.Label17 = new System.Windows.Forms.Label();
			this.Label16 = new System.Windows.Forms.Label();
			this.Label15 = new System.Windows.Forms.Label();
			this.txtStsLAlarm = new System.Windows.Forms.TextBox();
			this.txtStsUAlarm = new System.Windows.Forms.TextBox();
			this.txtStsDc = new System.Windows.Forms.TextBox();
			this.GroupBox2 = new System.Windows.Forms.GroupBox();
			this.Label11 = new System.Windows.Forms.Label();
			this.Label10 = new System.Windows.Forms.Label();
			this.Label9 = new System.Windows.Forms.Label();
			this.Label8 = new System.Windows.Forms.Label();
			this.txtCh3 = new System.Windows.Forms.TextBox();
			this.txtCh2 = new System.Windows.Forms.TextBox();
			this.txtCh1 = new System.Windows.Forms.TextBox();
			this.txtCh0 = new System.Windows.Forms.TextBox();
			this.Label7 = new System.Windows.Forms.Label();
			this.Label6 = new System.Windows.Forms.Label();
			this.Label5 = new System.Windows.Forms.Label();
			this.Label4 = new System.Windows.Forms.Label();
			this.GroupBox1 = new System.Windows.Forms.GroupBox();
			this.lblErr = new System.Windows.Forms.Label();
			this.lblStart = new System.Windows.Forms.Label();
			this.Label3 = new System.Windows.Forms.Label();
			this.Label2 = new System.Windows.Forms.Label();
			this.cboDevice = new System.Windows.Forms.ComboBox();
			this.Label1 = new System.Windows.Forms.Label();
			this.GroupBox3.SuspendLayout();
			this.GroupBox2.SuspendLayout();
			this.GroupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(351, 347);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(144, 24);
			this.btnOK.TabIndex = 31;
			this.btnOK.Text = "Exit";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(351, 299);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(144, 24);
			this.btnExit.TabIndex = 30;
			this.btnExit.Text = "Termination";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// btnStatus
			// 
			this.btnStatus.Location = new System.Drawing.Point(351, 251);
			this.btnStatus.Name = "btnStatus";
			this.btnStatus.Size = new System.Drawing.Size(144, 24);
			this.btnStatus.TabIndex = 29;
			this.btnStatus.Text = "Status";
			this.btnStatus.Click += new System.EventHandler(this.btnStatus_Click);
			// 
			// btnInput
			// 
			this.btnInput.Location = new System.Drawing.Point(351, 219);
			this.btnInput.Name = "btnInput";
			this.btnInput.Size = new System.Drawing.Size(144, 24);
			this.btnInput.TabIndex = 28;
			this.btnInput.Text = "Input Value";
			this.btnInput.Click += new System.EventHandler(this.btnInput_Click);
			// 
			// btnStop
			// 
			this.btnStop.Location = new System.Drawing.Point(351, 171);
			this.btnStop.Name = "btnStop";
			this.btnStop.Size = new System.Drawing.Size(144, 24);
			this.btnStop.TabIndex = 27;
			this.btnStop.Text = "Stop";
			this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
			// 
			// btnStart
			// 
			this.btnStart.Location = new System.Drawing.Point(351, 139);
			this.btnStart.Name = "btnStart";
			this.btnStart.Size = new System.Drawing.Size(144, 24);
			this.btnStart.TabIndex = 26;
			this.btnStart.Text = "Start";
			this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
			// 
			// btnASet
			// 
			this.btnASet.Location = new System.Drawing.Point(351, 91);
			this.btnASet.Name = "btnASet";
			this.btnASet.Size = new System.Drawing.Size(144, 24);
			this.btnASet.TabIndex = 25;
			this.btnASet.Text = "Alarm Setting...";
			this.btnASet.Click += new System.EventHandler(this.btnASet_Click);
			// 
			// btnMSet
			// 
			this.btnMSet.Location = new System.Drawing.Point(351, 59);
			this.btnMSet.Name = "btnMSet";
			this.btnMSet.Size = new System.Drawing.Size(144, 24);
			this.btnMSet.TabIndex = 24;
			this.btnMSet.Text = "Measurement Setting...";
			this.btnMSet.Click += new System.EventHandler(this.btnMSet_Click);
			// 
			// btnInit
			// 
			this.btnInit.Location = new System.Drawing.Point(351, 13);
			this.btnInit.Name = "btnInit";
			this.btnInit.Size = new System.Drawing.Size(144, 24);
			this.btnInit.TabIndex = 23;
			this.btnInit.Text = "Initialization";
			this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
			// 
			// txtRet
			// 
			this.txtRet.Location = new System.Drawing.Point(95, 395);
			this.txtRet.Name = "txtRet";
			this.txtRet.Size = new System.Drawing.Size(400, 20);
			this.txtRet.TabIndex = 22;
			this.txtRet.Text = "";
			// 
			// Label18
			// 
			this.Label18.Location = new System.Drawing.Point(15, 397);
			this.Label18.Name = "Label18";
			this.Label18.Size = new System.Drawing.Size(80, 16);
			this.Label18.TabIndex = 21;
			this.Label18.Text = "Return Value :";
			// 
			// GroupBox3
			// 
			this.GroupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.Label14,
																					this.Label13,
																					this.Label12,
																					this.Label17,
																					this.Label16,
																					this.Label15,
																					this.txtStsLAlarm,
																					this.txtStsUAlarm,
																					this.txtStsDc});
			this.GroupBox3.Location = new System.Drawing.Point(15, 267);
			this.GroupBox3.Name = "GroupBox3";
			this.GroupBox3.Size = new System.Drawing.Size(312, 104);
			this.GroupBox3.TabIndex = 20;
			this.GroupBox3.TabStop = false;
			this.GroupBox3.Text = "Status";
			// 
			// Label14
			// 
			this.Label14.Location = new System.Drawing.Point(15, 73);
			this.Label14.Name = "Label14";
			this.Label14.Size = new System.Drawing.Size(90, 16);
			this.Label14.TabIndex = 14;
			this.Label14.Text = "Low-limit Alarm :";
			this.Label14.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Label13
			// 
			this.Label13.Location = new System.Drawing.Point(16, 45);
			this.Label13.Name = "Label13";
			this.Label13.Size = new System.Drawing.Size(90, 16);
			this.Label13.TabIndex = 13;
			this.Label13.Text = "High-limit Alarm :";
			this.Label13.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Label12
			// 
			this.Label12.Location = new System.Drawing.Point(8, 18);
			this.Label12.Name = "Label12";
			this.Label12.Size = new System.Drawing.Size(90, 16);
			this.Label12.TabIndex = 12;
			this.Label12.Text = "Disconnect :";
			this.Label12.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Label17
			// 
			this.Label17.Location = new System.Drawing.Point(237, 75);
			this.Label17.Name = "Label17";
			this.Label17.Size = new System.Drawing.Size(32, 16);
			this.Label17.TabIndex = 11;
			this.Label17.Text = "Hex";
			// 
			// Label16
			// 
			this.Label16.Location = new System.Drawing.Point(237, 47);
			this.Label16.Name = "Label16";
			this.Label16.Size = new System.Drawing.Size(32, 16);
			this.Label16.TabIndex = 10;
			this.Label16.Text = "Hex";
			// 
			// Label15
			// 
			this.Label15.Location = new System.Drawing.Point(237, 20);
			this.Label15.Name = "Label15";
			this.Label15.Size = new System.Drawing.Size(32, 16);
			this.Label15.TabIndex = 9;
			this.Label15.Text = "Hex";
			// 
			// txtStsLAlarm
			// 
			this.txtStsLAlarm.Location = new System.Drawing.Point(106, 72);
			this.txtStsLAlarm.Name = "txtStsLAlarm";
			this.txtStsLAlarm.Size = new System.Drawing.Size(120, 20);
			this.txtStsLAlarm.TabIndex = 7;
			this.txtStsLAlarm.Text = "";
			// 
			// txtStsUAlarm
			// 
			this.txtStsUAlarm.Location = new System.Drawing.Point(106, 44);
			this.txtStsUAlarm.Name = "txtStsUAlarm";
			this.txtStsUAlarm.Size = new System.Drawing.Size(120, 20);
			this.txtStsUAlarm.TabIndex = 6;
			this.txtStsUAlarm.Text = "";
			// 
			// txtStsDc
			// 
			this.txtStsDc.Location = new System.Drawing.Point(106, 16);
			this.txtStsDc.Name = "txtStsDc";
			this.txtStsDc.Size = new System.Drawing.Size(120, 20);
			this.txtStsDc.TabIndex = 5;
			this.txtStsDc.Text = "";
			// 
			// GroupBox2
			// 
			this.GroupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.Label11,
																					this.Label10,
																					this.Label9,
																					this.Label8,
																					this.txtCh3,
																					this.txtCh2,
																					this.txtCh1,
																					this.txtCh0,
																					this.Label7,
																					this.Label6,
																					this.Label5,
																					this.Label4});
			this.GroupBox2.Location = new System.Drawing.Point(15, 119);
			this.GroupBox2.Name = "GroupBox2";
			this.GroupBox2.Size = new System.Drawing.Size(312, 136);
			this.GroupBox2.TabIndex = 19;
			this.GroupBox2.TabStop = false;
			this.GroupBox2.Text = "Input Value";
			// 
			// Label11
			// 
			this.Label11.Location = new System.Drawing.Point(237, 106);
			this.Label11.Name = "Label11";
			this.Label11.Size = new System.Drawing.Size(32, 16);
			this.Label11.TabIndex = 11;
			this.Label11.Text = "C";
			// 
			// Label10
			// 
			this.Label10.Location = new System.Drawing.Point(237, 81);
			this.Label10.Name = "Label10";
			this.Label10.Size = new System.Drawing.Size(32, 16);
			this.Label10.TabIndex = 10;
			this.Label10.Text = "C";
			// 
			// Label9
			// 
			this.Label9.Location = new System.Drawing.Point(237, 54);
			this.Label9.Name = "Label9";
			this.Label9.Size = new System.Drawing.Size(32, 16);
			this.Label9.TabIndex = 9;
			this.Label9.Text = "C";
			// 
			// Label8
			// 
			this.Label8.Location = new System.Drawing.Point(237, 29);
			this.Label8.Name = "Label8";
			this.Label8.Size = new System.Drawing.Size(32, 16);
			this.Label8.TabIndex = 8;
			this.Label8.Text = "C";
			// 
			// txtCh3
			// 
			this.txtCh3.Location = new System.Drawing.Point(106, 102);
			this.txtCh3.Name = "txtCh3";
			this.txtCh3.Size = new System.Drawing.Size(120, 20);
			this.txtCh3.TabIndex = 7;
			this.txtCh3.Text = "";
			// 
			// txtCh2
			// 
			this.txtCh2.Location = new System.Drawing.Point(106, 76);
			this.txtCh2.Name = "txtCh2";
			this.txtCh2.Size = new System.Drawing.Size(120, 20);
			this.txtCh2.TabIndex = 6;
			this.txtCh2.Text = "";
			// 
			// txtCh1
			// 
			this.txtCh1.Location = new System.Drawing.Point(106, 50);
			this.txtCh1.Name = "txtCh1";
			this.txtCh1.Size = new System.Drawing.Size(120, 20);
			this.txtCh1.TabIndex = 5;
			this.txtCh1.Text = "";
			// 
			// txtCh0
			// 
			this.txtCh0.Location = new System.Drawing.Point(106, 24);
			this.txtCh0.Name = "txtCh0";
			this.txtCh0.Size = new System.Drawing.Size(120, 20);
			this.txtCh0.TabIndex = 4;
			this.txtCh0.Text = "";
			// 
			// Label7
			// 
			this.Label7.Location = new System.Drawing.Point(8, 104);
			this.Label7.Name = "Label7";
			this.Label7.Size = new System.Drawing.Size(90, 16);
			this.Label7.TabIndex = 3;
			this.Label7.Text = "Channel 3 :";
			this.Label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Label6
			// 
			this.Label6.Location = new System.Drawing.Point(8, 77);
			this.Label6.Name = "Label6";
			this.Label6.Size = new System.Drawing.Size(90, 16);
			this.Label6.TabIndex = 2;
			this.Label6.Text = "Channel 2 :";
			this.Label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Label5
			// 
			this.Label5.Location = new System.Drawing.Point(8, 52);
			this.Label5.Name = "Label5";
			this.Label5.Size = new System.Drawing.Size(90, 16);
			this.Label5.TabIndex = 1;
			this.Label5.Text = "Channel 1 :";
			this.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Label4
			// 
			this.Label4.Location = new System.Drawing.Point(8, 25);
			this.Label4.Name = "Label4";
			this.Label4.Size = new System.Drawing.Size(90, 16);
			this.Label4.TabIndex = 0;
			this.Label4.Text = "Channel 0 :";
			this.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// GroupBox1
			// 
			this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.lblErr,
																					this.lblStart,
																					this.Label3,
																					this.Label2});
			this.GroupBox1.Location = new System.Drawing.Point(15, 51);
			this.GroupBox1.Name = "GroupBox1";
			this.GroupBox1.Size = new System.Drawing.Size(312, 56);
			this.GroupBox1.TabIndex = 18;
			this.GroupBox1.TabStop = false;
			this.GroupBox1.Text = "Start/Error Register";
			// 
			// lblErr
			// 
			this.lblErr.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblErr.Location = new System.Drawing.Point(235, 24);
			this.lblErr.Name = "lblErr";
			this.lblErr.Size = new System.Drawing.Size(40, 18);
			this.lblErr.TabIndex = 3;
			// 
			// lblStart
			// 
			this.lblStart.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblStart.Location = new System.Drawing.Point(102, 24);
			this.lblStart.Name = "lblStart";
			this.lblStart.Size = new System.Drawing.Size(40, 18);
			this.lblStart.TabIndex = 2;
			// 
			// Label3
			// 
			this.Label3.Location = new System.Drawing.Point(155, 25);
			this.Label3.Name = "Label3";
			this.Label3.Size = new System.Drawing.Size(80, 16);
			this.Label3.TabIndex = 1;
			this.Label3.Text = "Error Register";
			// 
			// Label2
			// 
			this.Label2.Location = new System.Drawing.Point(22, 25);
			this.Label2.Name = "Label2";
			this.Label2.Size = new System.Drawing.Size(80, 16);
			this.Label2.TabIndex = 0;
			this.Label2.Text = "Start Register";
			// 
			// cboDevice
			// 
			this.cboDevice.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboDevice.Location = new System.Drawing.Point(65, 16);
			this.cboDevice.Name = "cboDevice";
			this.cboDevice.Size = new System.Drawing.Size(264, 21);
			this.cboDevice.TabIndex = 17;
			// 
			// Label1
			// 
			this.Label1.Location = new System.Drawing.Point(15, 18);
			this.Label1.Name = "Label1";
			this.Label1.Size = new System.Drawing.Size(48, 16);
			this.Label1.TabIndex = 16;
			this.Label1.Text = "Device";
			// 
			// Pti4
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(512, 429);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnOK,
																		  this.btnExit,
																		  this.btnStatus,
																		  this.btnInput,
																		  this.btnStop,
																		  this.btnStart,
																		  this.btnASet,
																		  this.btnMSet,
																		  this.btnInit,
																		  this.txtRet,
																		  this.Label18,
																		  this.GroupBox3,
																		  this.GroupBox2,
																		  this.GroupBox1,
																		  this.cboDevice,
																		  this.Label1});
			this.Name = "Pti4";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Temperature/Resistance measurement sample program";
			this.Load += new System.EventHandler(this.Pti4_Load);
			this.GroupBox3.ResumeLayout(false);
			this.GroupBox2.ResumeLayout(false);
			this.GroupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		//	Initialize dialog
		private void Pti4_Load(object sender, System.EventArgs e)
		{
			// Get the list of devices registered
			int	ret;
			StringBuilder	device = new StringBuilder(50);
			StringBuilder	devicename = new StringBuilder(50);
			string	str = new string(' ', 100);
			short	index = 0;
			short	group_id, unit_id, device_id;
			group_id = 0;
			unit_id = 0;
			device_id = 0;
			do 
			{
				ret = Ccappt.PtQueryDeviceName(index, ref group_id, ref unit_id, ref device_id, devicename, device);
				if (ret == 0)
				{
					str = string.Format("{0} [{1}]", devicename, device);
					cboDevice.Items.Add(str);
					index++;
				}
			} while (ret == 0);
			cboDevice.SelectedIndex = 0;

			txtCh0.Text = "0";
			txtCh1.Text = "0";
			txtCh2.Text = "0";
			txtCh3.Text = "0";
		}

		//	Click the [Initialization] button
		private void btnInit_Click(object sender, System.EventArgs e)
		{
			int	ret;
			StringBuilder	devicename = new StringBuilder(50);
			StringBuilder	tmp = new StringBuilder(50);
			short	index;
			short	group_id, unit_id, device_id;
			// Detect device
			index = (short)cboDevice.SelectedIndex;
			group_id = 0;
			unit_id = 0;
			device_id = 0;
			ret = Ccappt.PtQueryDeviceName(index, ref group_id, ref unit_id, ref device_id, devicename, tmp);
			if (ret != 0)
			{
				ErrorHandling("PtQueryDeviceName", ret);
				return;
			}

			// Initialization
			ret = Ccappt.PtInit(devicename, ref m_Id);
			if (ret != 0)
			{
				ErrorHandling("PtInit", ret);
				return;
			}

			txtRet.Text = "Initialization : Successed";
			ShowRemoteStatus();
		}

		//	Click the [Measurement Setting] button
		private void btnMSet_Click(object sender, System.EventArgs e)
		{
			Measure	MeasureDlg = new Measure();

			// Invoke the Measurement Setting dialog
			MeasureDlg.SetId(m_Id);
			MeasureDlg.ShowDialog();
			MeasureDlg.Dispose();
		}

		//	Click the [Alarm Setting] button
		private void btnASet_Click(object sender, System.EventArgs e)
		{
			Alarm	AlarmDlg = new Alarm();

			// Invoke the Alarm Setting dialog
			AlarmDlg.SetId(m_Id);
			AlarmDlg.ShowDialog();
			AlarmDlg.Dispose();
		}

		//	Click the [Start] button
		private void btnStart_Click(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Start
			//-----------------------------------
			int	ret;
			ret = Ccappt.PtStartIO(m_Id, 0x01);
			//-----------------------------------
			// Process error
			//-----------------------------------
			if (ret != 0)
			{
				ErrorHandling("PtStartIO", ret);
				return;
			}
			txtRet.Text = "Start : Successed";
			ShowRemoteStatus();
		}

		//	Click the [Stop] button
		private void btnStop_Click(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Stop
			//-----------------------------------
			int	ret;
			ret = Ccappt.PtStartIO(m_Id, 0x00);
			//-----------------------------------
			// Process error
			//-----------------------------------
			if (ret != 0)
			{
				ErrorHandling("PtStartIO", ret);
				return;
			}
			txtRet.Text = "Stop : Successed";
			ShowRemoteStatus();
		}

		//	Click the [Input Value] button
		private void btnInput_Click(object sender, System.EventArgs e)
		{
			int	ret;
			float[]	data = new float[4];

			// Input the temperature or capacity value for 4 channels
			GCHandle Pter = GCHandle.Alloc(data, GCHandleType.Pinned);
			ret = Ccappt.PtMultiInputEx(m_Id, 4, data);
			Pter.Free();
			if (ret != 0)
			{
				ErrorHandling("PtMultiInputEx", ret);
				if (ret != 20102)
				{
					return;
				}
			}

			txtCh0.Text = string.Format("{0:f}", data[0]);
			txtCh1.Text = string.Format("{0:f}", data[1]);
			txtCh2.Text = string.Format("{0:f}", data[2]);
			txtCh3.Text = string.Format("{0:f}", data[3]);
			
			txtRet.Text = "Input Value : Successed";
		}

		//	Click the [Status] button
		private void btnStatus_Click(object sender, System.EventArgs e)
		{
			int	ret;
			int	disconnectstatus, upperstatus, lowerstatus;
			string	str = new string(' ', 32);

			// Get the disconnect status
			disconnectstatus = 0;
			upperstatus = 0;
			lowerstatus = 0;
			ret = Ccappt.PtGetDisconnectStatus(m_Id, ref disconnectstatus);
			if (ret != 0)
			{
				ErrorHandling("PtGetDisconnectStatus", ret);
				return;
			}

			// Get the alarm status
			ret = Ccappt.PtGetCurrentAlarmStatus(m_Id, ref upperstatus, ref lowerstatus);
			if (ret != 0)
			{
				ErrorHandling("PtGetCurrentAlarmStatus", ret);
				return;
			}

			str = string.Format("{0:X}", disconnectstatus);
			txtStsDc.Text = str;
			str = string.Format("{0:X}", upperstatus);
			txtStsUAlarm.Text = str;
			str = string.Format("{0:X}", lowerstatus);
			txtStsLAlarm.Text = str;

			txtRet.Text = "Getting Status : Successed";
		}

		//	Click the [Termination] button
		private void btnExit_Click(object sender, System.EventArgs e)
		{
			int	ret;

			// Termination
			ret = Ccappt.PtExit(m_Id);	
			if (ret != 0)
			{
				ErrorHandling("PtExit", ret);
				return;
			}

			txtRet.Text = "Termination : Successed";
		}

		//	Exit dialog
		private void btnOK_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		// Process error
		private void ErrorHandling(string Dfunction, int ret)
		{
			StringBuilder	errstr = new StringBuilder(256);
			string	str = new string(' ', 256);

			Ccappt.PtGetErrorString(ret, errstr);
			str = string.Format("[{0}] {1}", Dfunction, errstr);
			txtRet.Text = str;
		}

		// Display remote status
		private void ShowRemoteStatus()
		{
			byte	Start, Err;
			string	szText = new string(' ', 100);
			int	ret;

			// Input remote status
			Start = 0;
			Err = 0;
			ret = Ccappt.PtGetRemoteStatus(m_Id, ref Start, ref Err);
			// Process error
			if (ret != 0)
			{
				ErrorHandling("PtGetRemoteStatus", ret);
				return;
			}
			// Display remote status
			szText = string.Format("{0:X2}", Start);
			lblStart.Text = szText;
			szText = string.Format("{0:X2}", Err);
			lblErr.Text = szText;
		}

		private void Pti4_Closed(object sender, System.EventArgs e)
		{
			int	ret;

			// Termination
			ret = Ccappt.PtExit(m_Id);	
		}
	}
}
