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

namespace SingleAi
{
	/// <summary>
	/// Form1 ̊Tv̐łB
	/// </summary>
	public class SingleAi : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.Label Label5;
		internal System.Windows.Forms.Label Label4;
		internal System.Windows.Forms.Label Label3;
		internal System.Windows.Forms.Label Label2;
		internal System.Windows.Forms.Label Label1;
		internal System.Windows.Forms.Label lblErrorString;
		internal System.Windows.Forms.Label lblVolt;
		internal System.Windows.Forms.TextBox txtAiChannel;
		internal System.Windows.Forms.Label lblCData;
		internal System.Windows.Forms.Label lblErr;
		internal System.Windows.Forms.ComboBox cboAioSetAiRange;
		internal System.Windows.Forms.Button btnAioExit;
		internal System.Windows.Forms.Button btnAioSingleAi;
		internal System.Windows.Forms.Button btnStop;
		internal System.Windows.Forms.Button btnStart;
		internal System.Windows.Forms.Button btnAioInit;
		internal System.Windows.Forms.ComboBox cboDeviceName;
		internal System.Windows.Forms.Label lblStart;
		/// <summary>
		/// KvȃfUCiϐłB
		/// </summary>
		private System.ComponentModel.Container components = null;
		// ֐̖߂l
		int				Ret;
		// ID
		short			Id;
		// G[R[h
		StringBuilder	ErrorString = new StringBuilder(256);
		// 񏑎쐬p
		string			TextString;

