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 MultiAi
{
	/// <summary>
	/// Summary description for MultiAi.
	/// </summary>
	public class MultiAi : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.Label Label6;
		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 btnAioMultiAi;
		internal System.Windows.Forms.Button btnStop;
		internal System.Windows.Forms.Button btnStart;
		internal System.Windows.Forms.Label lblVolt;
		internal System.Windows.Forms.Label lblAiChannels;
		internal System.Windows.Forms.Label lblCData;
		internal System.Windows.Forms.Label lblErr;
		internal System.Windows.Forms.Label lblStart;
		internal System.Windows.Forms.TextBox txtChannels;
		internal System.Windows.Forms.ComboBox cboAioSetAiRange;
		internal System.Windows.Forms.Button btnAioInit;
		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;
		string			TextTemp;

		public MultiAi()
		{
			//
			// 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(MultiAi));
			this.lblErrorString = new System.Windows.Forms.Label();
			this.btnAioExit = new System.Windows.Forms.Button();
			this.btnAioMultiAi = new System.Windows.Forms.Button();
			this.btnStop = new System.Windows.Forms.Button();
			this.btnStart = new System.Windows.Forms.Button();
			this.lblVolt = new System.Windows.Forms.Label();
			this.lblAiChannels = new System.Windows.Forms.Label();
			this.lblCData = new System.Windows.Forms.Label();
			this.Label6 = new System.Windows.Forms.Label();
			this.lblErr = new System.Windows.Forms.Label();
			this.lblStart = new System.Windows.Forms.Label();
			this.Label5 = new System.Windows.Forms.Label();
			this.Label4 = new System.Windows.Forms.Label();
			this.txtChannels = new System.Windows.Forms.TextBox();
			this.cboAioSetAiRange = new System.Windows.Forms.ComboBox();
			this.Label3 = new System.Windows.Forms.Label();
			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.SuspendLayout();
			// 
			// lblErrorString
			// 
			this.lblErrorString.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblErrorString.Location = new System.Drawing.Point(12, 367);
			this.lblErrorString.Name = "lblErrorString";
			this.lblErrorString.Size = new System.Drawing.Size(286, 37);
			this.lblErrorString.TabIndex = 55;
			// 
			// btnAioExit
			// 
			this.btnAioExit.Location = new System.Drawing.Point(12, 221);
			this.btnAioExit.Name = "btnAioExit";
			this.btnAioExit.Size = new System.Drawing.Size(80, 25);
			this.btnAioExit.TabIndex = 39;
			this.btnAioExit.Text = "AioExit";
			this.btnAioExit.Click += new System.EventHandler(this.btnAioExit_Click);
			// 
			// btnAioMultiAi
			// 
			this.btnAioMultiAi.Location = new System.Drawing.Point(12, 179);
			this.btnAioMultiAi.Name = "btnAioMultiAi";
			this.btnAioMultiAi.Size = new System.Drawing.Size(80, 25);
			this.btnAioMultiAi.TabIndex = 38;
			this.btnAioMultiAi.Text = "AioMultiAi";
			this.btnAioMultiAi.Click += new System.EventHandler(this.btnAioMultiAi_Click);
			// 
			// btnStop
			// 
			this.btnStop.Location = new System.Drawing.Point(12, 139);
			this.btnStop.Name = "btnStop";
			this.btnStop.Size = new System.Drawing.Size(80, 25);
			this.btnStop.TabIndex = 45;
			this.btnStop.Text = "Stop";
			this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
			// 
			// btnStart
			// 
			this.btnStart.Location = new System.Drawing.Point(12, 98);
			this.btnStart.Name = "btnStart";
			this.btnStart.Size = new System.Drawing.Size(80, 25);
			this.btnStart.TabIndex = 44;
			this.btnStart.Text = "Start";
			this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
			// 
			// lblVolt
			// 
			this.lblVolt.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblVolt.Location = new System.Drawing.Point(164, 179);
			this.lblVolt.Name = "lblVolt";
			this.lblVolt.Size = new System.Drawing.Size(78, 173);
			this.lblVolt.TabIndex = 54;
			// 
			// lblAiChannels
			// 
			this.lblAiChannels.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblAiChannels.Location = new System.Drawing.Point(116, 179);
			this.lblAiChannels.Name = "lblAiChannels";
			this.lblAiChannels.Size = new System.Drawing.Size(40, 173);
			this.lblAiChannels.TabIndex = 53;
			// 
			// lblCData
			// 
			this.lblCData.Location = new System.Drawing.Point(180, 156);
			this.lblCData.Name = "lblCData";
			this.lblCData.Size = new System.Drawing.Size(64, 19);
			this.lblCData.TabIndex = 52;
			this.lblCData.Text = "Voltage";
			// 
			// Label6
			// 
			this.Label6.Location = new System.Drawing.Point(108, 156);
			this.Label6.Name = "Label6";
			this.Label6.Size = new System.Drawing.Size(64, 19);
			this.Label6.TabIndex = 51;
			this.Label6.Text = "Channel";
			// 
			// lblErr
			// 
			this.lblErr.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblErr.Location = new System.Drawing.Point(189, 128);
			this.lblErr.Name = "lblErr";
			this.lblErr.Size = new System.Drawing.Size(39, 17);
			this.lblErr.TabIndex = 50;
			// 
			// lblStart
			// 
			this.lblStart.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblStart.Location = new System.Drawing.Point(116, 128);
			this.lblStart.Name = "lblStart";
			this.lblStart.Size = new System.Drawing.Size(39, 17);
			this.lblStart.TabIndex = 49;
			// 
			// Label5
			// 
			this.Label5.Location = new System.Drawing.Point(189, 104);
			this.Label5.Name = "Label5";
			this.Label5.Size = new System.Drawing.Size(80, 18);
			this.Label5.TabIndex = 48;
			this.Label5.Text = "Error Register";
			// 
			// Label4
			// 
			this.Label4.Location = new System.Drawing.Point(108, 104);
			this.Label4.Name = "Label4";
			this.Label4.Size = new System.Drawing.Size(88, 18);
			this.Label4.TabIndex = 46;
			this.Label4.Text = "Start Register";
			// 
			// txtChannels
			// 
			this.txtChannels.Location = new System.Drawing.Point(216, 63);
			this.txtChannels.Name = "txtChannels";
			this.txtChannels.Size = new System.Drawing.Size(66, 20);
			this.txtChannels.TabIndex = 43;
			this.txtChannels.Text = "";
			// 
			// cboAioSetAiRange
			// 
			this.cboAioSetAiRange.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboAioSetAiRange.Location = new System.Drawing.Point(116, 63);
			this.cboAioSetAiRange.Name = "cboAioSetAiRange";
			this.cboAioSetAiRange.Size = new System.Drawing.Size(96, 21);
			this.cboAioSetAiRange.TabIndex = 41;
			this.cboAioSetAiRange.SelectionChangeCommitted += new System.EventHandler(this.cboAioSetAiRange_SelectionChangeCommitted);
			// 
			// Label3
			// 
			this.Label3.Location = new System.Drawing.Point(212, 40);
			this.Label3.Name = "Label3";
			this.Label3.Size = new System.Drawing.Size(112, 19);
			this.Label3.TabIndex = 42;
			this.Label3.Text = "Number of Channels";
			// 
			// Label2
			// 
			this.Label2.Location = new System.Drawing.Point(108, 40);
			this.Label2.Name = "Label2";
			this.Label2.Size = new System.Drawing.Size(64, 19);
			this.Label2.TabIndex = 40;
			this.Label2.Text = "Range";
			// 
			// btnAioInit
			// 
			this.btnAioInit.Location = new System.Drawing.Point(12, 60);
			this.btnAioInit.Name = "btnAioInit";
			this.btnAioInit.Size = new System.Drawing.Size(80, 25);
			this.btnAioInit.TabIndex = 36;
			this.btnAioInit.Text = "AioInit";
			this.btnAioInit.Click += new System.EventHandler(this.btnAioInit_Click);
			// 
			// cboDeviceName
			// 
			this.cboDeviceName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboDeviceName.Location = new System.Drawing.Point(116, 11);
			this.cboDeviceName.Name = "cboDeviceName";
			this.cboDeviceName.Size = new System.Drawing.Size(180, 21);
			this.cboDeviceName.TabIndex = 47;
			// 
			// Label1
			// 
			this.Label1.Location = new System.Drawing.Point(12, 15);
			this.Label1.Name = "Label1";
			this.Label1.Size = new System.Drawing.Size(84, 19);
			this.Label1.TabIndex = 37;
			this.Label1.Text = "Device Name :";
			// 
			// MultiAi
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(312, 415);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.lblErrorString,
																		  this.btnAioExit,
																		  this.btnAioMultiAi,
																		  this.btnStop,
																		  this.btnStart,
																		  this.lblVolt,
																		  this.lblAiChannels,
																		  this.lblCData,
																		  this.Label6,
																		  this.lblErr,
																		  this.lblStart,
																		  this.Label5,
																		  this.Label4,
																		  this.txtChannels,
																		  this.cboAioSetAiRange,
																		  this.Label3,
																		  this.Label2,
																		  this.btnAioInit,
																		  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 = "MultiAi";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Analog input of plural channels once";
			this.Load += new System.EventHandler(this.MultiAi_Load);
			this.ResumeLayout(false);

		}
		#endregion

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

		//---------------------------------------------------------
		//	Initialize dialog
		//---------------------------------------------------------
		private void MultiAi_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 input range
			//-----------------------------------
			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");
		}

		//--------------------------------------------------------------------------------------------------
		//	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 != AIO.Ccapaio.AIO_ERR_SUCCESS) 
			{
				return;
			}
			//-----------------------------------
			// Get and display range
			//-----------------------------------
			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;
				//-----------------------------------
				// Switch display
				//-----------------------------------
				if (AiRange == AIO.Ccapaio.P20MA) 
				{
					lblCData.Text = "Current";
				}
				else
				{
					lblCData.Text = "Voltage";
				}
			}
			//-----------------------------------
			// Get and display the number of channels
			//-----------------------------------
			short	AiChannels;
			AiChannels = 0;
			Ret = AIO.Ccapaio.AioGetAiMaxChannels(Id, ref AiChannels);
			if (Ret == AIO.Ccapaio.AIO_ERR_SUCCESS) 
			{
				string	szText = new string(' ', 100);
				szText = string.Format("{0:d}", AiChannels);
				txtChannels.Text = szText;
			}
			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 input of specified channel once
		//--------------------------------------------------------------------------------------------------
		private void btnAioMultiAi_Click(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Analog input
			//-----------------------------------
			float[]	AiData = new float[256];
			short	AiChannels;
			string	szText = new string(' ', 100);
			szText = txtChannels.Text;
			try
			{
				AiChannels = Convert.ToInt16(szText);
			}
			catch (System.FormatException)
			{
				AiChannels = 0;
			}
			GCHandle Pter = GCHandle.Alloc(AiData, GCHandleType.Pinned);

			Ret = AIO.Ccapaio.AioMultiAiEx(Id, AiChannels, AiData);
			Pter.Free();
			//-----------------------------------
			// Process error
			//-----------------------------------
			AIO.Ccapaio.AioGetErrorString(Ret, ErrorString);
			TextString = "AioMultiAiEx : " + ErrorString;
			lblErrorString.Text = TextString;
			if(Ret != 0)
			{
				return;
			}
			//-----------------------------------
			// Display converted data
			//-----------------------------------
			ushort	i;
			TextString = "";
			for(i=0 ;i<AiChannels; i++)
			{
				TextTemp = string.Format("{0:d}\n", i);
				TextString += TextTemp;
				lblAiChannels.Text = TextString;
			}
			TextString = "";
			for(i = 0 ; i < AiChannels ; i++)
			{
				TextTemp = string.Format("{0:f6}V\n", AiData[i]);
				TextString += TextTemp;
				lblVolt.Text = TextString;
			}
		}

		//--------------------------------------------------------------------------------------------------
		//	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 MultiAi_Closed(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Terminate device
			//-----------------------------------
			Ret = AIO.Ccapaio.AioExit(Id);
		}

		//--------------------------------------------------------------------------------------------------
		//	Set analog input range
		//--------------------------------------------------------------------------------------------------
		private void cboAioSetAiRange_SelectionChangeCommitted(object sender, System.EventArgs e)
		{
			short	AiRange;
			short	Index;
			AiRange = 0;
			//-----------------------------------
			// Get screen content
			//-----------------------------------
			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;
			}
			//-----------------------------------
			// Set input range
			//-----------------------------------
			Ret = AIO.Ccapaio.AioSetAiRangeAll(Id, AiRange);
			//-----------------------------------
			// Process error
			//-----------------------------------
			AIO.Ccapaio.AioGetErrorString(Ret, ErrorString);
			TextString = "AioSetAiRangeAll : " + ErrorString;
			lblErrorString.Text = TextString;
			if (Ret != 0) 
			{
				return;
			}
			//-----------------------------------
			// Switch display
			//-----------------------------------
			if (AiRange == 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;
		}
	}
}