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

namespace DI
{
	/// <summary>
	/// Form1 的摘要描述。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button Close_Com;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.Button button5;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBitNum;
		private System.Windows.Forms.TextBox textCounterNum;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox textBaudrate;
		private System.Windows.Forms.TextBox textSlot;
		private System.Windows.Forms.TextBox textChecksum;
		private System.Windows.Forms.TextBox textTimeout;
		private System.Windows.Forms.TextBox textComport;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.GroupBox ReadDI;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.TextBox textBitState;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.TextBox textCounterValue;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.TextBox textTotalChannel;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.TextBox textAddress;
		private System.Windows.Forms.GroupBox groupBox2;
		/// <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.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.Close_Com = new System.Windows.Forms.Button();
			this.textBitNum = new System.Windows.Forms.TextBox();
			this.button3 = new System.Windows.Forms.Button();
			this.textCounterNum = new System.Windows.Forms.TextBox();
			this.button4 = new System.Windows.Forms.Button();
			this.button5 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.textBaudrate = new System.Windows.Forms.TextBox();
			this.textSlot = new System.Windows.Forms.TextBox();
			this.textChecksum = new System.Windows.Forms.TextBox();
			this.textTimeout = new System.Windows.Forms.TextBox();
			this.textComport = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.ReadDI = new System.Windows.Forms.GroupBox();
			this.label7 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.textBitState = new System.Windows.Forms.TextBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label9 = new System.Windows.Forms.Label();
			this.label10 = new System.Windows.Forms.Label();
			this.textCounterValue = new System.Windows.Forms.TextBox();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.textTotalChannel = new System.Windows.Forms.TextBox();
			this.label11 = new System.Windows.Forms.Label();
			this.textAddress = new System.Windows.Forms.TextBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(192, 64);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(72, 32);
			this.button1.TabIndex = 0;
			this.button1.Text = "Read DI";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(104, 280);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(64, 32);
			this.button2.TabIndex = 1;
			this.button2.Text = "Open Com";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// Close_Com
			// 
			this.Close_Com.Location = new System.Drawing.Point(240, 280);
			this.Close_Com.Name = "Close_Com";
			this.Close_Com.Size = new System.Drawing.Size(72, 32);
			this.Close_Com.TabIndex = 2;
			this.Close_Com.Text = "Close Com";
			this.Close_Com.Click += new System.EventHandler(this.Close_Com_Click);
			// 
			// textBitNum
			// 
			this.textBitNum.Location = new System.Drawing.Point(232, 152);
			this.textBitNum.Name = "textBitNum";
			this.textBitNum.Size = new System.Drawing.Size(32, 22);
			this.textBitNum.TabIndex = 4;
			this.textBitNum.Text = "";
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(184, 216);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(80, 24);
			this.button3.TabIndex = 5;
			this.button3.Text = "Read DI Bit";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// textCounterNum
			// 
			this.textCounterNum.Location = new System.Drawing.Point(344, 32);
			this.textCounterNum.Name = "textCounterNum";
			this.textCounterNum.Size = new System.Drawing.Size(32, 22);
			this.textCounterNum.TabIndex = 6;
			this.textCounterNum.Text = "";
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(24, 96);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(64, 40);
			this.button4.TabIndex = 7;
			this.button4.Text = "Read DI counter";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// button5
			// 
			this.button5.Location = new System.Drawing.Point(24, 144);
			this.button5.Name = "button5";
			this.button5.Size = new System.Drawing.Size(64, 40);
			this.button5.TabIndex = 8;
			this.button5.Text = "Clear DI Counter";
			this.button5.Click += new System.EventHandler(this.button5_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(192, 32);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(72, 22);
			this.textBox1.TabIndex = 3;
			this.textBox1.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(21, 32);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 16);
			this.label1.TabIndex = 9;
			this.label1.Text = "ComPort";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(21, 100);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 16);
			this.label2.TabIndex = 10;
			this.label2.Text = "Baudrate";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(29, 140);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(24, 16);
			this.label3.TabIndex = 11;
			this.label3.Text = "Slot";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(13, 172);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(56, 16);
			this.label4.TabIndex = 12;
			this.label4.Text = "CheckSum";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(21, 204);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(48, 16);
			this.label5.TabIndex = 13;
			this.label5.Text = "Timeout";
			// 
			// textBaudrate
			// 
			this.textBaudrate.Location = new System.Drawing.Point(78, 98);
			this.textBaudrate.Name = "textBaudrate";
			this.textBaudrate.Size = new System.Drawing.Size(72, 22);
			this.textBaudrate.TabIndex = 15;
			this.textBaudrate.Text = "9600";
			// 
			// textSlot
			// 
			this.textSlot.Location = new System.Drawing.Point(77, 132);
			this.textSlot.Name = "textSlot";
			this.textSlot.Size = new System.Drawing.Size(72, 22);
			this.textSlot.TabIndex = 16;
			this.textSlot.Text = "-1";
			// 
			// textChecksum
			// 
			this.textChecksum.Location = new System.Drawing.Point(77, 164);
			this.textChecksum.Name = "textChecksum";
			this.textChecksum.Size = new System.Drawing.Size(72, 22);
			this.textChecksum.TabIndex = 17;
			this.textChecksum.Text = "0";
			// 
			// textTimeout
			// 
			this.textTimeout.Location = new System.Drawing.Point(77, 196);
			this.textTimeout.Name = "textTimeout";
			this.textTimeout.Size = new System.Drawing.Size(72, 22);
			this.textTimeout.TabIndex = 18;
			this.textTimeout.Text = "100";
			// 
			// textComport
			// 
			this.textComport.Location = new System.Drawing.Point(77, 32);
			this.textComport.Name = "textComport";
			this.textComport.Size = new System.Drawing.Size(72, 22);
			this.textComport.TabIndex = 14;
			this.textComport.Text = "1";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(21, 228);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(48, 24);
			this.label6.TabIndex = 19;
			this.label6.Text = "Total Channel";
			// 
			// ReadDI
			// 
			this.ReadDI.Location = new System.Drawing.Point(176, 8);
			this.ReadDI.Name = "ReadDI";
			this.ReadDI.Size = new System.Drawing.Size(104, 104);
			this.ReadDI.TabIndex = 21;
			this.ReadDI.TabStop = false;
			this.ReadDI.Text = "Read DI";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(184, 144);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(48, 24);
			this.label7.TabIndex = 22;
			this.label7.Text = "Channel Number";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(192, 184);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(32, 16);
			this.label8.TabIndex = 23;
			this.label8.Text = "State";
			// 
			// textBitState
			// 
			this.textBitState.Location = new System.Drawing.Point(232, 184);
			this.textBitState.Name = "textBitState";
			this.textBitState.Size = new System.Drawing.Size(32, 22);
			this.textBitState.TabIndex = 24;
			this.textBitState.Text = "";
			// 
			// groupBox1
			// 
			this.groupBox1.Location = new System.Drawing.Point(176, 120);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(104, 144);
			this.groupBox1.TabIndex = 25;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "DI Channel state";
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(296, 32);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(48, 24);
			this.label9.TabIndex = 27;
			this.label9.Text = "Channel Number";
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(296, 72);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(56, 24);
			this.label10.TabIndex = 28;
			this.label10.Text = "Counter Value";
			// 
			// textCounterValue
			// 
			this.textCounterValue.Location = new System.Drawing.Point(344, 72);
			this.textCounterValue.Name = "textCounterValue";
			this.textCounterValue.Size = new System.Drawing.Size(48, 22);
			this.textCounterValue.TabIndex = 29;
			this.textCounterValue.Text = "";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.button4);
			this.groupBox3.Controls.Add(this.button5);
			this.groupBox3.Location = new System.Drawing.Point(288, 8);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(112, 256);
			this.groupBox3.TabIndex = 30;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "DI Counter";
			// 
			// textTotalChannel
			// 
			this.textTotalChannel.Location = new System.Drawing.Point(76, 228);
			this.textTotalChannel.Name = "textTotalChannel";
			this.textTotalChannel.Size = new System.Drawing.Size(72, 22);
			this.textTotalChannel.TabIndex = 31;
			this.textTotalChannel.Text = "16";
			// 
			// 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 = 32;
			this.label11.Text = "Address";
			// 
			// textAddress
			// 
			this.textAddress.Location = new System.Drawing.Point(77, 64);
			this.textAddress.Name = "textAddress";
			this.textAddress.Size = new System.Drawing.Size(72, 22);
			this.textAddress.TabIndex = 33;
			this.textAddress.Text = "1";
			// 
			// groupBox2
			// 
			this.groupBox2.Location = new System.Drawing.Point(8, 8);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(160, 256);
			this.groupBox2.TabIndex = 34;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Setting";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
			this.ClientSize = new System.Drawing.Size(408, 325);
			this.Controls.Add(this.textAddress);
			this.Controls.Add(this.label11);
			this.Controls.Add(this.textTotalChannel);
			this.Controls.Add(this.textCounterValue);
			this.Controls.Add(this.label10);
			this.Controls.Add(this.label9);
			this.Controls.Add(this.textBitState);
			this.Controls.Add(this.label8);
			this.Controls.Add(this.label7);
			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.textCounterNum);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.textBitNum);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.ReadDI);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.Close_Com);
			this.Controls.Add(this.button2);
			this.Name = "Form1";
			this.Text = "Form1";
			this.groupBox3.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 button1_Click(object sender, System.EventArgs e)
		{
			uint DIValue;
			short iRet;

			iRet=DCON.IO_Function.Read_DI(iComport,iAddress,iSlot,iTotalChannel,bChecksum,iTimeout,out DIValue);
			
			if(Convert.ToBoolean( iRet))
				MessageBox.Show("ErrorCode " + Convert.ToString(iRet));
			else
				textBox1.Text=Convert.ToString(DIValue,16);
		
		}

		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);
			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 button3_Click(object sender, System.EventArgs e)
		{
			bool iBit;
			short iRet;

			iRet=DCON.IO_Function.Read_DI_Bit(iComport,iAddress,iSlot,Convert.ToInt16(textBitNum.Text),iTotalChannel,bChecksum,iTimeout,out iBit);
			
			if(Convert.ToBoolean( iRet))
				MessageBox.Show("ErrorCode " + Convert.ToString(iRet));
			else
				textBitState.Text=Convert.ToString(iBit);
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			uint DIcounter;
			short iRet;
			
			iRet=DCON.IO_Function.Read_DI_Counter(iComport,iAddress,iSlot,Convert.ToInt16(textCounterNum.Text),iTotalChannel,bChecksum,iTimeout, out DIcounter);
			
			if(Convert.ToBoolean( iRet))
				MessageBox.Show("ErrorCode " + Convert.ToString(iRet));
			else
				textCounterValue.Text=Convert.ToString(DIcounter);

		}

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