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

namespace SingleAo
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class SingleAo : 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.Button btnAioExit;
		internal System.Windows.Forms.Button btnAioSingleAo;
		internal System.Windows.Forms.Button btnStop;
		internal System.Windows.Forms.Button btnStart;
		internal System.Windows.Forms.Button btnAioInit;
		internal System.Windows.Forms.TextBox txtVolt;
		internal System.Windows.Forms.TextBox txtAoChannel;
		internal System.Windows.Forms.Label lblCData;
		internal System.Windows.Forms.Label lblErr;
		internal System.Windows.Forms.Label lblStart;
		internal System.Windows.Forms.ComboBox cboAioSetAoRange;
		internal System.Windows.Forms.ComboBox cboDeviceName;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		// Return value of function
		int				Ret;
		// ID
		short			Id;
		// ErrorCode string
		StringBuilder	ErrorString = new StringBuilder(256);		
		// Used to format string
		string			TextString;

		public SingleAo()
		{
			//
			// 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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SingleAo));
			this.lblErrorString = new System.Windows.Forms.Label();
			this.btnAioExit = new System.Windows.Forms.Button();
			this.btnAioSingleAo = new System.Windows.Forms.Button();
			this.btnStop = new System.Windows.Forms.Button();
			this.btnStart = new System.Windows.Forms.Button();
			this.btnAioInit = new System.Windows.Forms.Button();
			this.txtVolt = new System.Windows.Forms.TextBox();
			this.txtAoChannel = 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.lblStart = new System.Windows.Forms.Label();
			this.Label4 = new System.Windows.Forms.Label();
			this.Label3 = new System.Windows.Forms.Label();
			this.cboAioSetAoRange = new System.Windows.Forms.ComboBox();
			this.Label2 = new System.Windows.Forms.Label();
			this.cboDeviceName = new System.Windows.Forms.ComboBox();
			this.Label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// lblErrorString
			// 
			this.lblErrorString.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblErrorString.Location = new System.Drawing.Point(12, 237);
			this.lblErrorString.Name = "lblErrorString";
			this.lblErrorString.Size = new System.Drawing.Size(280, 38);
			this.lblErrorString.TabIndex = 46;
			// 
			// btnAioExit
			// 
			this.btnAioExit.Location = new System.Drawing.Point(12, 194);
			this.btnAioExit.Name = "btnAioExit";
			this.btnAioExit.Size = new System.Drawing.Size(76, 25);
			this.btnAioExit.TabIndex = 32;
			this.btnAioExit.Text = "AioExit";
			this.btnAioExit.Click += new System.EventHandler(this.btnAioExit_Click);
			// 
			// btnAioSingleAo
			// 
			this.btnAioSingleAo.Location = new System.Drawing.Point(12, 157);
			this.btnAioSingleAo.Name = "btnAioSingleAo";
			this.btnAioSingleAo.Size = new System.Drawing.Size(76, 25);
			this.btnAioSingleAo.TabIndex = 31;
			this.btnAioSingleAo.Text = "AioSingleAo";
			this.btnAioSingleAo.Click += new System.EventHandler(this.btnAioSingleAo_Click);
			// 
			// btnStop
			// 
			this.btnStop.Location = new System.Drawing.Point(12, 120);
			this.btnStop.Name = "btnStop";
			this.btnStop.Size = new System.Drawing.Size(76, 25);
			this.btnStop.TabIndex = 41;
			this.btnStop.Text = "Stop";
			this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
			// 
			// btnStart
			// 
			this.btnStart.Location = new System.Drawing.Point(12, 83);
			this.btnStart.Name = "btnStart";
			this.btnStart.Size = new System.Drawing.Size(76, 25);
			this.btnStart.TabIndex = 40;
			this.btnStart.Text = "Start";
			this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
			// 
			// btnAioInit
			// 
			this.btnAioInit.Location = new System.Drawing.Point(12, 46);
			this.btnAioInit.Name = "btnAioInit";
			this.btnAioInit.Size = new System.Drawing.Size(76, 25);
			this.btnAioInit.TabIndex = 29;
			this.btnAioInit.Text = "AioInit";
			this.btnAioInit.Click += new System.EventHandler(this.btnAioInit_Click);
			// 
			// txtVolt
			// 
			this.txtVolt.Location = new System.Drawing.Point(157, 198);
			this.txtVolt.Name = "txtVolt";
			this.txtVolt.Size = new System.Drawing.Size(72, 20);
			this.txtVolt.TabIndex = 37;
			this.txtVolt.Text = "";
			// 
			// txtAoChannel
			// 
			this.txtAoChannel.Location = new System.Drawing.Point(101, 198);
			this.txtAoChannel.Name = "txtAoChannel";
			this.txtAoChannel.Size = new System.Drawing.Size(51, 20);
			this.txtAoChannel.TabIndex = 34;
			this.txtAoChannel.Text = "";
			// 
			// lblCData
			// 
			this.lblCData.Location = new System.Drawing.Point(157, 172);
			this.lblCData.Name = "lblCData";
			this.lblCData.Size = new System.Drawing.Size(64, 19);
			this.lblCData.TabIndex = 45;
			this.lblCData.Text = "Voltage";
			// 
			// Label5
			// 
			this.Label5.Location = new System.Drawing.Point(101, 172);
			this.Label5.Name = "Label5";
			this.Label5.Size = new System.Drawing.Size(64, 18);
			this.Label5.TabIndex = 44;
			this.Label5.Text = "Channel";
			// 
			// lblErr
			// 
			this.lblErr.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblErr.Location = new System.Drawing.Point(184, 134);
			this.lblErr.Name = "lblErr";
			this.lblErr.Size = new System.Drawing.Size(39, 17);
			this.lblErr.TabIndex = 42;
			// 
			// lblStart
			// 
			this.lblStart.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblStart.Location = new System.Drawing.Point(101, 134);
			this.lblStart.Name = "lblStart";
			this.lblStart.Size = new System.Drawing.Size(39, 17);
			this.lblStart.TabIndex = 39;
			// 
			// Label4
			// 
			this.Label4.Location = new System.Drawing.Point(181, 115);
			this.Label4.Name = "Label4";
			this.Label4.Size = new System.Drawing.Size(82, 19);
			this.Label4.TabIndex = 38;
			this.Label4.Text = "Error Register";
			// 
			// Label3
			// 
			this.Label3.Location = new System.Drawing.Point(101, 115);
			this.Label3.Name = "Label3";
			this.Label3.Size = new System.Drawing.Size(74, 19);
			this.Label3.TabIndex = 36;
			this.Label3.Text = "Start Register";
			// 
			// cboAioSetAoRange
			// 
			this.cboAioSetAoRange.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboAioSetAoRange.Location = new System.Drawing.Point(101, 66);
			this.cboAioSetAoRange.Name = "cboAioSetAoRange";
			this.cboAioSetAoRange.Size = new System.Drawing.Size(136, 21);
			this.cboAioSetAoRange.TabIndex = 35;
			this.cboAioSetAoRange.SelectionChangeCommitted += new System.EventHandler(this.cboAioSetAoRange_SelectionChangeCommitted);
			// 
			// Label2
			// 
			this.Label2.Location = new System.Drawing.Point(98, 46);
			this.Label2.Name = "Label2";
			this.Label2.Size = new System.Drawing.Size(64, 18);
			this.Label2.TabIndex = 33;
			this.Label2.Text = "Range";
			// 
			// cboDeviceName
			// 
			this.cboDeviceName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboDeviceName.Location = new System.Drawing.Point(101, 8);
			this.cboDeviceName.Name = "cboDeviceName";
			this.cboDeviceName.Size = new System.Drawing.Size(194, 21);
			this.cboDeviceName.TabIndex = 43;
			// 
			// Label1
			// 
			this.Label1.Location = new System.Drawing.Point(12, 13);
			this.Label1.Name = "Label1";
			this.Label1.Size = new System.Drawing.Size(78, 19);
			this.Label1.TabIndex = 30;
			this.Label1.Text = "Device Name :";
			// 
			// SingleAo
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(305, 283);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.lblErrorString,
																		  this.btnAioExit,
																		  this.btnAioSingleAo,
																		  this.btnStop,
																		  this.btnStart,
																		  this.btnAioInit,
																		  this.txtVolt,
																		  this.txtAoChannel,
																		  this.lblCData,
																		  this.Label5,
																		  this.lblErr,
																		  this.lblStart,
																		  this.Label4,
																		  this.Label3,
																		  this.cboAioSetAoRange,
																		  this.Label2,
																		  this.cboDeviceName,
																		  this.Label1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Name = "SingleAo";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Analog output of specified channel once";
			this.Load += new System.EventHandler(this.SingleAo_Load);
			this.ResumeLayout(false);

		}
		#endregion

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

		//---------------------------------------------------------
		//	Initialize dialog
		//---------------------------------------------------------
		private void SingleAo_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 = 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;

			//-----------------------------------
			// Create combobox for analog output range
			//-----------------------------------
			cboAioSetAoRange.Items.Add("-10 - +10V");
			cboAioSetAoRange.Items.Add("-5 - +5V");
			cboAioSetAoRange.Items.Add("0 - +10V");
			cboAioSetAoRange.Items.Add("0 - +5V");
			cboAioSetAoRange.Items.Add("0 - +20mA");

			txtAoChannel.Text = "0";
		}

		//--------------------------------------------------------------------------------------------------
		//	Initialize device
		//--------------------------------------------------------------------------------------------------
		private void btnAioInit_Click(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Initialization
			//-----------------------------------
			string	DeviceName = new string(' ', 100);
			DeviceName = cboDeviceName.Text;
			Ret = AIO.Ccapaio.AioInit(DeviceName, ref Id);
			//-----------------------------------
			// Process error
			//-----------------------------------
			AIO.Ccapaio.AioGetErrorString(Ret, ErrorString);
			TextString = "AioInit : " + ErrorString;
			lblErrorString.Text = TextString;
			if(Ret != 0)
			{
				return;
			}
			//-----------------------------------
			// Get and display range
			//-----------------------------------
			short	AoRange;
			short	Index;
			AoRange = 0;
			Index = 0;
			Ret = AIO.Ccapaio.AioGetAoRange(Id, 0, ref AoRange);
			if (Ret == AIO.Ccapaio.AIO_ERR_SUCCESS) 
			{
				switch(AoRange)
				{
					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;
				}
				cboAioSetAoRange.SelectedIndex = Index;
				//-----------------------------------
				// Switch display
				//-----------------------------------
				if(AoRange == AIO.Ccapaio.P20MA)
				{
					lblCData.Text = "Current";
				}
				else
				{
					lblCData.Text = "Voltage";
				}
			}
			ShowRemoteStatus();
		}

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

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

		//--------------------------------------------------------------------------------------------------
		//	Analog output of specified channel once
		//--------------------------------------------------------------------------------------------------
		private void btnAioSingleAo_Click(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Analog output
			//-----------------------------------
			string	szText = new string(' ', 100);
			short	AoChannel;
			float	AoVolt;
			szText = txtAoChannel.Text;
			try
			{
				AoChannel = Convert.ToInt16(szText);
			}
			catch (System.FormatException)
			{
				AoChannel = 0;
			}
			szText = txtVolt.Text;
			try
			{
				AoVolt = Convert.ToSingle(szText);
			}
			catch (System.FormatException)
			{
				AoVolt = 0;
			}

			Ret = AIO.Ccapaio.AioSingleAoEx(Id, AoChannel, AoVolt);
			//-----------------------------------
			// Process error
			//-----------------------------------
			AIO.Ccapaio.AioGetErrorString(Ret, ErrorString);
			TextString = "AioSingleAoEx : " + ErrorString;
			lblErrorString.Text = TextString;
			if(Ret != 0)
			{
				return;
			}
		}

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

		private void SingleAo_Closed(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Terminate device
			//-----------------------------------
			Ret = AIO.Ccapaio.AioExit(Id);
		}

		//--------------------------------------------------------------------------------------------------
		//	Set analog output range
		//--------------------------------------------------------------------------------------------------
		private void cboAioSetAoRange_SelectionChangeCommitted(object sender, System.EventArgs e)
		{
			short	AoRange;
			short	Index;
			AoRange = 0;
			//-----------------------------------
			// Get screen content
			//-----------------------------------
			Index = (short)cboAioSetAoRange.SelectedIndex;
			switch(Index)
			{
				case 0:	AoRange = AIO.Ccapaio.PM10;		break;
				case 1:	AoRange = AIO.Ccapaio.PM5;		break;
				case 2:	AoRange = AIO.Ccapaio.P10;		break;
				case 3:	AoRange = AIO.Ccapaio.P5;		break;
				case 4:	AoRange = AIO.Ccapaio.P20MA;	break;
			}
			//-----------------------------------
			// Set output range
			//-----------------------------------
			Ret = AIO.Ccapaio.AioSetAoRangeAll(Id, AoRange);
			//-----------------------------------
			// Process error
			//-----------------------------------
			AIO.Ccapaio.AioGetErrorString(Ret, ErrorString);
			TextString = "AioSetAoRangeAll : " + ErrorString;
			lblErrorString.Text = TextString;
			if (Ret != 0) 
			{
				return;
			}
			//-----------------------------------
			// Switch display
			//-----------------------------------
			if(AoRange == AIO.Ccapaio.P20MA)
			{
				lblCData.Text = "Current";
			}
			else
			{
				lblCData.Text = "Voltage";
			}
		}

		//--------------------------------------------------------------------------------------------------
		//	Display remote status
		//--------------------------------------------------------------------------------------------------
		private void ShowRemoteStatus()
		{
			byte	Start, Err;
			string	szText = new string(' ', 100);
			Start = 0;
			Err = 0;
			//-----------------------------------
			// Input remote status
			//-----------------------------------
			Ret = AIO.Ccapaio.AioGetRemoteStatus(Id, ref Start, ref Err);
			//-----------------------------------
			// Process error
			//-----------------------------------
			if (Ret != AIO.Ccapaio.AIO_ERR_SUCCESS) 
			{
				AIO.Ccapaio.AioGetErrorString(Ret, ErrorString);
				TextString = "AioGetRemoteStatus : " + ErrorString;
				lblErrorString.Text = TextString;
				return;
			}
			//-----------------------------------
			// Display remote status
			//-----------------------------------
			szText = string.Format("{0:X2}", Start);
			lblStart.Text = szText;
			szText = string.Format("{0:X2}", Err);
			lblErr.Text = szText;
		}
	}
}
