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

namespace DIO_Lateched
{
	/// <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 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.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button DI_State;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Button Close_Com;
		private System.Windows.Forms.RadioButton radioOn;
		private System.Windows.Forms.RadioButton radioOff;
		private System.Windows.Forms.TextBox textDITotalCh;
		private System.Windows.Forms.TextBox textDOTotalCh;
		private System.Windows.Forms.TextBox textDIState;
		private System.Windows.Forms.Button Clear_Latched;
		/// <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.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.textDITotalCh = new System.Windows.Forms.TextBox();
			this.textDOTotalCh = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.radioOn = new System.Windows.Forms.RadioButton();
			this.radioOff = new System.Windows.Forms.RadioButton();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.textDIState = new System.Windows.Forms.TextBox();
			this.DI_State = new System.Windows.Forms.Button();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.Close_Com = new System.Windows.Forms.Button();
			this.Clear_Latched = new System.Windows.Forms.Button();
			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 = 49;
			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 = 48;
			this.label11.Text = "Address";
			// 
			// textTimeout
			// 
			this.textTimeout.Location = new System.Drawing.Point(72, 192);
			this.textTimeout.Name = "textTimeout";
			this.textTimeout.Size = new System.Drawing.Size(72, 22);
			this.textTimeout.TabIndex = 45;
			this.textTimeout.Text = "100";
			// 
			// textChecksum
			// 
			this.textChecksum.Location = new System.Drawing.Point(72, 160);
			this.textChecksum.Name = "textChecksum";
			this.textChecksum.Size = new System.Drawing.Size(72, 22);
			this.textChecksum.TabIndex = 44;
			this.textChecksum.Text = "0";
			// 
			// textSlot
			// 
			this.textSlot.Location = new System.Drawing.Point(72, 128);
			this.textSlot.Name = "textSlot";
			this.textSlot.Size = new System.Drawing.Size(72, 22);
			this.textSlot.TabIndex = 43;
			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 = 42;
			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 = 41;
			this.textComport.Text = "1";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(16, 200);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(48, 16);
			this.label5.TabIndex = 40;
			this.label5.Text = "Timeout";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 168);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(56, 16);
			this.label4.TabIndex = 39;
			this.label4.Text = "CheckSum";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(24, 136);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(24, 16);
			this.label3.TabIndex = 38;
			this.label3.Text = "Slot";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 96);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 16);
			this.label2.TabIndex = 37;
			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 = 36;
			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(144, 224);
			this.groupBox2.TabIndex = 50;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Setting";
			// 
			// Open_Com
			// 
			this.Open_Com.Location = new System.Drawing.Point(24, 240);
			this.Open_Com.Name = "Open_Com";
			this.Open_Com.Size = new System.Drawing.Size(72, 32);
			this.Open_Com.TabIndex = 35;
			this.Open_Com.Text = "Open Com";
			this.Open_Com.Click += new System.EventHandler(this.button2_Click);
			// 
			// textDITotalCh
			// 
			this.textDITotalCh.Location = new System.Drawing.Point(232, 30);
			this.textDITotalCh.Name = "textDITotalCh";
			this.textDITotalCh.Size = new System.Drawing.Size(48, 22);
			this.textDITotalCh.TabIndex = 51;
			this.textDITotalCh.Text = "8";
			// 
			// textDOTotalCh
			// 
			this.textDOTotalCh.Location = new System.Drawing.Point(232, 70);
			this.textDOTotalCh.Name = "textDOTotalCh";
			this.textDOTotalCh.Size = new System.Drawing.Size(48, 22);
			this.textDOTotalCh.TabIndex = 52;
			this.textDOTotalCh.Text = "8";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(176, 30);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(48, 24);
			this.label6.TabIndex = 53;
			this.label6.Text = "DI Total Channel";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(176, 70);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(56, 24);
			this.label7.TabIndex = 54;
			this.label7.Text = "DO Total Channel";
			// 
			// radioOn
			// 
			this.radioOn.Checked = true;
			this.radioOn.Location = new System.Drawing.Point(186, 116);
			this.radioOn.Name = "radioOn";
			this.radioOn.Size = new System.Drawing.Size(40, 24);
			this.radioOn.TabIndex = 55;
			this.radioOn.TabStop = true;
			this.radioOn.Text = "On";
			// 
			// radioOff
			// 
			this.radioOff.Location = new System.Drawing.Point(233, 120);
			this.radioOff.Name = "radioOff";
			this.radioOff.Size = new System.Drawing.Size(40, 16);
			this.radioOff.TabIndex = 56;
			this.radioOff.Text = "Off";
			// 
			// groupBox1
			// 
			this.groupBox1.Location = new System.Drawing.Point(176, 102);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(104, 40);
			this.groupBox1.TabIndex = 57;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "State";
			// 
			// textDIState
			// 
			this.textDIState.Location = new System.Drawing.Point(176, 158);
			this.textDIState.Name = "textDIState";
			this.textDIState.Size = new System.Drawing.Size(56, 22);
			this.textDIState.TabIndex = 58;
			this.textDIState.Text = "";
			// 
			// DI_State
			// 
			this.DI_State.Location = new System.Drawing.Point(240, 158);
			this.DI_State.Name = "DI_State";
			this.DI_State.Size = new System.Drawing.Size(56, 58);
			this.DI_State.TabIndex = 59;
			this.DI_State.Text = "DIO State";
			this.DI_State.Click += new System.EventHandler(this.DI_State_Click);
			// 
			// groupBox3
			// 
			this.groupBox3.Location = new System.Drawing.Point(160, 8);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(144, 224);
			this.groupBox3.TabIndex = 62;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "DIO Lateched";
			// 
			// Close_Com
			// 
			this.Close_Com.Location = new System.Drawing.Point(120, 240);
			this.Close_Com.Name = "Close_Com";
			this.Close_Com.Size = new System.Drawing.Size(72, 32);
			this.Close_Com.TabIndex = 63;
			this.Close_Com.Text = "Close Com";
			this.Close_Com.Click += new System.EventHandler(this.Close_Com_Click);
			// 
			// Clear_Latched
			// 
			this.Clear_Latched.Location = new System.Drawing.Point(216, 240);
			this.Clear_Latched.Name = "Clear_Latched";
			this.Clear_Latched.Size = new System.Drawing.Size(80, 32);
			this.Clear_Latched.TabIndex = 64;
			this.Clear_Latched.Text = "Clear Latched";
			this.Clear_Latched.Click += new System.EventHandler(this.Clear_Latched_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
			this.ClientSize = new System.Drawing.Size(320, 277);
			this.Controls.Add(this.Clear_Latched);
			this.Controls.Add(this.Close_Com);
			this.Controls.Add(this.DI_State);
			this.Controls.Add(this.textDIState);
			this.Controls.Add(this.radioOff);
			this.Controls.Add(this.radioOn);
			this.Controls.Add(this.label7);
			this.Controls.Add(this.label6);
			this.Controls.Add(this.textDOTotalCh);
			this.Controls.Add(this.textDITotalCh);
			this.Controls.Add(this.textAddress);
			this.Controls.Add(this.label11);
			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.Open_Com);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.groupBox3);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 應用程式的主進入點。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
			
		short iSlot,iTimeout,iAddress,bChecksum;
		byte iComport;
		uint iBaudrate;
		bool bState;

		private void button2_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);
			
			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 DI_State_Click(object sender, System.EventArgs e)
		{
			uint DILatchValue;
			uint DOLatchValue;
			short iRet;
			
			if (radioOn.Checked)
				bState=true;
			else
				bState=false;

			//DILatchValue=DCON.DigitalInLatched(iComport,iAddress,iSlot,Convert.ToInt16(textDITotalCh.Text),bState,bChecksum,iTimeout);
			if(Convert.ToInt16(textDOTotalCh.Text)==0 && Convert.ToInt16(textDITotalCh.Text)!=0)
			{
				iRet=DCON.IO_Function.Read_DI_Latch(iComport,iAddress,iSlot,Convert.ToInt16(textDITotalCh.Text),bState,bChecksum,iTimeout,out DILatchValue);
				
				if(Convert.ToBoolean( iRet))
					MessageBox.Show("ErrorCode " + Convert.ToString(iRet));
				else
					textDIState.Text=Convert.ToString(DILatchValue,16);
			}
			else 
			{
				
				iRet=DCON.IO_Function.Read_DIO_Latch(iComport,iAddress,iSlot,Convert.ToInt16(textDITotalCh.Text),
					Convert.ToInt16(textDOTotalCh.Text),bState,bChecksum,iTimeout,
					out DILatchValue,out DOLatchValue);
			
				if(Convert.ToBoolean( iRet))
					MessageBox.Show("ErrorCode " + Convert.ToString(iRet));
				else
					textDIState.Text=Convert.ToString(DILatchValue,16);
				
			}	
			
		}

		
		private void Clear_Latched_Click(object sender, System.EventArgs e)
		{
			short iRet;
			
			iRet=DCON.IO_Function.Clear_DIO_Latch(iComport,iAddress,iSlot,bChecksum,iTimeout);
			
			if(Convert.ToBoolean( iRet))
				MessageBox.Show("ErrorCode " + Convert.ToString(iRet));
		}

		
	}
}
