﻿// Usual Counter Sample
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CcntCs;

namespace Counter
{
	/// <summary>
	/// Summary description for Counter.
	/// </summary>
	public class Counter : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label_DeviceName;
		private System.Windows.Forms.Label label_Channel0;
		private System.Windows.Forms.Label label_Channel1;
		private System.Windows.Forms.Label label_Channel2;
		private System.Windows.Forms.Label label_Channel3;
		private System.Windows.Forms.Label label_Channel4;
		private System.Windows.Forms.Label label_Channel5;
		private System.Windows.Forms.Label label_Channel6;
		private System.Windows.Forms.Label label_Channel7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.GroupBox groupBox_CountValue;
		private System.Windows.Forms.GroupBox groupBox_Status;
		private System.Windows.Forms.GroupBox groupBox_PresetValue;
		private System.Windows.Forms.TextBox textBox_DeviceName;
		private System.Windows.Forms.RadioButton radioButtonCountValue_Dec;
		private System.Windows.Forms.RadioButton radioButton_CountValue_Hex;
		private System.Windows.Forms.TextBox textBox_CountValue_Channel0;
		private System.Windows.Forms.TextBox textBox_CountValue_Channel1;
		private System.Windows.Forms.TextBox textBox_CountValue_Channel2;
		private System.Windows.Forms.TextBox textBox_CountValue_Channel3;
		private System.Windows.Forms.TextBox textBox_CountValue_Channel4;
		private System.Windows.Forms.TextBox textBox_CountValue_Channel5;
		private System.Windows.Forms.TextBox textBox_CountValue_Channel6;
		private System.Windows.Forms.TextBox textBox_CountValue_Channel7;
		private System.Windows.Forms.TextBox textBox_Status_Channel0;
		private System.Windows.Forms.TextBox textBox_Status_Channel1;
		private System.Windows.Forms.TextBox textBox_Status_Channel2;
		private System.Windows.Forms.TextBox textBox_Status_Channel3;
		private System.Windows.Forms.TextBox textBox_Status_Channel4;
		private System.Windows.Forms.TextBox textBox_Status_Channel5;
		private System.Windows.Forms.TextBox textBox_Status_Channel6;
		private System.Windows.Forms.TextBox textBox_Status_Channel7;
		private System.Windows.Forms.TextBox textBox_PresetValue_Channel0;
		private System.Windows.Forms.TextBox textBox_PresetValue_Channel1;
		private System.Windows.Forms.TextBox textBox_PresetValue_Channel2;
		private System.Windows.Forms.TextBox textBox_PresetValue_Channel3;
		private System.Windows.Forms.TextBox textBox_PresetValue_Channel4;
		private System.Windows.Forms.TextBox textBox_PresetValue_Channel5;
		private System.Windows.Forms.TextBox textBox_PresetValue_Channel6;
		private System.Windows.Forms.TextBox textBox_PresetValue_Channel7;
		private System.Windows.Forms.RadioButton radioButton_PresetValue_Hex;
		private System.Windows.Forms.RadioButton radioButton_PresetValue_Dec;
		private System.Windows.Forms.Button button_CntInit;
		private System.Windows.Forms.Button button_ModeSet;
		private System.Windows.Forms.Button button_ComparisonSet;
		private System.Windows.Forms.Button button_CntStartCount;
		private System.Windows.Forms.Button button_CntPreset;
		private System.Windows.Forms.Button button_CntStopCount;
		private System.Windows.Forms.Button button_CntExit;
		private System.Windows.Forms.Button button_Exit;
		private System.Windows.Forms.TextBox textBox_Comparison_result;
		private System.Windows.Forms.TextBox textBox_ReturnCode;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components;

		private System.Windows.Forms.TextBox[] m_CH = new System.Windows.Forms.TextBox[8];
		private System.Windows.Forms.TextBox[] m_Status = new System.Windows.Forms.TextBox[8];
		private System.Windows.Forms.TextBox[] m_Preset = new System.Windows.Forms.TextBox[8];

		private System.Windows.Forms.Timer timer1;

		/// <summary>
		/// Create instance Ccnt class
		/// </summary>
		Ccnt  cnt = new Ccnt();

		public static IntPtr m_Handle;
		public static short  m_Id;
		public static short  m_ChannelNum;
		public uint[]        PresetData = new uint[8] { 0, 100, 200, 300, 400, 500, 600, 700 };
		public int           MatchCount;

		public Counter()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			m_CH[0] = textBox_CountValue_Channel0;
			m_CH[1] = textBox_CountValue_Channel1;
			m_CH[2] = textBox_CountValue_Channel2;
			m_CH[3] = textBox_CountValue_Channel3;
			m_CH[4] = textBox_CountValue_Channel4;
			m_CH[5] = textBox_CountValue_Channel5;
			m_CH[6] = textBox_CountValue_Channel6;
			m_CH[7] = textBox_CountValue_Channel7;

			m_Status[0] = textBox_Status_Channel0;
			m_Status[1] = textBox_Status_Channel1;
			m_Status[2] = textBox_Status_Channel2;
			m_Status[3] = textBox_Status_Channel3;
			m_Status[4] = textBox_Status_Channel4;
			m_Status[5] = textBox_Status_Channel5;
			m_Status[6] = textBox_Status_Channel6;
			m_Status[7] = textBox_Status_Channel7;

			m_Preset[0] = textBox_PresetValue_Channel0;
			m_Preset[1] = textBox_PresetValue_Channel1;
			m_Preset[2] = textBox_PresetValue_Channel2;
			m_Preset[3] = textBox_PresetValue_Channel3;
			m_Preset[4] = textBox_PresetValue_Channel4;
			m_Preset[5] = textBox_PresetValue_Channel5;
			m_Preset[6] = textBox_PresetValue_Channel6;
			m_Preset[7] = textBox_PresetValue_Channel7;

