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

namespace SimpleIO
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class SimpleIO : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.Label Label1;
		internal System.Windows.Forms.GroupBox GroupBox4;
		internal System.Windows.Forms.Label Label12;
		internal System.Windows.Forms.Label Label13;
		internal System.Windows.Forms.GroupBox GroupBox3;
		internal System.Windows.Forms.Label Label8;
		internal System.Windows.Forms.Label Label9;
		internal System.Windows.Forms.Label Label10;
		internal System.Windows.Forms.GroupBox GroupBox2;
		internal System.Windows.Forms.Label Label6;
		internal System.Windows.Forms.Label Label7;
		internal System.Windows.Forms.GroupBox GroupBox1;
		internal System.Windows.Forms.Label Label4;
		internal System.Windows.Forms.Label Label3;
		internal System.Windows.Forms.Label Label2;
		internal System.Windows.Forms.Label Label5;
		internal System.Windows.Forms.ComboBox cboDeviceName;
		internal System.Windows.Forms.Button btnExit;
		internal System.Windows.Forms.Button btnEchoBackBit;
		internal System.Windows.Forms.Button btnOutBit;
		internal System.Windows.Forms.TextBox txtOutBitData;
		internal System.Windows.Forms.TextBox txtOutBitNo;
		internal System.Windows.Forms.Button btnEchoBackByte;
		internal System.Windows.Forms.Button btnOutByte;
		internal System.Windows.Forms.TextBox txtOutPortData;
		internal System.Windows.Forms.TextBox txtOutPortNo;
		internal System.Windows.Forms.TextBox txtReturnCode;
		internal System.Windows.Forms.Button btnInpBit;
		internal System.Windows.Forms.TextBox txtInpBitData;
		internal System.Windows.Forms.TextBox txtInpBitNo;
		internal System.Windows.Forms.Button btnDioExit;
		internal System.Windows.Forms.Button btnInpByte;
		internal System.Windows.Forms.TextBox txtInpPortData;
		internal System.Windows.Forms.TextBox txtInpPortNo;
		internal System.Windows.Forms.Button btnDioInit;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		// Additional data
		short			Id;											// Device ID
		int				Ret;										// Return value
		StringBuilder	szError = new StringBuilder("", 256);		// Error string
		string			szReturnCode = new string(' ', 256);		// The string for error
		string			szText = new string(' ', 100);

		public SimpleIO()
		{
			//
			// 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.cboDeviceName = new System.Windows.Forms.ComboBox();
			this.btnExit = new System.Windows.Forms.Button();
			this.GroupBox4 = new System.Windows.Forms.GroupBox();
			this.btnEchoBackBit = new System.Windows.Forms.Button();
			this.btnOutBit = new System.Windows.Forms.Button();
			this.txtOutBitData = new System.Windows.Forms.TextBox();
			this.txtOutBitNo = new System.Windows.Forms.TextBox();
			this.Label12 = new System.Windows.Forms.Label();
			this.Label13 = new System.Windows.Forms.Label();
			this.GroupBox3 = new System.Windows.Forms.GroupBox();
			this.btnEchoBackByte = new System.Windows.Forms.Button();
			this.btnOutByte = new System.Windows.Forms.Button();
			this.txtOutPortData = new System.Windows.Forms.TextBox();
			this.txtOutPortNo = new System.Windows.Forms.TextBox();
			this.Label8 = new System.Windows.Forms.Label();
			this.Label9 = new System.Windows.Forms.Label();
			this.Label10 = new System.Windows.Forms.Label();
			this.txtReturnCode = new System.Windows.Forms.TextBox();
			this.GroupBox2 = new System.Windows.Forms.GroupBox();
			this.btnInpBit = new System.Windows.Forms.Button();
			this.txtInpBitData = new System.Windows.Forms.TextBox();
			this.txtInpBitNo = new System.Windows.Forms.TextBox();
			this.Label6 = new System.Windows.Forms.Label();
			this.Label7 = new System.Windows.Forms.Label();
			this.btnDioExit = new System.Windows.Forms.Button();
			this.GroupBox1 = new System.Windows.Forms.GroupBox();
			this.btnInpByte = new System.Windows.Forms.Button();
			this.txtInpPortData = new System.Windows.Forms.TextBox();
			this.txtInpPortNo = 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.Label5 = new System.Windows.Forms.Label();
			this.btnDioInit = new System.Windows.Forms.Button();
			this.GroupBox4.SuspendLayout();
			this.GroupBox3.SuspendLayout();
			this.GroupBox2.SuspendLayout();
			this.GroupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// Label1
			// 
			this.Label1.Location = new System.Drawing.Point(12, 12);
			this.Label1.Name = "Label1";
			this.Label1.Size = new System.Drawing.Size(78, 19);
			this.Label1.TabIndex = 37;
			this.Label1.Text = "Device Name :";
			// 
			// cboDeviceName
			// 
			this.cboDeviceName.Location = new System.Drawing.Point(95, 11);
			this.cboDeviceName.Name = "cboDeviceName";
			this.cboDeviceName.Size = new System.Drawing.Size(200, 21);
			this.cboDeviceName.TabIndex = 36;
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(364, 419);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(105, 25);
			this.btnExit.TabIndex = 34;
			this.btnExit.Text = "Exit";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// GroupBox4
			// 
			this.GroupBox4.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnEchoBackBit,
																					this.btnOutBit,
																					this.txtOutBitData,
																					this.txtOutBitNo,
																					this.Label12,
																					this.Label13});
			this.GroupBox4.Location = new System.Drawing.Point(12, 298);
			this.GroupBox4.Name = "GroupBox4";
			this.GroupBox4.Size = new System.Drawing.Size(580, 70);
			this.GroupBox4.TabIndex = 32;
			this.GroupBox4.TabStop = false;
			this.GroupBox4.Text = "1 Bit Output";
			// 
			// btnEchoBackBit
			// 
			this.btnEchoBackBit.Location = new System.Drawing.Point(464, 24);
			this.btnEchoBackBit.Name = "btnEchoBackBit";
			this.btnEchoBackBit.Size = new System.Drawing.Size(105, 25);
			this.btnEchoBackBit.TabIndex = 14;
			this.btnEchoBackBit.Text = "DioEchoBackBit";
			this.btnEchoBackBit.Click += new System.EventHandler(this.btnEchoBackBit_Click);
			// 
			// btnOutBit
			// 
			this.btnOutBit.Location = new System.Drawing.Point(352, 24);
			this.btnOutBit.Name = "btnOutBit";
			this.btnOutBit.Size = new System.Drawing.Size(105, 25);
			this.btnOutBit.TabIndex = 13;
			this.btnOutBit.Text = "DioOutBit";
			this.btnOutBit.Click += new System.EventHandler(this.btnOutBit_Click);
			// 
			// txtOutBitData
			// 
			this.txtOutBitData.Location = new System.Drawing.Point(236, 26);
			this.txtOutBitData.Name = "txtOutBitData";
			this.txtOutBitData.Size = new System.Drawing.Size(45, 20);
			this.txtOutBitData.TabIndex = 12;
			this.txtOutBitData.Text = "TextBox2";
			// 
			// txtOutBitNo
			// 
			this.txtOutBitNo.Location = new System.Drawing.Point(86, 26);
			this.txtOutBitNo.Name = "txtOutBitNo";
			this.txtOutBitNo.Size = new System.Drawing.Size(45, 20);
			this.txtOutBitNo.TabIndex = 11;
			this.txtOutBitNo.Text = "TextBox1";
			// 
			// Label12
			// 
			this.Label12.Location = new System.Drawing.Point(166, 27);
			this.Label12.Name = "Label12";
			this.Label12.Size = new System.Drawing.Size(72, 19);
			this.Label12.TabIndex = 2;
			this.Label12.Text = "Output Data :";
			// 
			// Label13
			// 
			this.Label13.Location = new System.Drawing.Point(10, 27);
			this.Label13.Name = "Label13";
			this.Label13.Size = new System.Drawing.Size(72, 19);
			this.Label13.TabIndex = 1;
			this.Label13.Text = "Logic BitNo :";
			// 
			// GroupBox3
			// 
			this.GroupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnEchoBackByte,
																					this.btnOutByte,
																					this.txtOutPortData,
																					this.txtOutPortNo,
																					this.Label8,
																					this.Label9,
																					this.Label10});
			this.GroupBox3.Location = new System.Drawing.Point(12, 214);
			this.GroupBox3.Name = "GroupBox3";
			this.GroupBox3.Size = new System.Drawing.Size(580, 70);
			this.GroupBox3.TabIndex = 31;
			this.GroupBox3.TabStop = false;
			this.GroupBox3.Text = "1 Port Output";
			// 
			// btnEchoBackByte
			// 
			this.btnEchoBackByte.Location = new System.Drawing.Point(464, 24);
			this.btnEchoBackByte.Name = "btnEchoBackByte";
			this.btnEchoBackByte.Size = new System.Drawing.Size(105, 25);
			this.btnEchoBackByte.TabIndex = 10;
			this.btnEchoBackByte.Text = "DioEchoBackByte";
			this.btnEchoBackByte.Click += new System.EventHandler(this.btnEchoBackByte_Click);
			// 
			// btnOutByte
			// 
			this.btnOutByte.Location = new System.Drawing.Point(352, 24);
			this.btnOutByte.Name = "btnOutByte";
			this.btnOutByte.Size = new System.Drawing.Size(105, 25);
			this.btnOutByte.TabIndex = 9;
			this.btnOutByte.Text = "DioOutByte";
			this.btnOutByte.Click += new System.EventHandler(this.btnOutByte_Click);
			// 
			// txtOutPortData
			// 
			this.txtOutPortData.Location = new System.Drawing.Point(236, 26);
			this.txtOutPortData.Name = "txtOutPortData";
			this.txtOutPortData.Size = new System.Drawing.Size(45, 20);
			this.txtOutPortData.TabIndex = 8;
			this.txtOutPortData.Text = "TextBox2";
			// 
			// txtOutPortNo
			// 
			this.txtOutPortNo.Location = new System.Drawing.Point(86, 26);
			this.txtOutPortNo.Name = "txtOutPortNo";
			this.txtOutPortNo.Size = new System.Drawing.Size(45, 20);
			this.txtOutPortNo.TabIndex = 7;
			this.txtOutPortNo.Text = "TextBox1";
			// 
			// Label8
			// 
			this.Label8.Location = new System.Drawing.Point(290, 27);
			this.Label8.Name = "Label8";
			this.Label8.Size = new System.Drawing.Size(38, 19);
			this.Label8.TabIndex = 3;
			this.Label8.Text = "(Hex)";
			// 
			// Label9
			// 
			this.Label9.Location = new System.Drawing.Point(166, 27);
			this.Label9.Name = "Label9";
			this.Label9.Size = new System.Drawing.Size(78, 19);
			this.Label9.TabIndex = 2;
			this.Label9.Text = "Output Data :";
			// 
			// Label10
			// 
			this.Label10.Location = new System.Drawing.Point(10, 27);
			this.Label10.Name = "Label10";
			this.Label10.Size = new System.Drawing.Size(78, 19);
			this.Label10.TabIndex = 1;
			this.Label10.Text = "Logic PortNo :";
			// 
			// txtReturnCode
			// 
			this.txtReturnCode.Location = new System.Drawing.Point(87, 384);
			this.txtReturnCode.Name = "txtReturnCode";
			this.txtReturnCode.Size = new System.Drawing.Size(264, 20);
			this.txtReturnCode.TabIndex = 35;
			this.txtReturnCode.Text = "";
			// 
			// GroupBox2
			// 
			this.GroupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnInpBit,
																					this.txtInpBitData,
																					this.txtInpBitNo,
																					this.Label6,
																					this.Label7});
			this.GroupBox2.Location = new System.Drawing.Point(12, 130);
			this.GroupBox2.Name = "GroupBox2";
			this.GroupBox2.Size = new System.Drawing.Size(580, 70);
			this.GroupBox2.TabIndex = 30;
			this.GroupBox2.TabStop = false;
			this.GroupBox2.Text = "1 Bit Input";
			// 
			// btnInpBit
			// 
			this.btnInpBit.Location = new System.Drawing.Point(352, 24);
			this.btnInpBit.Name = "btnInpBit";
			this.btnInpBit.Size = new System.Drawing.Size(105, 25);
			this.btnInpBit.TabIndex = 6;
			this.btnInpBit.Text = "DioInpBit";
			this.btnInpBit.Click += new System.EventHandler(this.btnInpBit_Click);
			// 
			// txtInpBitData
			// 
			this.txtInpBitData.Location = new System.Drawing.Point(236, 26);
			this.txtInpBitData.Name = "txtInpBitData";
			this.txtInpBitData.Size = new System.Drawing.Size(45, 20);
			this.txtInpBitData.TabIndex = 5;
			this.txtInpBitData.Text = "";
			// 
			// txtInpBitNo
			// 
			this.txtInpBitNo.Location = new System.Drawing.Point(86, 26);
			this.txtInpBitNo.Name = "txtInpBitNo";
			this.txtInpBitNo.Size = new System.Drawing.Size(45, 20);
			this.txtInpBitNo.TabIndex = 4;
			this.txtInpBitNo.Text = "TextBox1";
			// 
			// Label6
			// 
			this.Label6.Location = new System.Drawing.Point(166, 27);
			this.Label6.Name = "Label6";
			this.Label6.Size = new System.Drawing.Size(66, 19);
			this.Label6.TabIndex = 2;
			this.Label6.Text = "Input Data :";
			// 
			// Label7
			// 
			this.Label7.Location = new System.Drawing.Point(10, 27);
			this.Label7.Name = "Label7";
			this.Label7.Size = new System.Drawing.Size(70, 19);
			this.Label7.TabIndex = 1;
			this.Label7.Text = "Logic BitNo :";
			// 
			// btnDioExit
			// 
			this.btnDioExit.Location = new System.Drawing.Point(364, 382);
			this.btnDioExit.Name = "btnDioExit";
			this.btnDioExit.Size = new System.Drawing.Size(105, 25);
			this.btnDioExit.TabIndex = 33;
			this.btnDioExit.Text = "DioExit";
			this.btnDioExit.Click += new System.EventHandler(this.btnDioExit_Click);
			// 
			// GroupBox1
			// 
			this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnInpByte,
																					this.txtInpPortData,
																					this.txtInpPortNo,
																					this.Label4,
																					this.Label3,
																					this.Label2});
			this.GroupBox1.Location = new System.Drawing.Point(12, 47);
			this.GroupBox1.Name = "GroupBox1";
			this.GroupBox1.Size = new System.Drawing.Size(580, 69);
			this.GroupBox1.TabIndex = 29;
			this.GroupBox1.TabStop = false;
			this.GroupBox1.Text = "1 Port Input";
			// 
			// btnInpByte
			// 
			this.btnInpByte.Location = new System.Drawing.Point(352, 24);
			this.btnInpByte.Name = "btnInpByte";
			this.btnInpByte.Size = new System.Drawing.Size(105, 25);
			this.btnInpByte.TabIndex = 3;
			this.btnInpByte.Text = "DioInpByte";
			this.btnInpByte.Click += new System.EventHandler(this.btnInpByte_Click);
			// 
			// txtInpPortData
			// 
			this.txtInpPortData.Location = new System.Drawing.Point(236, 26);
			this.txtInpPortData.Name = "txtInpPortData";
			this.txtInpPortData.Size = new System.Drawing.Size(45, 20);
			this.txtInpPortData.TabIndex = 2;
			this.txtInpPortData.Text = "";
			// 
			// txtInpPortNo
			// 
			this.txtInpPortNo.Location = new System.Drawing.Point(86, 26);
			this.txtInpPortNo.Name = "txtInpPortNo";
			this.txtInpPortNo.Size = new System.Drawing.Size(45, 20);
			this.txtInpPortNo.TabIndex = 1;
			this.txtInpPortNo.Text = "TextBox1";
			// 
			// Label4
			// 
			this.Label4.Location = new System.Drawing.Point(290, 27);
			this.Label4.Name = "Label4";
			this.Label4.Size = new System.Drawing.Size(38, 19);
			this.Label4.TabIndex = 3;
			this.Label4.Text = "(Hex)";
			// 
			// Label3
			// 
			this.Label3.Location = new System.Drawing.Point(166, 27);
			this.Label3.Name = "Label3";
			this.Label3.Size = new System.Drawing.Size(74, 19);
			this.Label3.TabIndex = 2;
			this.Label3.Text = "Input Data :";
			// 
			// Label2
			// 
			this.Label2.Location = new System.Drawing.Point(10, 27);
			this.Label2.Name = "Label2";
			this.Label2.Size = new System.Drawing.Size(78, 19);
			this.Label2.TabIndex = 1;
			this.Label2.Text = "Logic PortNo :";
			// 
			// Label5
			// 
			this.Label5.Location = new System.Drawing.Point(12, 385);
			this.Label5.Name = "Label5";
			this.Label5.Size = new System.Drawing.Size(83, 18);
			this.Label5.TabIndex = 38;
			this.Label5.Text = "Return Value :";
			// 
			// btnDioInit
			// 
			this.btnDioInit.Location = new System.Drawing.Point(364, 9);
			this.btnDioInit.Name = "btnDioInit";
			this.btnDioInit.Size = new System.Drawing.Size(105, 25);
			this.btnDioInit.TabIndex = 28;
			this.btnDioInit.Text = "DioInit";
			this.btnDioInit.Click += new System.EventHandler(this.btnDioInit_Click);
			// 
			// SimpleIO
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(604, 457);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.Label1,
																		  this.cboDeviceName,
																		  this.btnExit,
																		  this.GroupBox4,
																		  this.GroupBox3,
																		  this.txtReturnCode,
																		  this.GroupBox2,
																		  this.btnDioExit,
																		  this.GroupBox1,
																		  this.Label5,
																		  this.btnDioInit});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "SimpleIO";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Simple I/O";
			this.Load += new System.EventHandler(this.SimpleIO_Load);
			this.GroupBox4.ResumeLayout(false);
			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 SimpleIO());
		}

		//---------------------------------------------------------
		//	Initialize dialog
		//---------------------------------------------------------
		private void SimpleIO_Load(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Detect device
			//-----------------------------------
			short	Index;
			short	GroupID, UnitID, DeviceID;
			StringBuilder	DeviceName = new StringBuilder(256);
			StringBuilder	Device = new StringBuilder(256);
			GroupID = 0;
			UnitID = 0;
			DeviceID = 0;
			for (Index=0; ; Index++) 
			{
				Ret = DIO.Ccapdio.DioQueryDeviceName(Index, ref GroupID, ref UnitID, ref DeviceID, DeviceName, Device);
				if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS) 
				{
					break;
				}
				cboDeviceName.Items.Add(DeviceName);
			}
			cboDeviceName.SelectedIndex = 0;
			//-----------------------------------
			// Initialize screen items
			//-----------------------------------
			txtInpPortNo.Text = "0";
			txtInpBitNo.Text = "0";
			txtOutPortNo.Text = "0";
			txtOutBitNo.Text = "0";
			txtOutPortData.Text = "55";
			txtOutBitData.Text = "1";
			txtReturnCode.Text = "";
		}

		//---------------------------------------------------------
		//	Initialization
		//---------------------------------------------------------
		private void btnDioInit_Click(object sender, System.EventArgs e)
		{
			string	szDeviceName = new string(' ', 100);
			//-----------------------------
			// Get device name from screen
			//-----------------------------
			szDeviceName = cboDeviceName.Text;
			//-----------------------------
			// Initialize and get ID
			//-----------------------------
			Ret = DIO.Ccapdio.DioInit(szDeviceName, ref Id);
			//-----------------------------
			// Process error
			//-----------------------------
			DIO.Ccapdio.DioGetErrorString(Ret, szError);
			szReturnCode = "DioInit : " + szError;
			txtReturnCode.Text = szReturnCode;
		}

		//---------------------------------------------------------
		//	1 port input
		//---------------------------------------------------------
		private void btnInpByte_Click(object sender, System.EventArgs e)
		{
			short	InpPortNo;
			byte	InpPortData;
			InpPortData = 0;
			//-----------------------------
			// Get port No. from screen
			//-----------------------------
			szText = txtInpPortNo.Text;
			try
			{
				InpPortNo = Convert.ToInt16(szText);
			}
			catch (System.FormatException)
			{
				InpPortNo = 0;
			}
			//-----------------------------
			// Port input
			//-----------------------------
			Ret = DIO.Ccapdio.DioInpByte(Id, InpPortNo, ref InpPortData);
			//-----------------------------
			// Process error
			//-----------------------------
			DIO.Ccapdio.DioGetErrorString(Ret, szError);
			szReturnCode = "DioInpByte : " + szError;
			txtReturnCode.Text = szReturnCode;
			if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS) 
			{
				return;
			}
			//-----------------------------
			// Display data
			//-----------------------------
			szText = string.Format("{0:X2}", InpPortData);
			txtInpPortData.Text = szText;
		}

		//---------------------------------------------------------
		//	1 bit input
		//---------------------------------------------------------
		private void btnInpBit_Click(object sender, System.EventArgs e)
		{
			short	InpBitNo;
			byte	InpBitData;
			InpBitData = 0;
			//-----------------------------
			// Get bit No. from screen
			//-----------------------------
			szText = txtInpBitNo.Text;
			try
			{
				InpBitNo = Convert.ToInt16(szText);
			}
			catch (System.FormatException)
			{
				InpBitNo = 0;
			}
			//-----------------------------
			// Bit input
			//-----------------------------
			Ret = DIO.Ccapdio.DioInpBit(Id, InpBitNo, ref InpBitData);
			//-----------------------------
			// Process error
			//-----------------------------
			DIO.Ccapdio.DioGetErrorString(Ret, szError);
			szReturnCode = "DioInpBit : " + szError;
			txtReturnCode.Text = szReturnCode;
			if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS) 
			{
				return;
			}
			//-----------------------------
			// Display data
			//-----------------------------
			szText = string.Format("{0:X}", InpBitData);
			txtInpBitData.Text = szText;
		}

		//---------------------------------------------------------
		//	1 port output
		//---------------------------------------------------------
		private void btnOutByte_Click(object sender, System.EventArgs e)
		{
			short	OutPortNo;
			byte	OutPortData;
			uint	dwTmp;
			//-----------------------------
			// Get the port No. and the output data from screen
			//-----------------------------
			szText = txtOutPortNo.Text;
			try
			{
				OutPortNo = Convert.ToInt16(szText);			
			}
			catch (System.FormatException)
			{
				OutPortNo = 0;
			}
			szText = txtOutPortData.Text; 
			try
			{
				dwTmp = uint.Parse(szText, NumberStyles.HexNumber);
			}
			catch (System.FormatException)
			{
				dwTmp = 0;
			}
			OutPortData = (byte)dwTmp;
			//-----------------------------
			// Port output
			//-----------------------------
			Ret = DIO.Ccapdio.DioOutByte(Id, OutPortNo, OutPortData);
			//-----------------------------
			// Process error
			//-----------------------------
			DIO.Ccapdio.DioGetErrorString(Ret, szError);
			szReturnCode = "DioOutByte : " + szError;
			txtReturnCode.Text = szReturnCode;
			if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS) 
			{
				return;
			}
		}

		//---------------------------------------------------------
		//	1 bit output
		//---------------------------------------------------------
		private void btnOutBit_Click(object sender, System.EventArgs e)
		{
			short	OutBitNo;
			byte	OutBitData;
			uint	dwTmp;
			//-----------------------------
			// Get the bit No. and the output data from screen
			//-----------------------------
			szText = txtOutBitNo.Text;
			try
			{
				OutBitNo = Convert.ToInt16(szText);
			}
			catch (System.FormatException)
			{
				OutBitNo = 0;
			}
			szText = txtOutBitData.Text;
			try
			{
				dwTmp = uint.Parse(szText, NumberStyles.HexNumber);
			}
			catch (System.FormatException)
			{
				dwTmp = 0;
			}
			OutBitData = (byte)dwTmp;
			//-----------------------------
			// Bit output
			//-----------------------------
			Ret = DIO.Ccapdio.DioOutBit(Id, OutBitNo, OutBitData);
			//-----------------------------
			// Process error
			//-----------------------------
			DIO.Ccapdio.DioGetErrorString(Ret, szError);
			szReturnCode = "DioOutBit : " + szError;
			txtReturnCode.Text = szReturnCode;
			if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS) 
			{
				return;
			}	
		}

		//---------------------------------------------------------
		//	1 port EchoBack
		//---------------------------------------------------------
		private void btnEchoBackByte_Click(object sender, System.EventArgs e)
		{
			short	OutPortNo;
			byte	OutPortData;
			OutPortData = 0;
			//-----------------------------
			// Get port No. from screen
			//-----------------------------
			szText = txtOutPortNo.Text;
			try
			{
				OutPortNo = Convert.ToInt16(szText);
			}
			catch (System.FormatException)
			{
				OutPortNo = 0;
			}
			//-----------------------------
			// Port EchoBack
			//-----------------------------
			Ret = DIO.Ccapdio.DioEchoBackByte(Id, OutPortNo, ref OutPortData);
			//-----------------------------
			// Process error
			//-----------------------------
			DIO.Ccapdio.DioGetErrorString(Ret, szError);
			szReturnCode = "DioEchoBackByte : " + szError;
			txtReturnCode.Text = szReturnCode;	
			if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS) 
			{
				return;
			}
			//-----------------------------
			// Display data
			//-----------------------------
			szText = string.Format("{0:X2}", OutPortData);
			txtOutPortData.Text = szText;
		}

		//---------------------------------------------------------
		//	1 Bit EchoBack
		//---------------------------------------------------------
		private void btnEchoBackBit_Click(object sender, System.EventArgs e)
		{
			short	OutBitNo;
			byte	OutBitData;
			OutBitData = 0;
			//-----------------------------
			// Get bit No. from screen
			//-----------------------------
			szText = txtOutBitNo.Text;
			try
			{
				OutBitNo = Convert.ToInt16(szText);
			}
			catch (System.FormatException)
			{
				OutBitNo = 0;
			}
			//-----------------------------
			// Bit EchoBack
			//-----------------------------
			Ret = DIO.Ccapdio.DioEchoBackBit(Id, OutBitNo, ref OutBitData);
			//-----------------------------
			// Process error
			//-----------------------------
			DIO.Ccapdio.DioGetErrorString(Ret, szError);
			szReturnCode = "DioEchoBackBit : " + szError;
			txtReturnCode.Text = szReturnCode;	
			if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS) 
			{
				return;
			}
			//-----------------------------
			// Display data
			//-----------------------------
			szText = string.Format("{0:X}", OutBitData);
			txtOutBitData.Text = szText;
		}

		//---------------------------------------------------------
		//	Termination
		//---------------------------------------------------------
		private void btnDioExit_Click(object sender, System.EventArgs e)
		{
			//-----------------------------
			// Termination
			//-----------------------------
			Ret = DIO.Ccapdio.DioExit(Id);
			//-----------------------------
			// Process error
			//-----------------------------
			DIO.Ccapdio.DioGetErrorString(Ret, szError);
			szReturnCode = "DioExit : " + szError;
			txtReturnCode.Text = szReturnCode;	
		}

		//---------------------------------------------------------
		//	Exit dialog
		//---------------------------------------------------------
		private void btnExit_Click(object sender, System.EventArgs e)
		{
			//-----------------------------
			// Termination
			//-----------------------------
			Ret = DIO.Ccapdio.DioExit(Id);
			//-----------------------------
			// Exit dialog
			//-----------------------------
			this.Close();
		}
		
		private void SimpleIO_Closed(object sender, System.EventArgs e)
		{
			//-----------------------------
			// Termination
			//-----------------------------
			Ret = DIO.Ccapdio.DioExit(Id);
		}
	}
}