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

namespace AI
{
	/// <summary>
	/// Form1 的摘要描述。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textAddress;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.TextBox textTotalChannel;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox textTimeout;
		private System.Windows.Forms.TextBox textChecksum;
		private System.Windows.Forms.TextBox textSlot;
		private System.Windows.Forms.TextBox textBaudrate;
		private System.Windows.Forms.TextBox textComport;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Button Open_Com;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.TextBox textBox5;
		private System.Windows.Forms.TextBox textBox6;
		private System.Windows.Forms.TextBox textBox7;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.Label label12;
		private System.Windows.Forms.Label label13;
		private System.Windows.Forms.Label label14;
		private System.Windows.Forms.TextBox textBox8;
		private System.Windows.Forms.Label label15;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button Close_Com;
		private System.Windows.Forms.Label label16;
		private System.Windows.Forms.Label label17;
		private System.Windows.Forms.Button Read_Single_AI;
		private System.Windows.Forms.Button Read_AI_ALL;
		private System.Windows.Forms.TextBox textValue;
		private System.Windows.Forms.TextBox textChannel;
		private System.Windows.Forms.GroupBox groupBox3;
		/// <summary>
		/// 設計工具所需的變數。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows Form 設計工具支援的必要項
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼
			//
		}

		/// <summary>
		/// 清除任何使用中的資源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form 設計工具產生的程式碼
		/// <summary>
		/// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
		/// 這個方法的內容。
		/// </summary>
		private void InitializeComponent()
		{
			this.textAddress = new System.Windows.Forms.TextBox();
			this.label11 = new System.Windows.Forms.Label();
			this.textTotalChannel = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.textTimeout = new System.Windows.Forms.TextBox();
			this.textChecksum = new System.Windows.Forms.TextBox();
			this.textSlot = new System.Windows.Forms.TextBox();
			this.textBaudrate = new System.Windows.Forms.TextBox();
			this.textComport = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.Open_Com = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.textBox5 = new System.Windows.Forms.TextBox();
			this.textBox6 = new System.Windows.Forms.TextBox();
			this.textBox7 = new System.Windows.Forms.TextBox();
			this.label7 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.label9 = new System.Windows.Forms.Label();
			this.label10 = new System.Windows.Forms.Label();
			this.label12 = new System.Windows.Forms.Label();
			this.label13 = new System.Windows.Forms.Label();
			this.label14 = new System.Windows.Forms.Label();
			this.textBox8 = new System.Windows.Forms.TextBox();
			this.label15 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.Read_AI_ALL = new System.Windows.Forms.Button();
			this.Close_Com = new System.Windows.Forms.Button();
			this.label16 = new System.Windows.Forms.Label();
			this.textValue = new System.Windows.Forms.TextBox();
			this.label17 = new System.Windows.Forms.Label();
			this.Read_Single_AI = new System.Windows.Forms.Button();
			this.textChannel = new System.Windows.Forms.TextBox();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// textAddress
			// 
			this.textAddress.Location = new System.Drawing.Point(72, 64);
			this.textAddress.Name = "textAddress";
			this.textAddress.Size = new System.Drawing.Size(72, 22);
			this.textAddress.TabIndex = 48;
			this.textAddress.Text = "1";
			// 
			// label11
			// 
			this.label11.Location = new System.Drawing.Point(24, 64);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(48, 16);
			this.label11.TabIndex = 47;
			this.label11.Text = "Address";
			// 
			// textTotalChannel
			// 
			this.textTotalChannel.Location = new System.Drawing.Point(72, 232);
			this.textTotalChannel.Name = "textTotalChannel";
			this.textTotalChannel.Size = new System.Drawing.Size(72, 22);
			this.textTotalChannel.TabIndex = 46;
			this.textTotalChannel.Text = "8";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(16, 232);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(48, 24);
			this.label6.TabIndex = 45;
			this.label6.Text = "Total Channel";
			// 
			// textTimeout
			// 
			this.textTimeout.Location = new System.Drawing.Point(72, 200);
			this.textTimeout.Name = "textTimeout";
			this.textTimeout.Size = new System.Drawing.Size(72, 22);
			this.textTimeout.TabIndex = 44;
			this.textTimeout.Text = "100";
			// 
			// textChecksum
			// 
			this.textChecksum.Location = new System.Drawing.Point(72, 168);
			this.textChecksum.Name = "textChecksum";
			this.textChecksum.Size = new System.Drawing.Size(72, 22);
			this.textChecksum.TabIndex = 43;
			this.textChecksum.Text = "0";
			// 
			// textSlot
			// 
			this.textSlot.Location = new System.Drawing.Point(72, 136);
			this.textSlot.Name = "textSlot";
			this.textSlot.Size = new System.Drawing.Size(72, 22);
			this.textSlot.TabIndex = 42;
			this.textSlot.Text = "-1";
			// 
			// textBaudrate
			// 
			this.textBaudrate.Location = new System.Drawing.Point(72, 96);
			this.textBaudrate.Name = "textBaudrate";
			this.textBaudrate.Size = new System.Drawing.Size(72, 22);
			this.textBaudrate.TabIndex = 41;
			this.textBaudrate.Text = "9600";
			// 
			// textComport
			// 
			this.textComport.Location = new System.Drawing.Point(72, 32);
			this.textComport.Name = "textComport";
			this.textComport.Size = new System.Drawing.Size(72, 22);
			this.textComport.TabIndex = 40;
			this.textComport.Text = "1";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(16, 208);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(48, 16);
			this.label5.TabIndex = 39;
			this.label5.Text = "Timeout";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 176);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(56, 16);
			this.label4.TabIndex = 38;
			this.label4.Text = "CheckSum";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(24, 144);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(24, 16);
			this.label3.TabIndex = 37;
			this.label3.Text = "Slot";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 104);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 16);
			this.label2.TabIndex = 36;
			this.label2.Text = "Baudrate";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 32);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 16);
			this.label1.TabIndex = 35;
			this.label1.Text = "ComPort";
			// 
			// groupBox2
			// 
			this.groupBox2.Location = new System.Drawing.Point(8, 8);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(160, 272);
			this.groupBox2.TabIndex = 49;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Setting";
			// 
			// Open_Com
			// 
			this.Open_Com.Location = new System.Drawing.Point(80, 392);
			this.Open_Com.Name = "Open_Com";
			this.Open_Com.Size = new System.Drawing.Size(80, 32);
			this.Open_Com.TabIndex = 50;
			this.Open_Com.Text = "Open Com";
			this.Open_Com.Click += new System.EventHandler(this.Open_Com_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(213, 24);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(64, 22);
			this.textBox1.TabIndex = 51;
			this.textBox1.Text = "";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(213, 56);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(64, 22);
			this.textBox2.TabIndex = 52;
			this.textBox2.Text = "";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(213, 88);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(64, 22);
			this.textBox3.TabIndex = 53;
			this.textBox3.Text = "";
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(213, 120);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(64, 22);
			this.textBox4.TabIndex = 54;
			this.textBox4.Text = "";
			// 
			// textBox5
			// 
			this.textBox5.Location = new System.Drawing.Point(213, 152);
			this.textBox5.Name = "textBox5";
			this.textBox5.Size = new System.Drawing.Size(64, 22);
			this.textBox5.TabIndex = 55;
			this.textBox5.Text = "";
			// 
			// textBox6
			// 
			this.textBox6.Location = new System.Drawing.Point(213, 184);
			this.textBox6.Name = "textBox6";
			this.textBox6.Size = new System.Drawing.Size(64, 22);
			this.textBox6.TabIndex = 56;
			this.textBox6.Text = "";
			// 
			// textBox7
			// 
			this.textBox7.Location = new System.Drawing.Point(213, 216);
			this.textBox7.Name = "textBox7";
			this.textBox7.Size = new System.Drawing.Size(64, 22);
			this.textBox7.TabIndex = 57;
			this.textBox7.Text = "";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(184, 32);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(24, 16);
			this.label7.TabIndex = 58;
			this.label7.Text = "ch0";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(184, 64);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(24, 12);
			this.label8.TabIndex = 59;
			this.label8.Text = "ch1";
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(184, 96);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(24, 12);
			this.label9.TabIndex = 60;
			this.label9.Text = "ch2";
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(184, 128);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(24, 12);
			this.label10.TabIndex = 61;
			this.label10.Text = "ch3";
			// 
			// label12
			// 
			this.label12.Location = new System.Drawing.Point(184, 160);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(24, 12);
			this.label12.TabIndex = 62;
			this.label12.Text = "ch4";
			// 
			// label13
			// 
			this.label13.Location = new System.Drawing.Point(184, 192);
			this.label13.Name = "label13";
			this.label13.Size = new System.Drawing.Size(24, 12);
			this.label13.TabIndex = 63;
			this.label13.Text = "ch5";
			// 
			// label14
			// 
			this.label14.Location = new System.Drawing.Point(184, 224);
			this.label14.Name = "label14";
			this.label14.Size = new System.Drawing.Size(24, 12);
			this.label14.TabIndex = 64;
			this.label14.Text = "ch6";
			// 
			// textBox8
			// 
			this.textBox8.Location = new System.Drawing.Point(214, 248);
			this.textBox8.Name = "textBox8";
			this.textBox8.Size = new System.Drawing.Size(64, 22);
			this.textBox8.TabIndex = 65;
			this.textBox8.Text = "";
			// 
			// label15
			// 
			this.label15.Location = new System.Drawing.Point(184, 256);
			this.label15.Name = "label15";
			this.label15.Size = new System.Drawing.Size(24, 12);
			this.label15.TabIndex = 66;
			this.label15.Text = "ch7";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.Read_AI_ALL);
			this.groupBox1.Location = new System.Drawing.Point(168, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(168, 272);
			this.groupBox1.TabIndex = 67;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Read AI ALL";
			// 
			// Read_AI_ALL
			// 
			this.Read_AI_ALL.Location = new System.Drawing.Point(120, 48);
			this.Read_AI_ALL.Name = "Read_AI_ALL";
			this.Read_AI_ALL.Size = new System.Drawing.Size(40, 136);
			this.Read_AI_ALL.TabIndex = 0;
			this.Read_AI_ALL.Text = "Read AI ALL";
			this.Read_AI_ALL.Click += new System.EventHandler(this.Read_AI_ALL_Click);
			// 
			// Close_Com
			// 
			this.Close_Com.Location = new System.Drawing.Point(208, 392);
			this.Close_Com.Name = "Close_Com";
			this.Close_Com.Size = new System.Drawing.Size(80, 32);
			this.Close_Com.TabIndex = 68;
			this.Close_Com.Text = "Close Com";
			this.Close_Com.Click += new System.EventHandler(this.Close_Com_Click);
			// 
			// label16
			// 
			this.label16.Location = new System.Drawing.Point(80, 312);
			this.label16.Name = "label16";
			this.label16.Size = new System.Drawing.Size(48, 24);
			this.label16.TabIndex = 70;
			this.label16.Text = "Channel Number";
			// 
			// textValue
			// 
			this.textValue.Location = new System.Drawing.Point(128, 344);
			this.textValue.Name = "textValue";
			this.textValue.Size = new System.Drawing.Size(48, 22);
			this.textValue.TabIndex = 71;
			this.textValue.Text = "";
			// 
			// label17
			// 
			this.label17.Location = new System.Drawing.Point(88, 352);
			this.label17.Name = "label17";
			this.label17.Size = new System.Drawing.Size(32, 16);
			this.label17.TabIndex = 72;
			this.label17.Text = "Value";
			// 
			// Read_Single_AI
			// 
			this.Read_Single_AI.Location = new System.Drawing.Point(192, 320);
			this.Read_Single_AI.Name = "Read_Single_AI";
			this.Read_Single_AI.Size = new System.Drawing.Size(88, 32);
			this.Read_Single_AI.TabIndex = 73;
			this.Read_Single_AI.Text = "Read Single AI";
			this.Read_Single_AI.Click += new System.EventHandler(this.Read_Single_AI_Click);
			// 
			// textChannel
			// 
			this.textChannel.Location = new System.Drawing.Point(128, 312);
			this.textChannel.Name = "textChannel";
			this.textChannel.Size = new System.Drawing.Size(48, 22);
			this.textChannel.TabIndex = 74;
			this.textChannel.Text = "3";
			// 
			// groupBox3
			// 
			this.groupBox3.Location = new System.Drawing.Point(72, 280);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(216, 104);
			this.groupBox3.TabIndex = 75;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Read Single AI";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
			this.ClientSize = new System.Drawing.Size(344, 453);
			this.Controls.Add(this.textChannel);
			this.Controls.Add(this.Read_Single_AI);
			this.Controls.Add(this.label17);
			this.Controls.Add(this.textValue);
			this.Controls.Add(this.label16);
			this.Controls.Add(this.Close_Com);
			this.Controls.Add(this.label15);
			this.Controls.Add(this.textBox8);
			this.Controls.Add(this.label14);
			this.Controls.Add(this.label13);
			this.Controls.Add(this.label12);
			this.Controls.Add(this.label10);
			this.Controls.Add(this.label9);
			this.Controls.Add(this.label8);
			this.Controls.Add(this.label7);
			this.Controls.Add(this.textBox7);
			this.Controls.Add(this.textBox6);
			this.Controls.Add(this.textBox5);
			this.Controls.Add(this.textBox4);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.Open_Com);
			this.Controls.Add(this.textAddress);
			this.Controls.Add(this.label11);
			this.Controls.Add(this.textTotalChannel);
			this.Controls.Add(this.label6);
			this.Controls.Add(this.textTimeout);
			this.Controls.Add(this.textChecksum);
			this.Controls.Add(this.textSlot);
			this.Controls.Add(this.textBaudrate);
			this.Controls.Add(this.textComport);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.groupBox3);
			this.Name = "Form1";
			this.Text = "j";
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 應用程式的主進入點。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		short iSlot,iTimeout,iTotalChannel,iAddress,bChecksum;
		byte iComport;
		uint iBaudrate;

		private void Open_Com_Click(object sender, System.EventArgs e)
		{
			iComport=Convert.ToByte(textComport.Text);
			iAddress = Convert.ToInt16(textAddress.Text);
			iBaudrate=Convert.ToUInt32(textBaudrate.Text);
			iSlot=Convert.ToInt16(textSlot.Text);
			bChecksum=Convert.ToInt16(textChecksum.Text);
			iTimeout=Convert.ToInt16(textTimeout.Text);
			iTotalChannel=Convert.ToInt16(textTotalChannel.Text);

			DCON.UART.Open_Com(iComport,iBaudrate,8,0,1);
		}

		private void Close_Com_Click(object sender, System.EventArgs e)
		{
			DCON.UART.Close_Com(iComport);
		}

		private void Read_AI_ALL_Click(object sender, System.EventArgs e)
		{
			float[] AIValue=new float[16];
			short iRet;
			
			iRet=DCON.IO_Function.Read_AI_All(iComport,iAddress,iSlot,bChecksum,iTimeout,AIValue);
			
			if(Convert.ToBoolean( iRet))
				MessageBox.Show("ErrorCode " + Convert.ToString(iRet));
			else
			{
				textBox1.Text=Convert.ToString(AIValue[0]);
				textBox2.Text=Convert.ToString(AIValue[1]);
				textBox3.Text=Convert.ToString(AIValue[2]);
				textBox4.Text=Convert.ToString(AIValue[3]);
				textBox5.Text=Convert.ToString(AIValue[4]);
				textBox6.Text=Convert.ToString(AIValue[5]);
				textBox7.Text=Convert.ToString(AIValue[6]);
				textBox8.Text=Convert.ToString(AIValue[7]);
			}
		}

		private void Read_Single_AI_Click(object sender, System.EventArgs e)
		{
			float AIvalue;
			short iRet;
			iRet=DCON.IO_Function.Read_AI(iComport,iAddress,iSlot, Convert.ToInt16(textChannel.Text),iTotalChannel,bChecksum,iTimeout,out AIvalue);
			if(Convert.ToBoolean( iRet))
				MessageBox.Show("ErrorCode " + Convert.ToString(iRet));
			else
				textValue.Text=Convert.ToString(AIvalue);
		}

				
	}
}