			m_Handle = this.Handle;
		}

		/// <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()
		{
            this.components = new System.ComponentModel.Container();
            this.label_DeviceName = new System.Windows.Forms.Label();
            this.label_Channel0 = new System.Windows.Forms.Label();
            this.label_Channel1 = new System.Windows.Forms.Label();
            this.label_Channel2 = new System.Windows.Forms.Label();
            this.label_Channel3 = new System.Windows.Forms.Label();
            this.label_Channel4 = new System.Windows.Forms.Label();
            this.label_Channel5 = new System.Windows.Forms.Label();
            this.label_Channel6 = new System.Windows.Forms.Label();
            this.label_Channel7 = new System.Windows.Forms.Label();
            this.label8 = new System.Windows.Forms.Label();
            this.label9 = new System.Windows.Forms.Label();
            this.groupBox_CountValue = new System.Windows.Forms.GroupBox();
            this.textBox_CountValue_Channel7 = new System.Windows.Forms.TextBox();
            this.textBox_CountValue_Channel6 = new System.Windows.Forms.TextBox();
            this.textBox_CountValue_Channel5 = new System.Windows.Forms.TextBox();
            this.textBox_CountValue_Channel4 = new System.Windows.Forms.TextBox();
            this.textBox_CountValue_Channel3 = new System.Windows.Forms.TextBox();
            this.textBox_CountValue_Channel2 = new System.Windows.Forms.TextBox();
            this.textBox_CountValue_Channel1 = new System.Windows.Forms.TextBox();
            this.textBox_CountValue_Channel0 = new System.Windows.Forms.TextBox();
            this.radioButton_CountValue_Hex = new System.Windows.Forms.RadioButton();
            this.radioButtonCountValue_Dec = new System.Windows.Forms.RadioButton();
            this.groupBox_Status = new System.Windows.Forms.GroupBox();
            this.textBox_Status_Channel0 = new System.Windows.Forms.TextBox();
            this.textBox_Status_Channel3 = new System.Windows.Forms.TextBox();
            this.textBox_Status_Channel5 = new System.Windows.Forms.TextBox();
            this.textBox_Status_Channel2 = new System.Windows.Forms.TextBox();
            this.textBox_Status_Channel1 = new System.Windows.Forms.TextBox();
            this.textBox_Status_Channel6 = new System.Windows.Forms.TextBox();
            this.textBox_Status_Channel4 = new System.Windows.Forms.TextBox();
            this.textBox_Status_Channel7 = new System.Windows.Forms.TextBox();
            this.groupBox_PresetValue = new System.Windows.Forms.GroupBox();
            this.textBox_PresetValue_Channel7 = new System.Windows.Forms.TextBox();
            this.radioButton_PresetValue_Hex = new System.Windows.Forms.RadioButton();
            this.radioButton_PresetValue_Dec = new System.Windows.Forms.RadioButton();
            this.textBox_PresetValue_Channel6 = new System.Windows.Forms.TextBox();
            this.textBox_PresetValue_Channel5 = new System.Windows.Forms.TextBox();
            this.textBox_PresetValue_Channel4 = new System.Windows.Forms.TextBox();
            this.textBox_PresetValue_Channel3 = new System.Windows.Forms.TextBox();
            this.textBox_PresetValue_Channel2 = new System.Windows.Forms.TextBox();
            this.textBox_PresetValue_Channel1 = new System.Windows.Forms.TextBox();
            this.textBox_PresetValue_Channel0 = new System.Windows.Forms.TextBox();
            this.textBox_DeviceName = new System.Windows.Forms.TextBox();
            this.button_CntInit = new System.Windows.Forms.Button();
            this.button_ModeSet = new System.Windows.Forms.Button();
            this.button_ComparisonSet = new System.Windows.Forms.Button();
            this.button_CntStartCount = new System.Windows.Forms.Button();
            this.button_CntPreset = new System.Windows.Forms.Button();
            this.button_CntStopCount = new System.Windows.Forms.Button();
            this.button_CntExit = new System.Windows.Forms.Button();
            this.button_Exit = new System.Windows.Forms.Button();
            this.textBox_Comparison_result = new System.Windows.Forms.TextBox();
            this.textBox_ReturnCode = new System.Windows.Forms.TextBox();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.groupBox_CountValue.SuspendLayout();
            this.groupBox_Status.SuspendLayout();
            this.groupBox_PresetValue.SuspendLayout();
            this.SuspendLayout();
            // 
            // label_DeviceName
            // 
            this.label_DeviceName.Location = new System.Drawing.Point(16, 17);
            this.label_DeviceName.Name = "label_DeviceName";
            this.label_DeviceName.Size = new System.Drawing.Size(80, 18);
            this.label_DeviceName.TabIndex = 0;
            this.label_DeviceName.Text = "Device Name:";
            // 
            // label_Channel0
            // 
            this.label_Channel0.Location = new System.Drawing.Point(16, 139);
            this.label_Channel0.Name = "label_Channel0";
            this.label_Channel0.Size = new System.Drawing.Size(80, 17);
            this.label_Channel0.TabIndex = 1;
            this.label_Channel0.Text = "Channel0:";
            // 
            // label_Channel1
            // 
            this.label_Channel1.Location = new System.Drawing.Point(16, 173);
            this.label_Channel1.Name = "label_Channel1";
            this.label_Channel1.Size = new System.Drawing.Size(80, 18);
            this.label_Channel1.TabIndex = 2;
            this.label_Channel1.Text = "Channel1:";
            // 
            // label_Channel2
            // 
            this.label_Channel2.Location = new System.Drawing.Point(16, 208);
            this.label_Channel2.Name = "label_Channel2";
            this.label_Channel2.Size = new System.Drawing.Size(80, 17);
            this.label_Channel2.TabIndex = 3;
            this.label_Channel2.Text = "Channel2:";
            // 
            // label_Channel3
            // 
            this.label_Channel3.Location = new System.Drawing.Point(16, 243);
            this.label_Channel3.Name = "label_Channel3";
            this.label_Channel3.Size = new System.Drawing.Size(80, 17);
            this.label_Channel3.TabIndex = 4;
            this.label_Channel3.Text = "Channel3:";
            // 
            // label_Channel4
            // 
            this.label_Channel4.Location = new System.Drawing.Point(16, 277);
            this.label_Channel4.Name = "label_Channel4";
            this.label_Channel4.Size = new System.Drawing.Size(80, 18);
            this.label_Channel4.TabIndex = 5;
            this.label_Channel4.Text = "Channel4:";
            // 
            // label_Channel5
            // 
            this.label_Channel5.Location = new System.Drawing.Point(16, 312);
            this.label_Channel5.Name = "label_Channel5";
            this.label_Channel5.Size = new System.Drawing.Size(80, 17);
            this.label_Channel5.TabIndex = 6;
            this.label_Channel5.Text = "Channel5:";
            // 
            // label_Channel6
            // 
            this.label_Channel6.Location = new System.Drawing.Point(16, 347);
            this.label_Channel6.Name = "label_Channel6";
            this.label_Channel6.Size = new System.Drawing.Size(80, 17);
            this.label_Channel6.TabIndex = 7;
            this.label_Channel6.Text = "Channel6:";
            // 
            // label_Channel7
            // 
            this.label_Channel7.Location = new System.Drawing.Point(16, 381);
            this.label_Channel7.Name = "label_Channel7";
            this.label_Channel7.Size = new System.Drawing.Size(80, 18);
            this.label_Channel7.TabIndex = 8;
            this.label_Channel7.Text = "Channel7:";
            // 
            // label8
            // 
            this.label8.Location = new System.Drawing.Point(16, 433);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(104, 18);
            this.label8.TabIndex = 9;
            this.label8.Text = "Comparison result:";
            // 
            // label9
            // 
            this.label9.Location = new System.Drawing.Point(16, 468);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(80, 17);
            this.label9.TabIndex = 10;
            this.label9.Text = "Retunr Code:";
            // 
            // groupBox_CountValue
            // 
            this.groupBox_CountValue.Controls.Add(this.textBox_CountValue_Channel7);
            this.groupBox_CountValue.Controls.Add(this.textBox_CountValue_Channel6);
            this.groupBox_CountValue.Controls.Add(this.textBox_CountValue_Channel5);
            this.groupBox_CountValue.Controls.Add(this.textBox_CountValue_Channel4);
            this.groupBox_CountValue.Controls.Add(this.textBox_CountValue_Channel3);
            this.groupBox_CountValue.Controls.Add(this.textBox_CountValue_Channel2);
            this.groupBox_CountValue.Controls.Add(this.textBox_CountValue_Channel1);
            this.groupBox_CountValue.Controls.Add(this.textBox_CountValue_Channel0);
            this.groupBox_CountValue.Controls.Add(this.radioButton_CountValue_Hex);
            this.groupBox_CountValue.Controls.Add(this.radioButtonCountValue_Dec);
            this.groupBox_CountValue.Location = new System.Drawing.Point(104, 61);
            this.groupBox_CountValue.Name = "groupBox_CountValue";
            this.groupBox_CountValue.Size = new System.Drawing.Size(128, 355);
            this.groupBox_CountValue.TabIndex = 11;
            this.groupBox_CountValue.TabStop = false;
            this.groupBox_CountValue.Text = "Count Value";
            // 
            // textBox_CountValue_Channel7
            // 
            this.textBox_CountValue_Channel7.Location = new System.Drawing.Point(16, 321);
            this.textBox_CountValue_Channel7.Name = "textBox_CountValue_Channel7";
            this.textBox_CountValue_Channel7.Size = new System.Drawing.Size(100, 20);
            this.textBox_CountValue_Channel7.TabIndex = 22;
            this.textBox_CountValue_Channel7.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_CountValue_Channel6
            // 
            this.textBox_CountValue_Channel6.Location = new System.Drawing.Point(16, 286);
            this.textBox_CountValue_Channel6.Name = "textBox_CountValue_Channel6";
            this.textBox_CountValue_Channel6.Size = new System.Drawing.Size(100, 20);
            this.textBox_CountValue_Channel6.TabIndex = 21;
            this.textBox_CountValue_Channel6.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_CountValue_Channel5
            // 
            this.textBox_CountValue_Channel5.Location = new System.Drawing.Point(16, 251);
            this.textBox_CountValue_Channel5.Name = "textBox_CountValue_Channel5";
            this.textBox_CountValue_Channel5.Size = new System.Drawing.Size(100, 20);
            this.textBox_CountValue_Channel5.TabIndex = 20;
            this.textBox_CountValue_Channel5.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_CountValue_Channel4
            // 
            this.textBox_CountValue_Channel4.Location = new System.Drawing.Point(16, 217);
            this.textBox_CountValue_Channel4.Name = "textBox_CountValue_Channel4";
            this.textBox_CountValue_Channel4.Size = new System.Drawing.Size(100, 20);
            this.textBox_CountValue_Channel4.TabIndex = 19;
            this.textBox_CountValue_Channel4.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_CountValue_Channel3
            // 
            this.textBox_CountValue_Channel3.Location = new System.Drawing.Point(16, 182);
            this.textBox_CountValue_Channel3.Name = "textBox_CountValue_Channel3";
            this.textBox_CountValue_Channel3.Size = new System.Drawing.Size(100, 20);
            this.textBox_CountValue_Channel3.TabIndex = 18;
            this.textBox_CountValue_Channel3.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_CountValue_Channel2
            // 
            this.textBox_CountValue_Channel2.Location = new System.Drawing.Point(16, 147);
            this.textBox_CountValue_Channel2.Name = "textBox_CountValue_Channel2";
            this.textBox_CountValue_Channel2.Size = new System.Drawing.Size(100, 20);
            this.textBox_CountValue_Channel2.TabIndex = 17;
            this.textBox_CountValue_Channel2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_CountValue_Channel1
            // 
            this.textBox_CountValue_Channel1.Location = new System.Drawing.Point(16, 113);
            this.textBox_CountValue_Channel1.Name = "textBox_CountValue_Channel1";
            this.textBox_CountValue_Channel1.Size = new System.Drawing.Size(100, 20);
            this.textBox_CountValue_Channel1.TabIndex = 16;
            this.textBox_CountValue_Channel1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_CountValue_Channel0
            // 
            this.textBox_CountValue_Channel0.Location = new System.Drawing.Point(16, 78);
            this.textBox_CountValue_Channel0.Name = "textBox_CountValue_Channel0";
            this.textBox_CountValue_Channel0.Size = new System.Drawing.Size(100, 20);
            this.textBox_CountValue_Channel0.TabIndex = 15;
            this.textBox_CountValue_Channel0.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // radioButton_CountValue_Hex
            // 
            this.radioButton_CountValue_Hex.Location = new System.Drawing.Point(16, 43);
            this.radioButton_CountValue_Hex.Name = "radioButton_CountValue_Hex";
            this.radioButton_CountValue_Hex.Size = new System.Drawing.Size(104, 26);
            this.radioButton_CountValue_Hex.TabIndex = 1;
            this.radioButton_CountValue_Hex.Text = "Hex";
            // 
            // radioButtonCountValue_Dec
            // 
            this.radioButtonCountValue_Dec.Checked = true;
            this.radioButtonCountValue_Dec.Location = new System.Drawing.Point(16, 17);
            this.radioButtonCountValue_Dec.Name = "radioButtonCountValue_Dec";
            this.radioButtonCountValue_Dec.Size = new System.Drawing.Size(104, 26);
            this.radioButtonCountValue_Dec.TabIndex = 0;
            this.radioButtonCountValue_Dec.TabStop = true;
            this.radioButtonCountValue_Dec.Text = "Dec";
            // 
            // groupBox_Status
            // 
            this.groupBox_Status.Controls.Add(this.textBox_Status_Channel0);
            this.groupBox_Status.Controls.Add(this.textBox_Status_Channel3);
            this.groupBox_Status.Controls.Add(this.textBox_Status_Channel5);
            this.groupBox_Status.Controls.Add(this.textBox_Status_Channel2);
            this.groupBox_Status.Controls.Add(this.textBox_Status_Channel1);
            this.groupBox_Status.Controls.Add(this.textBox_Status_Channel6);
            this.groupBox_Status.Controls.Add(this.textBox_Status_Channel4);
            this.groupBox_Status.Controls.Add(this.textBox_Status_Channel7);
            this.groupBox_Status.Location = new System.Drawing.Point(240, 61);
            this.groupBox_Status.Name = "groupBox_Status";
            this.groupBox_Status.Size = new System.Drawing.Size(120, 355);
            this.groupBox_Status.TabIndex = 12;
            this.groupBox_Status.TabStop = false;
            this.groupBox_Status.Text = "Status";
            // 
            // textBox_Status_Channel0
            // 
            this.textBox_Status_Channel0.Location = new System.Drawing.Point(8, 78);
            this.textBox_Status_Channel0.Name = "textBox_Status_Channel0";
            this.textBox_Status_Channel0.Size = new System.Drawing.Size(100, 20);
            this.textBox_Status_Channel0.TabIndex = 30;
            this.textBox_Status_Channel0.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_Status_Channel3
            // 
            this.textBox_Status_Channel3.Location = new System.Drawing.Point(8, 182);
            this.textBox_Status_Channel3.Name = "textBox_Status_Channel3";
            this.textBox_Status_Channel3.Size = new System.Drawing.Size(100, 20);
            this.textBox_Status_Channel3.TabIndex = 26;
            this.textBox_Status_Channel3.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_Status_Channel5
            // 
            this.textBox_Status_Channel5.Location = new System.Drawing.Point(8, 251);
            this.textBox_Status_Channel5.Name = "textBox_Status_Channel5";
            this.textBox_Status_Channel5.Size = new System.Drawing.Size(100, 20);
            this.textBox_Status_Channel5.TabIndex = 28;
            this.textBox_Status_Channel5.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_Status_Channel2
            // 
            this.textBox_Status_Channel2.Location = new System.Drawing.Point(8, 147);
            this.textBox_Status_Channel2.Name = "textBox_Status_Channel2";
            this.textBox_Status_Channel2.Size = new System.Drawing.Size(100, 20);
            this.textBox_Status_Channel2.TabIndex = 25;
            this.textBox_Status_Channel2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_Status_Channel1
            // 
            this.textBox_Status_Channel1.Location = new System.Drawing.Point(8, 113);
            this.textBox_Status_Channel1.Name = "textBox_Status_Channel1";
            this.textBox_Status_Channel1.Size = new System.Drawing.Size(100, 20);
            this.textBox_Status_Channel1.TabIndex = 24;
            this.textBox_Status_Channel1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_Status_Channel6
            // 
            this.textBox_Status_Channel6.Location = new System.Drawing.Point(8, 286);
            this.textBox_Status_Channel6.Name = "textBox_Status_Channel6";
            this.textBox_Status_Channel6.Size = new System.Drawing.Size(100, 20);
            this.textBox_Status_Channel6.TabIndex = 29;
            this.textBox_Status_Channel6.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_Status_Channel4
            // 
            this.textBox_Status_Channel4.Location = new System.Drawing.Point(8, 217);
            this.textBox_Status_Channel4.Name = "textBox_Status_Channel4";
            this.textBox_Status_Channel4.Size = new System.Drawing.Size(100, 20);
            this.textBox_Status_Channel4.TabIndex = 27;
            this.textBox_Status_Channel4.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_Status_Channel7
            // 
            this.textBox_Status_Channel7.Location = new System.Drawing.Point(8, 321);
            this.textBox_Status_Channel7.Name = "textBox_Status_Channel7";
            this.textBox_Status_Channel7.Size = new System.Drawing.Size(100, 20);
            this.textBox_Status_Channel7.TabIndex = 25;
            this.textBox_Status_Channel7.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // groupBox_PresetValue
            // 
            this.groupBox_PresetValue.Controls.Add(this.textBox_PresetValue_Channel7);
            this.groupBox_PresetValue.Controls.Add(this.radioButton_PresetValue_Hex);
            this.groupBox_PresetValue.Controls.Add(this.radioButton_PresetValue_Dec);
            this.groupBox_PresetValue.Controls.Add(this.textBox_PresetValue_Channel6);
            this.groupBox_PresetValue.Controls.Add(this.textBox_PresetValue_Channel5);
            this.groupBox_PresetValue.Controls.Add(this.textBox_PresetValue_Channel4);
            this.groupBox_PresetValue.Controls.Add(this.textBox_PresetValue_Channel3);
            this.groupBox_PresetValue.Controls.Add(this.textBox_PresetValue_Channel2);
            this.groupBox_PresetValue.Controls.Add(this.textBox_PresetValue_Channel1);
            this.groupBox_PresetValue.Controls.Add(this.textBox_PresetValue_Channel0);
            this.groupBox_PresetValue.Location = new System.Drawing.Point(368, 61);
            this.groupBox_PresetValue.Name = "groupBox_PresetValue";
            this.groupBox_PresetValue.Size = new System.Drawing.Size(128, 355);
            this.groupBox_PresetValue.TabIndex = 13;
            this.groupBox_PresetValue.TabStop = false;
            this.groupBox_PresetValue.Text = "Preset Value";
            // 
            // textBox_PresetValue_Channel7
            // 
            this.textBox_PresetValue_Channel7.Location = new System.Drawing.Point(16, 321);
            this.textBox_PresetValue_Channel7.Name = "textBox_PresetValue_Channel7";
            this.textBox_PresetValue_Channel7.Size = new System.Drawing.Size(100, 20);
            this.textBox_PresetValue_Channel7.TabIndex = 32;
            this.textBox_PresetValue_Channel7.Text = "700";
            this.textBox_PresetValue_Channel7.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // radioButton_PresetValue_Hex
            // 
            this.radioButton_PresetValue_Hex.Location = new System.Drawing.Point(16, 43);
            this.radioButton_PresetValue_Hex.Name = "radioButton_PresetValue_Hex";
            this.radioButton_PresetValue_Hex.Size = new System.Drawing.Size(104, 26);
            this.radioButton_PresetValue_Hex.TabIndex = 24;
            this.radioButton_PresetValue_Hex.Text = "Hex";
            this.radioButton_PresetValue_Hex.CheckedChanged += new System.EventHandler(this.radioButton_PresetValue_Hex_CheckedChanged);
            // 
            // radioButton_PresetValue_Dec
            // 
            this.radioButton_PresetValue_Dec.Checked = true;
            this.radioButton_PresetValue_Dec.Location = new System.Drawing.Point(16, 17);
            this.radioButton_PresetValue_Dec.Name = "radioButton_PresetValue_Dec";
            this.radioButton_PresetValue_Dec.Size = new System.Drawing.Size(104, 26);
            this.radioButton_PresetValue_Dec.TabIndex = 23;
            this.radioButton_PresetValue_Dec.TabStop = true;
            this.radioButton_PresetValue_Dec.Text = "Dec";
            this.radioButton_PresetValue_Dec.CheckedChanged += new System.EventHandler(this.radioButton_PresetValue_Dec_CheckedChanged);
            // 
            // textBox_PresetValue_Channel6
            // 
            this.textBox_PresetValue_Channel6.Location = new System.Drawing.Point(16, 286);
            this.textBox_PresetValue_Channel6.Name = "textBox_PresetValue_Channel6";
            this.textBox_PresetValue_Channel6.Size = new System.Drawing.Size(100, 20);
            this.textBox_PresetValue_Channel6.TabIndex = 31;
            this.textBox_PresetValue_Channel6.Text = "600";
            this.textBox_PresetValue_Channel6.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_PresetValue_Channel5
            // 
            this.textBox_PresetValue_Channel5.Location = new System.Drawing.Point(16, 251);
            this.textBox_PresetValue_Channel5.Name = "textBox_PresetValue_Channel5";
            this.textBox_PresetValue_Channel5.Size = new System.Drawing.Size(100, 20);
            this.textBox_PresetValue_Channel5.TabIndex = 30;
            this.textBox_PresetValue_Channel5.Text = "500";
            this.textBox_PresetValue_Channel5.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_PresetValue_Channel4
            // 
            this.textBox_PresetValue_Channel4.Location = new System.Drawing.Point(16, 217);
            this.textBox_PresetValue_Channel4.Name = "textBox_PresetValue_Channel4";
            this.textBox_PresetValue_Channel4.Size = new System.Drawing.Size(100, 20);
            this.textBox_PresetValue_Channel4.TabIndex = 29;
            this.textBox_PresetValue_Channel4.Text = "400";
            this.textBox_PresetValue_Channel4.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_PresetValue_Channel3
            // 
            this.textBox_PresetValue_Channel3.Location = new System.Drawing.Point(16, 182);
            this.textBox_PresetValue_Channel3.Name = "textBox_PresetValue_Channel3";
            this.textBox_PresetValue_Channel3.Size = new System.Drawing.Size(100, 20);
            this.textBox_PresetValue_Channel3.TabIndex = 28;
            this.textBox_PresetValue_Channel3.Text = "300";
            this.textBox_PresetValue_Channel3.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_PresetValue_Channel2
            // 
            this.textBox_PresetValue_Channel2.Location = new System.Drawing.Point(16, 147);
            this.textBox_PresetValue_Channel2.Name = "textBox_PresetValue_Channel2";
            this.textBox_PresetValue_Channel2.Size = new System.Drawing.Size(100, 20);
            this.textBox_PresetValue_Channel2.TabIndex = 27;
            this.textBox_PresetValue_Channel2.Text = "200";
            this.textBox_PresetValue_Channel2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_PresetValue_Channel1
            // 
            this.textBox_PresetValue_Channel1.Location = new System.Drawing.Point(16, 113);
            this.textBox_PresetValue_Channel1.Name = "textBox_PresetValue_Channel1";
            this.textBox_PresetValue_Channel1.Size = new System.Drawing.Size(100, 20);
            this.textBox_PresetValue_Channel1.TabIndex = 26;
            this.textBox_PresetValue_Channel1.Text = "100";
            this.textBox_PresetValue_Channel1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_PresetValue_Channel0
            // 
            this.textBox_PresetValue_Channel0.Location = new System.Drawing.Point(16, 78);
            this.textBox_PresetValue_Channel0.Name = "textBox_PresetValue_Channel0";
            this.textBox_PresetValue_Channel0.Size = new System.Drawing.Size(100, 20);
            this.textBox_PresetValue_Channel0.TabIndex = 27;
            this.textBox_PresetValue_Channel0.Text = "0";
            this.textBox_PresetValue_Channel0.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // textBox_DeviceName
            // 
            this.textBox_DeviceName.Location = new System.Drawing.Point(120, 17);
            this.textBox_DeviceName.Name = "textBox_DeviceName";
            this.textBox_DeviceName.Size = new System.Drawing.Size(100, 20);
            this.textBox_DeviceName.TabIndex = 14;
            this.textBox_DeviceName.Text = "CNT000";
            // 
            // button_CntInit
            // 
            this.button_CntInit.Location = new System.Drawing.Point(512, 17);
            this.button_CntInit.Name = "button_CntInit";
            this.button_CntInit.Size = new System.Drawing.Size(112, 26);
            this.button_CntInit.TabIndex = 15;
            this.button_CntInit.Text = "CntInit";
            this.button_CntInit.Click += new System.EventHandler(this.button_CntInit_Click);
            // 
            // button_ModeSet
            // 
            this.button_ModeSet.Location = new System.Drawing.Point(512, 69);
            this.button_ModeSet.Name = "button_ModeSet";
            this.button_ModeSet.Size = new System.Drawing.Size(112, 26);
            this.button_ModeSet.TabIndex = 16;
            this.button_ModeSet.Text = "Mode Set...";
            this.button_ModeSet.Click += new System.EventHandler(this.button_ModeSet_Click);
            // 
            // button_ComparisonSet
            // 
            this.button_ComparisonSet.Location = new System.Drawing.Point(512, 113);
            this.button_ComparisonSet.Name = "button_ComparisonSet";
            this.button_ComparisonSet.Size = new System.Drawing.Size(112, 26);
            this.button_ComparisonSet.TabIndex = 17;
            this.button_ComparisonSet.Text = "Comparison Set...";
            this.button_ComparisonSet.Click += new System.EventHandler(this.button_ComparisonSet_Click);
            // 
            // button_CntStartCount
            // 
            this.button_CntStartCount.Location = new System.Drawing.Point(512, 173);
            this.button_CntStartCount.Name = "button_CntStartCount";
            this.button_CntStartCount.Size = new System.Drawing.Size(112, 26);
            this.button_CntStartCount.TabIndex = 18;
            this.button_CntStartCount.Text = "CntStartCount";
            this.button_CntStartCount.Click += new System.EventHandler(this.button_CntStartCount_Click);
            // 
            // button_CntPreset
            // 
            this.button_CntPreset.Location = new System.Drawing.Point(512, 217);
            this.button_CntPreset.Name = "button_CntPreset";
            this.button_CntPreset.Size = new System.Drawing.Size(112, 26);
            this.button_CntPreset.TabIndex = 19;
            this.button_CntPreset.Text = "CntPreset";
            this.button_CntPreset.Click += new System.EventHandler(this.button_CntPreset_Click);
            // 
            // button_CntStopCount
            // 
            this.button_CntStopCount.Location = new System.Drawing.Point(512, 260);
            this.button_CntStopCount.Name = "button_CntStopCount";
            this.button_CntStopCount.Size = new System.Drawing.Size(112, 26);
            this.button_CntStopCount.TabIndex = 20;
            this.button_CntStopCount.Text = "CntStopCount";
            this.button_CntStopCount.Click += new System.EventHandler(this.button_CntStopCount_Click);
            // 
            // button_CntExit
            // 
            this.button_CntExit.Location = new System.Drawing.Point(512, 425);
            this.button_CntExit.Name = "button_CntExit";
            this.button_CntExit.Size = new System.Drawing.Size(112, 26);
            this.button_CntExit.TabIndex = 21;
            this.button_CntExit.Text = "CntExit";
            this.button_CntExit.Click += new System.EventHandler(this.button_CntExit_Click);
            // 
            // button_Exit
            // 
            this.button_Exit.Location = new System.Drawing.Point(512, 468);
            this.button_Exit.Name = "button_Exit";
            this.button_Exit.Size = new System.Drawing.Size(112, 26);
            this.button_Exit.TabIndex = 22;
            this.button_Exit.Text = "Exit";
            this.button_Exit.Click += new System.EventHandler(this.button_Exit_Click);
            // 
            // textBox_Comparison_result
            // 
            this.textBox_Comparison_result.Location = new System.Drawing.Point(120, 433);
            this.textBox_Comparison_result.Name = "textBox_Comparison_result";
            this.textBox_Comparison_result.Size = new System.Drawing.Size(368, 20);
            this.textBox_Comparison_result.TabIndex = 23;
            // 
            // textBox_ReturnCode
            // 
            this.textBox_ReturnCode.Location = new System.Drawing.Point(120, 468);
            this.textBox_ReturnCode.Name = "textBox_ReturnCode";
            this.textBox_ReturnCode.Size = new System.Drawing.Size(368, 20);
            this.textBox_ReturnCode.TabIndex = 24;
            // 
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // Counter
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(640, 509);
            this.Controls.Add(this.textBox_ReturnCode);
            this.Controls.Add(this.textBox_Comparison_result);
            this.Controls.Add(this.button_Exit);
            this.Controls.Add(this.button_CntExit);
            this.Controls.Add(this.button_CntStopCount);
            this.Controls.Add(this.button_CntPreset);
            this.Controls.Add(this.button_CntStartCount);
            this.Controls.Add(this.button_ComparisonSet);
            this.Controls.Add(this.button_ModeSet);
            this.Controls.Add(this.button_CntInit);
            this.Controls.Add(this.textBox_DeviceName);
            this.Controls.Add(this.groupBox_PresetValue);
            this.Controls.Add(this.groupBox_Status);
            this.Controls.Add(this.groupBox_CountValue);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.label8);
            this.Controls.Add(this.label_Channel7);
            this.Controls.Add(this.label_Channel6);
            this.Controls.Add(this.label_Channel5);
            this.Controls.Add(this.label_Channel4);
            this.Controls.Add(this.label_Channel3);
            this.Controls.Add(this.label_Channel2);
            this.Controls.Add(this.label_Channel1);
            this.Controls.Add(this.label_Channel0);
            this.Controls.Add(this.label_DeviceName);
            this.Name = "Counter";
            this.Text = "Counter";
            this.groupBox_CountValue.ResumeLayout(false);
            this.groupBox_CountValue.PerformLayout();
            this.groupBox_Status.ResumeLayout(false);
            this.groupBox_Status.PerformLayout();
            this.groupBox_PresetValue.ResumeLayout(false);
            this.groupBox_PresetValue.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion


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

		private void radioButton_PresetValue_Dec_CheckedChanged(object sender, System.EventArgs e)
		{
			for (short ChNo = 0; ChNo < 8; ChNo++) 
			{
				m_Preset[ChNo].Text = System.Convert.ToString(PresetData[ChNo]);
			}
		}

		private void radioButton_PresetValue_Hex_CheckedChanged(object sender, System.EventArgs e)
		{
			for (short ChNo = 0; ChNo < 8; ChNo++) 
			{
				m_Preset[ChNo].Text = System.Convert.ToString(PresetData[ChNo], 16);
			}		
		}

		private void button_CntInit_Click(object sender, System.EventArgs e)
		{
			// Initialization handling
			int	Ret;

			Ret = cnt.Init(textBox_DeviceName.Text, out m_Id);

			string	ErrorString;
			cnt.GetErrorString(Ret, out ErrorString);
			textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
			if (Ret != 0)
			{
				return;
			}
			//-----------------------------
			// Get all channel number
			//-----------------------------
			Ret = cnt.GetMaxChannels(Counter.m_Id, out m_ChannelNum);
			
			cnt.GetErrorString(Ret, out ErrorString);
			textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
			if (Ret != 0)
			{
				m_ChannelNum = 0;
				return;
			}
			// This sample must not exceed 8 ports
			if (m_ChannelNum > 8)
			{
				m_ChannelNum = 8;
			}
			//-----------------------------
			// Counter, Status, Preset
			// Exprocess invalidate
			//-----------------------------
			for (short ChNo = m_ChannelNum; ChNo < 8; ChNo++) 
			{
				m_CH[ChNo].Enabled = false;	
				m_Status[ChNo].Enabled = false;
				m_Preset[ChNo].Enabled = false;
			}

			MatchCount = 0;
		}

		private void button_ModeSet_Click(object sender, System.EventArgs e)
		{
			//---------------------------------------------
			// CntMode dialog open
			//---------------------------------------------
			Mode	ModeDlg = new Mode();
			ModeDlg.ShowDialog();
			ModeDlg.Dispose();
		}

		private void button_ComparisonSet_Click(object sender, System.EventArgs e)
		{
			//---------------------------------------------
			// Comparison Set dialog open
			//---------------------------------------------
			Comp	ComparisonSetDlg = new Comp();
			ComparisonSetDlg.ShowDialog();
			ComparisonSetDlg.Dispose();
		}

		private void button_CntStartCount_Click(object sender, System.EventArgs e)
		{
			short[] ChNoArray = new short[8];
			int Ret;
			string	ErrorString;

			//-----------------------------
			// Set parameter
			//-----------------------------
			for (short ChNo = 0; ChNo < m_ChannelNum; ChNo++) 
			{
				ChNoArray[ChNo] = ChNo;
			}
			//-----------------------------
			// Counter start
			//-----------------------------
			Ret = cnt.StartCount(m_Id, ChNoArray, m_ChannelNum);
			//-----------------------------
			// Error process
			//-----------------------------
			cnt.GetErrorString(Ret, out ErrorString);
			textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
			if (Ret != (int)CcntConst.CNT_ERR_SUCCESS) 
			{
				return;
			}
			//-----------------------------
			// Timer start
			//-----------------------------
			timer1.Start();
		}

		private void button_CntPreset_Click(object sender, System.EventArgs e)
		{
			short[] ChNoArray = new short[8];
			int Ret;
			string	ErrorString;

			//-----------------------------
			// Set parameter
			//-----------------------------
			for (short ChNo = 0; ChNo < m_ChannelNum; ChNo++) 
			{
				ChNoArray[ChNo] = ChNo;
				
				try 
				{
					if (radioButton_PresetValue_Dec.Checked == true) 
					{
						PresetData[ChNo] = System.Convert.ToUInt32(m_Preset[ChNo].Text);
					} 
					else 
					{
						PresetData[ChNo] = System.Convert.ToUInt32(m_Preset[ChNo].Text, 16);
					}
				} 
				catch
				{
					PresetData[ChNo] = 0;
				}
			}
			//-----------------------------
			// Set Preset Data
			//-----------------------------
			Ret = cnt.Preset(m_Id, ChNoArray, m_ChannelNum, PresetData);
			//-----------------------------
			// Error process
			//-----------------------------
			cnt.GetErrorString(Ret, out ErrorString);
			textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
			if (Ret != (int)CcntConst.CNT_ERR_SUCCESS) 
			{
				return;
			}
		}

		private void button_CntStopCount_Click(object sender, System.EventArgs e)
		{
			short[] ChNoArray = new short[8];
			int Ret;
			string	ErrorString;

			//-----------------------------
			// Set parameter
			//-----------------------------
			for (short ChNo = 0; ChNo < m_ChannelNum; ChNo++) 
			{
				ChNoArray[ChNo] = ChNo;
			}
			//-----------------------------
			// Counter stop
			//-----------------------------
			Ret = cnt.StopCount(m_Id, ChNoArray, m_ChannelNum);
			//-----------------------------
			// Error process
			//-----------------------------
			cnt.GetErrorString(Ret, out ErrorString);
			textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
			if (Ret != (int)CcntConst.CNT_ERR_SUCCESS) 
			{
				return;
			}
			//-----------------------------
			// Timer stop
			//-----------------------------
			timer1.Stop();
		}

		private void button_CntExit_Click(object sender, System.EventArgs e)
		{
			int	Ret;
			string	ErrorString;

			Ret = cnt.Exit(m_Id);

			cnt.GetErrorString(Ret, out ErrorString);
			textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;		
		}

		private void button_Exit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void timer1_Tick(object sender, System.EventArgs e)
		{
			short[] ChNoArray = new short[8];
			uint[] CountValue = new uint[8];
			int		Ret;
			short	Sts;

			//-----------------------------
			// Set parameter
			//-----------------------------
			for (short ChNo = 0; ChNo < m_ChannelNum; ChNo++) 
			{
				ChNoArray[ChNo] = ChNo;
			}
			//-----------------------------
			// Get count value
			//-----------------------------
			Ret = cnt.ReadCount(m_Id, ChNoArray, m_ChannelNum, CountValue);
			if (Ret == (int)CcntConst.CNT_ERR_SUCCESS)
			{
				for (short ChNo = 0; ChNo < m_ChannelNum; ChNo++) 
				{
					if (radioButtonCountValue_Dec.Checked == true) 
					{
						m_CH[ChNo].Text = System.Convert.ToString(CountValue[ChNo]);
					} 
					else 
					{
						m_CH[ChNo].Text = System.Convert.ToString(CountValue[ChNo], 16);
					}
				}
			}
			//-----------------------------
			// Get status
			//-----------------------------
			for (short ChNo = 0; ChNo < m_ChannelNum; ChNo++) 
			{
				Ret = cnt.ReadStatus(m_Id, ChNo, out Sts);
				if (Ret == (int)CcntConst.CNT_ERR_SUCCESS)
				{
					m_Status[ChNo].Text = System.Convert.ToString(Sts, 16);
				}
			}
		}

		protected override void WndProc(ref Message m)
		{
			switch (m.Msg)
			{
				case((int)CcntConst.CNTM_COUNTUP_CH0):
				case((int)CcntConst.CNTM_COUNTUP_CH1):
				case((int)CcntConst.CNTM_COUNTUP_CH2):
				case((int)CcntConst.CNTM_COUNTUP_CH3):
				case((int)CcntConst.CNTM_COUNTUP_CH4):
				case((int)CcntConst.CNTM_COUNTUP_CH5):
				case((int)CcntConst.CNTM_COUNTUP_CH6):
				case((int)CcntConst.CNTM_COUNTUP_CH7):
					MatchCount++;
					if (((m.WParam.ToInt32() & 0x0ffff0000) >> 16) == (int)CcntConst.CNT_DIR_UP) 
					{
						textBox_Comparison_result.Text = 
							"Count-match  !! CH" + System.Convert.ToString(m.Msg & 0x07) + 
							" Value = " + Convert.ToUInt32(m.LParam.ToInt64() & 0x00000000ffffffff) +
							" UP Match Count = " + System.Convert.ToString(MatchCount);
					} 
					else 
					{
						textBox_Comparison_result.Text = 
							"Count-match  !! CH" + System.Convert.ToString(m.Msg & 0x07) + 
							" Value = " + Convert.ToUInt32(m.LParam.ToInt64() & 0x00000000ffffffff) +
							" DOWN Match Count = " + System.Convert.ToString(MatchCount);
					}
					break;
			}
			base.WndProc(ref m);
		}
	}
}