		public SingleAi()
		{
			//
			// 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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SingleAi));
			this.lblErrorString = new System.Windows.Forms.Label();
			this.lblVolt = new System.Windows.Forms.Label();
			this.txtAiChannel = new System.Windows.Forms.TextBox();
			this.lblCData = new System.Windows.Forms.Label();
			this.Label5 = new System.Windows.Forms.Label();
			this.lblErr = new System.Windows.Forms.Label();
			this.Label4 = new System.Windows.Forms.Label();
			this.Label3 = new System.Windows.Forms.Label();
			this.cboAioSetAiRange = new System.Windows.Forms.ComboBox();
			this.btnAioExit = new System.Windows.Forms.Button();
			this.btnAioSingleAi = new System.Windows.Forms.Button();
			this.btnStop = new System.Windows.Forms.Button();
			this.btnStart = new System.Windows.Forms.Button();
			this.Label2 = new System.Windows.Forms.Label();
			this.btnAioInit = new System.Windows.Forms.Button();
			this.cboDeviceName = new System.Windows.Forms.ComboBox();
			this.Label1 = new System.Windows.Forms.Label();
			this.lblStart = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// lblErrorString
			// 
			this.lblErrorString.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblErrorString.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.lblErrorString.Location = new System.Drawing.Point(18, 208);
			this.lblErrorString.Name = "lblErrorString";
			this.lblErrorString.Size = new System.Drawing.Size(272, 32);
			this.lblErrorString.TabIndex = 36;
			// 
			// lblVolt
			// 
			this.lblVolt.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblVolt.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.lblVolt.Location = new System.Drawing.Point(169, 172);
			this.lblVolt.Name = "lblVolt";
			this.lblVolt.Size = new System.Drawing.Size(73, 19);
			this.lblVolt.TabIndex = 35;
			// 
			// txtAiChannel
			// 
			this.txtAiChannel.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.txtAiChannel.Location = new System.Drawing.Point(110, 172);
			this.txtAiChannel.Name = "txtAiChannel";
			this.txtAiChannel.Size = new System.Drawing.Size(52, 19);
			this.txtAiChannel.TabIndex = 23;
			this.txtAiChannel.Text = "";
			// 
			// lblCData
			// 
			this.lblCData.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.lblCData.Location = new System.Drawing.Point(166, 151);
			this.lblCData.Name = "lblCData";
			this.lblCData.Size = new System.Drawing.Size(39, 15);
			this.lblCData.TabIndex = 34;
			this.lblCData.Text = "d";
			// 
			// Label5
			// 
			this.Label5.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.Label5.Location = new System.Drawing.Point(110, 152);
			this.Label5.Name = "Label5";
			this.Label5.Size = new System.Drawing.Size(69, 15);
			this.Label5.TabIndex = 33;
			this.Label5.Text = "`l";
			// 
			// lblErr
			// 
			this.lblErr.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblErr.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.lblErr.Location = new System.Drawing.Point(193, 117);
			this.lblErr.Name = "lblErr";
			this.lblErr.Size = new System.Drawing.Size(39, 17);
			this.lblErr.TabIndex = 32;
			// 
			// Label4
			// 
			this.Label4.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.Label4.Location = new System.Drawing.Point(189, 100);
			this.Label4.Name = "Label4";
			this.Label4.Size = new System.Drawing.Size(83, 15);
			this.Label4.TabIndex = 30;
			this.Label4.Text = "G[WX^";
			// 
			// Label3
			// 
			this.Label3.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.Label3.Location = new System.Drawing.Point(111, 100);
			this.Label3.Name = "Label3";
			this.Label3.Size = new System.Drawing.Size(75, 15);
			this.Label3.TabIndex = 29;
			this.Label3.Text = "NWX^";
			// 
			// cboAioSetAiRange
			// 
			this.cboAioSetAiRange.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboAioSetAiRange.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.cboAioSetAiRange.ItemHeight = 12;
			this.cboAioSetAiRange.Location = new System.Drawing.Point(113, 64);
			this.cboAioSetAiRange.Name = "cboAioSetAiRange";
			this.cboAioSetAiRange.Size = new System.Drawing.Size(144, 20);
			this.cboAioSetAiRange.TabIndex = 25;
			this.cboAioSetAiRange.SelectionChangeCommitted += new System.EventHandler(this.cboAioSetAiRange_SelectionChangeCommitted);
			// 
			// btnAioExit
			// 
			this.btnAioExit.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.btnAioExit.Location = new System.Drawing.Point(18, 168);
			this.btnAioExit.Name = "btnAioExit";
			this.btnAioExit.Size = new System.Drawing.Size(80, 24);
			this.btnAioExit.TabIndex = 22;
			this.btnAioExit.Text = "I";
			this.btnAioExit.Click += new System.EventHandler(this.btnAioExit_Click);
			// 
			// btnAioSingleAi
			// 
			this.btnAioSingleAi.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.btnAioSingleAi.Location = new System.Drawing.Point(18, 136);
			this.btnAioSingleAi.Name = "btnAioSingleAi";
			this.btnAioSingleAi.Size = new System.Drawing.Size(80, 24);
			this.btnAioSingleAi.TabIndex = 21;
			this.btnAioSingleAi.Text = "ȈՓ";
			this.btnAioSingleAi.Click += new System.EventHandler(this.btnAioSingleAi_Click);
			// 
			// btnStop
			// 
			this.btnStop.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.btnStop.Location = new System.Drawing.Point(18, 104);
			this.btnStop.Name = "btnStop";
			this.btnStop.Size = new System.Drawing.Size(80, 24);
			this.btnStop.TabIndex = 28;
			this.btnStop.Text = "~";
			this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
			// 
			// btnStart
			// 
			this.btnStart.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.btnStart.Location = new System.Drawing.Point(18, 72);
			this.btnStart.Name = "btnStart";
			this.btnStart.Size = new System.Drawing.Size(80, 24);
			this.btnStart.TabIndex = 27;
			this.btnStart.Text = "N";
			this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
			// 
			// Label2
			// 
			this.Label2.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.Label2.Location = new System.Drawing.Point(109, 46);
			this.Label2.Name = "Label2";
			this.Label2.Size = new System.Drawing.Size(69, 15);
			this.Label2.TabIndex = 24;
			this.Label2.Text = "W";
			// 
			// btnAioInit
			// 
			this.btnAioInit.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.btnAioInit.Location = new System.Drawing.Point(17, 40);
			this.btnAioInit.Name = "btnAioInit";
			this.btnAioInit.Size = new System.Drawing.Size(81, 24);
			this.btnAioInit.TabIndex = 19;
			this.btnAioInit.Text = "";
			this.btnAioInit.Click += new System.EventHandler(this.btnAioInit_Click);
			// 
			// cboDeviceName
			// 
			this.cboDeviceName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboDeviceName.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.cboDeviceName.ItemHeight = 12;
			this.cboDeviceName.Location = new System.Drawing.Point(110, 13);
			this.cboDeviceName.Name = "cboDeviceName";
			this.cboDeviceName.Size = new System.Drawing.Size(180, 20);
			this.cboDeviceName.TabIndex = 26;
			// 
			// Label1
			// 
			this.Label1.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.Label1.Location = new System.Drawing.Point(14, 16);
			this.Label1.Name = "Label1";
			this.Label1.Size = new System.Drawing.Size(69, 15);
			this.Label1.TabIndex = 20;
			this.Label1.Text = "foCXF";
			// 
			// lblStart
			// 
			this.lblStart.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblStart.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.lblStart.Location = new System.Drawing.Point(113, 117);
			this.lblStart.Name = "lblStart";
			this.lblStart.Size = new System.Drawing.Size(39, 17);
			this.lblStart.TabIndex = 31;
			// 
			// SingleAi
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(304, 253);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.lblErrorString,
																		  this.lblVolt,
																		  this.txtAiChannel,
																		  this.lblCData,
																		  this.Label5,
																		  this.lblErr,
																		  this.Label4,
																		  this.Label3,
																		  this.cboAioSetAiRange,
																		  this.btnAioExit,
																		  this.btnAioSingleAi,
																		  this.btnStop,
																		  this.btnStart,
																		  this.Label2,
																		  this.btnAioInit,
																		  this.cboDeviceName,
																		  this.Label1,
																		  this.lblStart});
			this.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Name = "SingleAi";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "w`l1AiO";
			this.Load += new System.EventHandler(this.SingleAi_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// AvP[ṼC Gg |CgłB
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new SingleAi());
		}

		//---------------------------------------------------------
		//	_CAO
		//---------------------------------------------------------
		private void SingleAi_Load(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// foCX̌o
			//-----------------------------------
			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 = AIO.Ccapaio.AioQueryDeviceName(Index, ref GroupID, ref UnitID, ref DeviceID, DeviceName, Device);
				if (Ret != AIO.Ccapaio.AIO_ERR_SUCCESS)
				{
					break;
				}
				cboDeviceName.Items.Add(DeviceName);
			}
			cboDeviceName.SelectedIndex = 0;
			//-----------------------------------
			// AiO̓WpR{{bNX̍쐬
			//-----------------------------------
			cboAioSetAiRange.Items.Add("-10 - +10V");
			cboAioSetAiRange.Items.Add("-5 - +5V");
			cboAioSetAiRange.Items.Add("0 - +10V");
			cboAioSetAiRange.Items.Add("0 - +5V");
			cboAioSetAiRange.Items.Add("0 - +20mA");
	
			txtAiChannel.Text = "0";
		}

		//--------------------------------------------------------------------------------------------------
		//	foCX̏
		//--------------------------------------------------------------------------------------------------
		private void btnAioInit_Click(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// 
			//-----------------------------------
			string	DeviceName = new string(' ', 100);
			DeviceName = cboDeviceName.Text;
			Ret = AIO.Ccapaio.AioInit(DeviceName, ref Id);
			//-----------------------------------
			// G[
			//-----------------------------------
			AIO.Ccapaio.AioGetErrorString(Ret, ErrorString);
			TextString = "AioInit : " + ErrorString;
			lblErrorString.Text = TextString;
			if (Ret != AIO.Ccapaio.AIO_ERR_SUCCESS)
			{
				return;
			}
			//-----------------------------------
			// W̎擾ƕ\
			//-----------------------------------
			short	AiRange;
			short	Index;
			AiRange = 0;
			Index = 0;
			Ret = AIO.Ccapaio.AioGetAiRange(Id, 0, ref AiRange);
			if (Ret == AIO.Ccapaio.AIO_ERR_SUCCESS)
			{
				switch (AiRange)
				{
					case AIO.Ccapaio.PM10:	Index = 0;	break;
					case AIO.Ccapaio.PM5:	Index = 1;	break;
					case AIO.Ccapaio.P10:	Index = 2;	break;
					case AIO.Ccapaio.P5:	Index = 3;	break;
					case AIO.Ccapaio.P20MA:	Index = 4;	break;
				}
				cboAioSetAiRange.SelectedIndex = Index;
				//-----------------------------------
				// \̐؂ւ
				//-----------------------------------
				if (AiRange == AIO.Ccapaio.P20MA)
				{
					lblCData.Text = "d";
				}
				else
				{
					lblCData.Text = "d";
				}
			}
			ShowRemoteStatus();
		}

		//--------------------------------------------------------------------------------------------------
		//	N{^
		//--------------------------------------------------------------------------------------------------
		private void btnStart_Click(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// N
			//-----------------------------------
			Ret = AIO.Ccapaio.AioStartIO(Id, 0x01);
			//-----------------------------------
			// G[
			//-----------------------------------
			AIO.Ccapaio.AioGetErrorString(Ret, ErrorString);
			TextString = "AioStartIO : " +  ErrorString;
			lblErrorString.Text = TextString;
			if (Ret != 0)
			{
				return;
			}
			ShowRemoteStatus();
		}

		//--------------------------------------------------------------------------------------------------
		//	~{^
		//--------------------------------------------------------------------------------------------------
		private void btnStop_Click(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// ~
			//-----------------------------------
			Ret = AIO.Ccapaio.AioStartIO(Id, 0x00);
			//-----------------------------------
			// G[
			//-----------------------------------
			AIO.Ccapaio.AioGetErrorString(Ret, ErrorString);
			TextString = "AioStartIO : " +  ErrorString;
			lblErrorString.Text = TextString;
			if (Ret != 0)
			{
				return;
			}
			ShowRemoteStatus();
		}

		//--------------------------------------------------------------------------------------------------
		//	w`l1AiO
		//--------------------------------------------------------------------------------------------------
		private void btnAioSingleAi_Click(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// AiO
			//-----------------------------------
			float	AiData;
			short	AiChannel;
			string	szText;// = new string(' ', 100);
			AiData = 0;
			szText = txtAiChannel.Text;
			try 
			{
				AiChannel = Convert.ToInt16(szText);
			}
			catch (System.FormatException)
			{
				AiChannel = 0;
			}
			Ret = AIO.Ccapaio.AioSingleAiEx(Id, AiChannel, ref AiData);
			//-----------------------------------
			// G[
			//-----------------------------------
			AIO.Ccapaio.AioGetErrorString(Ret, ErrorString);
			TextString = "AioSingleAiEx : " +  ErrorString;
			lblErrorString.Text = TextString;
			if (Ret != 0)
			{
				return;
			}
			//-----------------------------------
			// \
			//-----------------------------------
			TextString = string.Format("{0:f6}V", AiData);
			lblVolt.Text = TextString;
		}

		//--------------------------------------------------------------------------------------------------
		//	foCX̏I
		//--------------------------------------------------------------------------------------------------
		private void btnAioExit_Click(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// foCX̏I
			//-----------------------------------
			Ret = AIO.Ccapaio.AioExit(Id);
			AIO.Ccapaio.AioGetErrorString(Ret, ErrorString);
			TextString = "AioExit : " + ErrorString;
			lblErrorString.Text = TextString;
		}

		private void SingleAi_Closed(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// foCX̏I
			//-----------------------------------
			Ret = AIO.Ccapaio.AioExit(Id);
		}

		//--------------------------------------------------------------------------------------------------
		//	AiO̓W̐ݒ
		//--------------------------------------------------------------------------------------------------
		private void cboAioSetAiRange_SelectionChangeCommitted(object sender, System.EventArgs e)
		{
			short	AiRange;
			short	Index;
			AiRange = 0;
			//-----------------------------------
			// ʓe̎擾
			//-----------------------------------
			Index = (short)cboAioSetAiRange.SelectedIndex;
			switch (Index)
			{
				case 0:	AiRange = AIO.Ccapaio.PM10;		break;
				case 1:	AiRange = AIO.Ccapaio.PM5;		break;
				case 2:	AiRange = AIO.Ccapaio.P10;		break;
				case 3:	AiRange = AIO.Ccapaio.P5;		break;
				case 4:	AiRange = AIO.Ccapaio.P20MA;	break;
			}
			//-----------------------------------
			// ̓W̐ݒ
			//-----------------------------------
			Ret = AIO.Ccapaio.AioSetAiRangeAll(Id, AiRange);
			//-----------------------------------
			// G[
			//-----------------------------------
			AIO.Ccapaio.AioGetErrorString(Ret, ErrorString);
			TextString = "AioSetAiRangeAll : " + ErrorString;
			lblErrorString.Text = TextString;
			if (Ret != 0)
			{
				return;
			}
			//-----------------------------------
			// \̐؂ւ
			//-----------------------------------
			if (AiRange == AIO.Ccapaio.P20MA)
			{
				lblCData.Text = "d";
			}
			else
			{
				lblCData.Text = "d";
			}
		}

		//--------------------------------------------------------------------------------------------------
		//	[gXe[^X̕\
		//--------------------------------------------------------------------------------------------------
		private void ShowRemoteStatus()
		{
			byte	Start, Err;
			string	szText = new string(' ', 100);
			Start = 0;
			Err = 0;
			//-----------------------------------
			// [gXe[^X
			//-----------------------------------
			Ret = AIO.Ccapaio.AioGetRemoteStatus(Id, ref Start, ref Err);
			//-----------------------------------
			// G[
			//-----------------------------------
			if (Ret != AIO.Ccapaio.AIO_ERR_SUCCESS)
			{
				AIO.Ccapaio.AioGetErrorString(Ret, ErrorString);
				TextString = "AioGetRemoteStatus : " + ErrorString;
				lblErrorString.Text = TextString;
				return;
			}
			//-----------------------------------
			// [gXe[^X\
			//-----------------------------------
			szText = string.Format("{0:X2}", Start);
			lblStart.Text = szText;
			szText = string.Format("{0:X2}", Err);
			lblErr.Text = szText;
		}
	}
}