﻿// High-performance Counter Sample
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
// Counter Header
using CcntCs;

namespace Counter
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class frmCounter : System.Windows.Forms.Form
    {
        public System.Windows.Forms.GroupBox Frame2;
        public System.Windows.Forms.Label lblNotify;
        public System.Windows.Forms.Button btnCntExit;
        public System.Windows.Forms.Button btnCntInit;
        public System.Windows.Forms.Button btnCntMode;
        public System.Windows.Forms.Button btnCntNotify;
        public System.Windows.Forms.GroupBox Frame1;
        public System.Windows.Forms.TextBox txtCountStatus3;
        public System.Windows.Forms.TextBox txtCountStatus2;
        public System.Windows.Forms.TextBox txtCountStatus1;
        public System.Windows.Forms.TextBox txtCountStatus0;
        public System.Windows.Forms.TextBox txtCountValue0;
        public System.Windows.Forms.TextBox txtCountValue3;
        public System.Windows.Forms.TextBox txtCountValue2;
        public System.Windows.Forms.TextBox txtCountValue1;
        public System.Windows.Forms.Label Label2;
        public System.Windows.Forms.Label Label5;
        public System.Windows.Forms.Label Label4;
        public System.Windows.Forms.Label Label3;
        public System.Windows.Forms.Label Label6;
        public System.Windows.Forms.TextBox txtRet;
        public System.Windows.Forms.Button btnCntStop;
        public System.Windows.Forms.Button btnCntReset;
        public System.Windows.Forms.Button btnCntPreset;
        public System.Windows.Forms.Button btnCntStart;
        public System.Windows.Forms.TextBox txtDeviceName;
        public System.Windows.Forms.Label Label1;
        public System.Windows.Forms.Button btnExit;
        public System.Windows.Forms.Button btnCntEvent;
        private IContainer components;

        public static short                     Id;                                  // Device ID
        public static short                     ChannelNum;                          // Total number of channels
        uint[]                                  CountMatch = new uint[CHANNEL_NUM];  // Count match number
        public const int                        CHANNEL_NUM = 4;
        public static IntPtr                    hMainWnd;
        const int                               WM_NOTIFY_COUNTUP = 0x0500;
        const int                               WM_NOTIFY_ERROR = 0x0501;
        const int                               WM_NOTIFY_CARRY = 0x0502;
        private Timer                           timer1;
        Ccnt                                    cnt = new Ccnt();
        private System.Windows.Forms.TextBox[]  txt_Count = new System.Windows.Forms.TextBox[CHANNEL_NUM];    // Count value control
        private System.Windows.Forms.TextBox[]  txt_Status = new System.Windows.Forms.TextBox[CHANNEL_NUM];   // Count status control

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

            //-----------------------------------
            // Control array settings
            //-----------------------------------
            //------------------
            // Count value control
            //------------------
            txt_Count[0] = txtCountValue0;
            txt_Count[1] = txtCountValue1;
            txt_Count[2] = txtCountValue2;
            txt_Count[3] = txtCountValue3;
            //------------------
            // Count status control
            //------------------
            txt_Status[0] = txtCountStatus0;
            txt_Status[1] = txtCountStatus1;
            txt_Status[2] = txtCountStatus2;
            txt_Status[3] = txtCountStatus3;
        }

        /// <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.Frame2 = new System.Windows.Forms.GroupBox();
            this.lblNotify = new System.Windows.Forms.Label();
            this.btnCntExit = new System.Windows.Forms.Button();
            this.btnCntInit = new System.Windows.Forms.Button();
            this.btnCntMode = new System.Windows.Forms.Button();
            this.btnCntNotify = new System.Windows.Forms.Button();
            this.Frame1 = new System.Windows.Forms.GroupBox();
            this.txtCountStatus3 = new System.Windows.Forms.TextBox();
            this.txtCountStatus2 = new System.Windows.Forms.TextBox();
            this.txtCountStatus1 = new System.Windows.Forms.TextBox();
            this.txtCountStatus0 = new System.Windows.Forms.TextBox();
            this.txtCountValue0 = new System.Windows.Forms.TextBox();
            this.txtCountValue3 = new System.Windows.Forms.TextBox();
            this.txtCountValue2 = new System.Windows.Forms.TextBox();
            this.txtCountValue1 = new System.Windows.Forms.TextBox();
            this.Label2 = new System.Windows.Forms.Label();
            this.Label5 = new System.Windows.Forms.Label();
            this.Label4 = new System.Windows.Forms.Label();
            this.Label3 = new System.Windows.Forms.Label();
            this.Label6 = new System.Windows.Forms.Label();
            this.txtRet = new System.Windows.Forms.TextBox();
            this.btnCntStop = new System.Windows.Forms.Button();
            this.btnCntReset = new System.Windows.Forms.Button();
            this.btnCntPreset = new System.Windows.Forms.Button();
            this.btnCntStart = new System.Windows.Forms.Button();
            this.txtDeviceName = new System.Windows.Forms.TextBox();
            this.Label1 = new System.Windows.Forms.Label();
            this.btnExit = new System.Windows.Forms.Button();
            this.btnCntEvent = new System.Windows.Forms.Button();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.Frame2.SuspendLayout();
            this.Frame1.SuspendLayout();
            this.SuspendLayout();
            // 
            // Frame2
            // 
            this.Frame2.BackColor = System.Drawing.SystemColors.Control;
            this.Frame2.Controls.Add(this.lblNotify);
            this.Frame2.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Frame2.Location = new System.Drawing.Point(13, 215);
            this.Frame2.Name = "Frame2";
            this.Frame2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Frame2.Size = new System.Drawing.Size(273, 150);
            this.Frame2.TabIndex = 68;
            this.Frame2.TabStop = false;
            this.Frame2.Text = "Notification:";
            // 
            // lblNotify
            // 
            this.lblNotify.BackColor = System.Drawing.SystemColors.Control;
            this.lblNotify.Cursor = System.Windows.Forms.Cursors.Default;
            this.lblNotify.ForeColor = System.Drawing.SystemColors.ControlText;
            this.lblNotify.Location = new System.Drawing.Point(8, 28);
            this.lblNotify.Name = "lblNotify";
            this.lblNotify.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.lblNotify.Size = new System.Drawing.Size(249, 103);
            this.lblNotify.TabIndex = 20;
            this.lblNotify.Text = "Notification:";
            // 
            // btnCntExit
            // 
            this.btnCntExit.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntExit.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntExit.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntExit.Location = new System.Drawing.Point(306, 336);
            this.btnCntExit.Name = "btnCntExit";
            this.btnCntExit.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntExit.Size = new System.Drawing.Size(105, 28);
            this.btnCntExit.TabIndex = 65;
            this.btnCntExit.Text = "CntExit";
            this.btnCntExit.UseVisualStyleBackColor = false;
            this.btnCntExit.Click += new System.EventHandler(this.btnCntExit_Click);
            // 
            // btnCntInit
            // 
            this.btnCntInit.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntInit.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntInit.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntInit.Location = new System.Drawing.Point(306, 11);
            this.btnCntInit.Name = "btnCntInit";
            this.btnCntInit.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntInit.Size = new System.Drawing.Size(105, 28);
            this.btnCntInit.TabIndex = 55;
            this.btnCntInit.Text = "CntInit";
            this.btnCntInit.UseVisualStyleBackColor = false;
            this.btnCntInit.Click += new System.EventHandler(this.btnCntInit_Click);
            // 
            // btnCntMode
            // 
            this.btnCntMode.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntMode.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntMode.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntMode.Location = new System.Drawing.Point(306, 51);
            this.btnCntMode.Name = "btnCntMode";
            this.btnCntMode.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntMode.Size = new System.Drawing.Size(105, 28);
            this.btnCntMode.TabIndex = 58;
            this.btnCntMode.Text = "Count Mode...";
            this.btnCntMode.UseVisualStyleBackColor = false;
            this.btnCntMode.Click += new System.EventHandler(this.btnCntMode_Click);
            // 
            // btnCntNotify
            // 
            this.btnCntNotify.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntNotify.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntNotify.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntNotify.Location = new System.Drawing.Point(306, 92);
            this.btnCntNotify.Name = "btnCntNotify";
            this.btnCntNotify.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntNotify.Size = new System.Drawing.Size(105, 28);
            this.btnCntNotify.TabIndex = 59;
            this.btnCntNotify.Text = "Notification...";
            this.btnCntNotify.UseVisualStyleBackColor = false;
            this.btnCntNotify.Click += new System.EventHandler(this.btnCntNotify_Click);
            // 
            // Frame1
            // 
            this.Frame1.BackColor = System.Drawing.SystemColors.Control;
            this.Frame1.Controls.Add(this.txtCountStatus3);
            this.Frame1.Controls.Add(this.txtCountStatus2);
            this.Frame1.Controls.Add(this.txtCountStatus1);
            this.Frame1.Controls.Add(this.txtCountStatus0);
            this.Frame1.Controls.Add(this.txtCountValue0);
            this.Frame1.Controls.Add(this.txtCountValue3);
            this.Frame1.Controls.Add(this.txtCountValue2);
            this.Frame1.Controls.Add(this.txtCountValue1);
            this.Frame1.Controls.Add(this.Label2);
            this.Frame1.Controls.Add(this.Label5);
            this.Frame1.Controls.Add(this.Label4);
            this.Frame1.Controls.Add(this.Label3);
            this.Frame1.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Frame1.Location = new System.Drawing.Point(13, 57);
            this.Frame1.Name = "Frame1";
            this.Frame1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Frame1.Size = new System.Drawing.Size(273, 150);
            this.Frame1.TabIndex = 67;
            this.Frame1.TabStop = false;
            this.Frame1.Text = "Count Value/Status:";
            // 
            // txtCountStatus3
            // 
            this.txtCountStatus3.AcceptsReturn = true;
            this.txtCountStatus3.BackColor = System.Drawing.SystemColors.Window;
            this.txtCountStatus3.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtCountStatus3.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtCountStatus3.Location = new System.Drawing.Point(168, 112);
            this.txtCountStatus3.MaxLength = 0;
            this.txtCountStatus3.Name = "txtCountStatus3";
            this.txtCountStatus3.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountStatus3.Size = new System.Drawing.Size(88, 20);
            this.txtCountStatus3.TabIndex = 28;
            // 
            // txtCountStatus2
            // 
            this.txtCountStatus2.AcceptsReturn = true;
            this.txtCountStatus2.BackColor = System.Drawing.SystemColors.Window;
            this.txtCountStatus2.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtCountStatus2.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtCountStatus2.Location = new System.Drawing.Point(168, 84);
            this.txtCountStatus2.MaxLength = 0;
            this.txtCountStatus2.Name = "txtCountStatus2";
            this.txtCountStatus2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountStatus2.Size = new System.Drawing.Size(88, 20);
            this.txtCountStatus2.TabIndex = 27;
            // 
            // txtCountStatus1
            // 
            this.txtCountStatus1.AcceptsReturn = true;
            this.txtCountStatus1.BackColor = System.Drawing.SystemColors.Window;
            this.txtCountStatus1.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtCountStatus1.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtCountStatus1.Location = new System.Drawing.Point(168, 56);
            this.txtCountStatus1.MaxLength = 0;
            this.txtCountStatus1.Name = "txtCountStatus1";
            this.txtCountStatus1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountStatus1.Size = new System.Drawing.Size(88, 20);
            this.txtCountStatus1.TabIndex = 26;
            // 
            // txtCountStatus0
            // 
            this.txtCountStatus0.AcceptsReturn = true;
            this.txtCountStatus0.BackColor = System.Drawing.SystemColors.Window;
            this.txtCountStatus0.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtCountStatus0.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtCountStatus0.Location = new System.Drawing.Point(168, 28);
            this.txtCountStatus0.MaxLength = 0;
            this.txtCountStatus0.Name = "txtCountStatus0";
            this.txtCountStatus0.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountStatus0.Size = new System.Drawing.Size(88, 20);
            this.txtCountStatus0.TabIndex = 25;
            // 
            // txtCountValue0
            // 
            this.txtCountValue0.AcceptsReturn = true;
            this.txtCountValue0.BackColor = System.Drawing.SystemColors.Window;
            this.txtCountValue0.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtCountValue0.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtCountValue0.Location = new System.Drawing.Point(56, 28);
            this.txtCountValue0.MaxLength = 0;
            this.txtCountValue0.Name = "txtCountValue0";
            this.txtCountValue0.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountValue0.Size = new System.Drawing.Size(88, 20);
            this.txtCountValue0.TabIndex = 24;
            // 
            // txtCountValue3
            // 
            this.txtCountValue3.AcceptsReturn = true;
            this.txtCountValue3.BackColor = System.Drawing.SystemColors.Window;
            this.txtCountValue3.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtCountValue3.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtCountValue3.Location = new System.Drawing.Point(56, 112);
            this.txtCountValue3.MaxLength = 0;
            this.txtCountValue3.Name = "txtCountValue3";
            this.txtCountValue3.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountValue3.Size = new System.Drawing.Size(88, 20);
            this.txtCountValue3.TabIndex = 18;
            // 
            // txtCountValue2
            // 
            this.txtCountValue2.AcceptsReturn = true;
            this.txtCountValue2.BackColor = System.Drawing.SystemColors.Window;
            this.txtCountValue2.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtCountValue2.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtCountValue2.Location = new System.Drawing.Point(56, 84);
            this.txtCountValue2.MaxLength = 0;
            this.txtCountValue2.Name = "txtCountValue2";
            this.txtCountValue2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountValue2.Size = new System.Drawing.Size(88, 20);
            this.txtCountValue2.TabIndex = 17;
            // 
            // txtCountValue1
            // 
            this.txtCountValue1.AcceptsReturn = true;
            this.txtCountValue1.BackColor = System.Drawing.SystemColors.Window;
            this.txtCountValue1.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtCountValue1.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtCountValue1.Location = new System.Drawing.Point(56, 56);
            this.txtCountValue1.MaxLength = 0;
            this.txtCountValue1.Name = "txtCountValue1";
            this.txtCountValue1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountValue1.Size = new System.Drawing.Size(88, 20);
            this.txtCountValue1.TabIndex = 16;
            // 
            // Label2
            // 
            this.Label2.AutoSize = true;
            this.Label2.BackColor = System.Drawing.SystemColors.Control;
            this.Label2.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label2.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label2.Location = new System.Drawing.Point(8, 34);
            this.Label2.Name = "Label2";
            this.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label2.Size = new System.Drawing.Size(31, 13);
            this.Label2.TabIndex = 23;
            this.Label2.Text = "CH0:";
            // 
            // Label5
            // 
            this.Label5.AutoSize = true;
            this.Label5.BackColor = System.Drawing.SystemColors.Control;
            this.Label5.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label5.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label5.Location = new System.Drawing.Point(8, 117);
            this.Label5.Name = "Label5";
            this.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label5.Size = new System.Drawing.Size(31, 13);
            this.Label5.TabIndex = 15;
            this.Label5.Text = "CH3:";
            // 
            // Label4
            // 
            this.Label4.AutoSize = true;
            this.Label4.BackColor = System.Drawing.SystemColors.Control;
            this.Label4.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label4.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label4.Location = new System.Drawing.Point(8, 90);
            this.Label4.Name = "Label4";
            this.Label4.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label4.Size = new System.Drawing.Size(31, 13);
            this.Label4.TabIndex = 14;
            this.Label4.Text = "CH2:";
            // 
            // Label3
            // 
            this.Label3.AutoSize = true;
            this.Label3.BackColor = System.Drawing.SystemColors.Control;
            this.Label3.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label3.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label3.Location = new System.Drawing.Point(8, 62);
            this.Label3.Name = "Label3";
            this.Label3.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label3.Size = new System.Drawing.Size(31, 13);
            this.Label3.TabIndex = 13;
            this.Label3.Text = "CH1:";
            // 
            // Label6
            // 
            this.Label6.AutoSize = true;
            this.Label6.BackColor = System.Drawing.SystemColors.Control;
            this.Label6.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label6.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label6.Location = new System.Drawing.Point(13, 384);
            this.Label6.Name = "Label6";
            this.Label6.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label6.Size = new System.Drawing.Size(72, 13);
            this.Label6.TabIndex = 69;
            this.Label6.Text = "Return Value:";
            // 
            // txtRet
            // 
            this.txtRet.AcceptsReturn = true;
            this.txtRet.BackColor = System.Drawing.SystemColors.Window;
            this.txtRet.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtRet.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtRet.Location = new System.Drawing.Point(88, 378);
            this.txtRet.MaxLength = 0;
            this.txtRet.Name = "txtRet";
            this.txtRet.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtRet.Size = new System.Drawing.Size(200, 20);
            this.txtRet.TabIndex = 70;
            // 
            // btnCntStop
            // 
            this.btnCntStop.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntStop.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntStop.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntStop.Location = new System.Drawing.Point(306, 296);
            this.btnCntStop.Name = "btnCntStop";
            this.btnCntStop.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntStop.Size = new System.Drawing.Size(105, 28);
            this.btnCntStop.TabIndex = 64;
            this.btnCntStop.Text = "CntStop";
            this.btnCntStop.UseVisualStyleBackColor = false;
            this.btnCntStop.Click += new System.EventHandler(this.btnCntStop_Click);
            // 
            // btnCntReset
            // 
            this.btnCntReset.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntReset.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntReset.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntReset.Location = new System.Drawing.Point(306, 255);
            this.btnCntReset.Name = "btnCntReset";
            this.btnCntReset.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntReset.Size = new System.Drawing.Size(105, 28);
            this.btnCntReset.TabIndex = 63;
            this.btnCntReset.Text = "CntReset";
            this.btnCntReset.UseVisualStyleBackColor = false;
            this.btnCntReset.Click += new System.EventHandler(this.btnCntReset_Click);
            // 
            // btnCntPreset
            // 
            this.btnCntPreset.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntPreset.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntPreset.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntPreset.Location = new System.Drawing.Point(306, 214);
            this.btnCntPreset.Name = "btnCntPreset";
            this.btnCntPreset.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntPreset.Size = new System.Drawing.Size(105, 28);
            this.btnCntPreset.TabIndex = 62;
            this.btnCntPreset.Text = "CntPreset";
            this.btnCntPreset.UseVisualStyleBackColor = false;
            this.btnCntPreset.Click += new System.EventHandler(this.btnCntPreset_Click);
            // 
            // btnCntStart
            // 
            this.btnCntStart.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntStart.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntStart.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntStart.Location = new System.Drawing.Point(306, 174);
            this.btnCntStart.Name = "btnCntStart";
            this.btnCntStart.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntStart.Size = new System.Drawing.Size(105, 27);
            this.btnCntStart.TabIndex = 61;
            this.btnCntStart.Text = "CntStart";
            this.btnCntStart.UseVisualStyleBackColor = false;
            this.btnCntStart.Click += new System.EventHandler(this.btnCntStart_Click);
            // 
            // txtDeviceName
            // 
            this.txtDeviceName.AcceptsReturn = true;
            this.txtDeviceName.BackColor = System.Drawing.SystemColors.Window;
            this.txtDeviceName.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtDeviceName.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtDeviceName.Location = new System.Drawing.Point(96, 12);
            this.txtDeviceName.MaxLength = 0;
            this.txtDeviceName.Name = "txtDeviceName";
            this.txtDeviceName.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtDeviceName.Size = new System.Drawing.Size(113, 20);
            this.txtDeviceName.TabIndex = 57;
            // 
            // Label1
            // 
            this.Label1.AutoSize = true;
            this.Label1.BackColor = System.Drawing.SystemColors.Control;
            this.Label1.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label1.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label1.Location = new System.Drawing.Point(13, 18);
            this.Label1.Name = "Label1";
            this.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label1.Size = new System.Drawing.Size(75, 13);
            this.Label1.TabIndex = 56;
            this.Label1.Text = "Device Name:";
            // 
            // btnExit
            // 
            this.btnExit.BackColor = System.Drawing.SystemColors.Control;
            this.btnExit.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnExit.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnExit.Location = new System.Drawing.Point(306, 377);
            this.btnExit.Name = "btnExit";
            this.btnExit.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnExit.Size = new System.Drawing.Size(105, 28);
            this.btnExit.TabIndex = 66;
            this.btnExit.Text = "Exit";
            this.btnExit.UseVisualStyleBackColor = false;
            this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
            // 
            // btnCntEvent
            // 
            this.btnCntEvent.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntEvent.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntEvent.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntEvent.Location = new System.Drawing.Point(306, 133);
            this.btnCntEvent.Name = "btnCntEvent";
            this.btnCntEvent.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntEvent.Size = new System.Drawing.Size(105, 28);
            this.btnCntEvent.TabIndex = 60;
            this.btnCntEvent.Text = "Event...";
            this.btnCntEvent.UseVisualStyleBackColor = false;
            this.btnCntEvent.Click += new System.EventHandler(this.btnCntEvent_Click);
            // 
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // frmCounter
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(424, 416);
            this.Controls.Add(this.Frame2);
            this.Controls.Add(this.btnCntExit);
            this.Controls.Add(this.btnCntInit);
            this.Controls.Add(this.btnCntMode);
            this.Controls.Add(this.btnCntNotify);
            this.Controls.Add(this.Frame1);
            this.Controls.Add(this.Label6);
            this.Controls.Add(this.txtRet);
            this.Controls.Add(this.btnCntStop);
            this.Controls.Add(this.btnCntReset);
            this.Controls.Add(this.btnCntPreset);
            this.Controls.Add(this.btnCntStart);
            this.Controls.Add(this.txtDeviceName);
            this.Controls.Add(this.Label1);
            this.Controls.Add(this.btnExit);
            this.Controls.Add(this.btnCntEvent);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmCounter";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Counter";
            this.Load += new System.EventHandler(this.frmCounter_Load);
            this.Frame2.ResumeLayout(false);
            this.Frame1.ResumeLayout(false);
            this.Frame1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

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

        //===================================================
        // Dialog initialization
        //===================================================
        private void frmCounter_Load(object sender, System.EventArgs e)
        {
            short   ch;

            //-----------------------------------
            // Initialize dialog display
            //-----------------------------------
            txtDeviceName.Text = "CNT000";
            //-----------------------------------
            // For each channel
            //-----------------------------------
            for(ch = 0; ch < CHANNEL_NUM; ch++){
                //------------------
                // Count value control
                //------------------
                txt_Count[ch].Text = "";
                //------------------
                // Count status control
                //------------------
                txt_Status[ch].Text = "";
            }
            //-----------------------------------
            // Initializing Parameters
            //-----------------------------------
            ChannelNum = CHANNEL_NUM;
            //-----------------------------------
            // Copy window's handle
            //-----------------------------------
            hMainWnd = this.Handle;
        }

        //---------------------------------------------
        // CntInit()
        //---------------------------------------------
        private void btnCntInit_Click(object sender, System.EventArgs e)
        {
            string    str;
            string    buf = new string(' ', 100);
            string    error_string = new string(' ', 256);
            int       ret;
            short     i;

            //-----------------------------------
            // Get device name
            //-----------------------------------
            buf = txtDeviceName.Text;

            //------------------------------
            // Initialization
            //------------------------------
            ret = cnt.Init(buf, out Id);
            cnt.GetErrorString(ret, out error_string);
            str = string.Format("CntInit ret = {0:d}: {1}", ret, error_string);
            txtRet.Text = str;
            
            if(ret != 0){
                return;
            }
            //-----------------------------
            // Get number of channels
            //-----------------------------
            cnt.GetMaxChannels(Id, out ChannelNum);
            //-----------------------------
            // If the sample control count is exceeded, 
            // the total channel count is corrected to the control count.
            //-----------------------------
            if(ChannelNum > CHANNEL_NUM){
                ChannelNum = CHANNEL_NUM;
            }
            //-----------------------------
            // Toggles the count value and status display according to the total number of channels.
            //-----------------------------
            for(i = 0; i < CHANNEL_NUM; i++){
                //-----------------------------
                // Disables the display when the number of loops exceeds the total number of channels.
                //-----------------------------
                if(i >= ChannelNum){
                    txt_Count[i].Enabled = false;
                    txt_Status[i].Enabled = false;
                //-----------------------------
                // Enables the display when the number of loops is less than or equal to the total number of channels.
                //-----------------------------
                }else{
                    txt_Count[i].Enabled = true;
                    txt_Status[i].Enabled = true;
                }
            }
        }

        //---------------------------------------------
        // Display Counter Mode Setting dialog
        //---------------------------------------------
        private void btnCntMode_Click(object sender, System.EventArgs e)
        {
            //------------------------------
            // Display Counter Mode Setting dialog
            //------------------------------
            frmMode    frm = new frmMode();
            frm.ShowDialog();
            frm.Dispose();
        }

        //---------------------------------------------
        // Display Notification Setting dialog
        //---------------------------------------------
        private void btnCntNotify_Click(object sender, System.EventArgs e)
        {
            //------------------------------
            // Display Notification Setting dialog
            //------------------------------
            frmNotify frm = new frmNotify();
            frm.ShowDialog();
            frm.Dispose();
        }

        //---------------------------------------------
        // Display Event Setting dialog
        //---------------------------------------------
        private void btnCntEvent_Click(object sender, System.EventArgs e)
        {
            //------------------------------
            // Display Event Setting dialog
            //------------------------------
            frmEvent frm = new frmEvent();
            frm.ShowDialog();
            frm.Dispose();
        }

        //---------------------------------------------
        // Start count
        //---------------------------------------------
        private void btnCntStart_Click(object sender, System.EventArgs e)
        {
            string  str;
            string  error_string = new string(' ', 256);
            int     ret;
            short[] ChNo = new short[CHANNEL_NUM];
            short   i;
            //------------------------------
            // Set parameter
            //------------------------------
            for (i=0; i<ChannelNum; i++) 
            {
                ChNo[i] = i;
            }

            //------------------------------
            // Start count
            //------------------------------
            ret = cnt.StartCount(Id, ChNo, ChannelNum);
            cnt.GetErrorString(ret, out error_string);
            str = string.Format("CntStartCount ret = {0:d}: {1}", ret, error_string);
            txtRet.Text = str;
            if (ret == 0) 
            {
                //------------------------------
                // Consider a device that does not have a timer notification function,
                // we use the window timer.
                // Please switch and use, according to the use.
                //------------------------------
                //------------------------------
                // Start timer
                //------------------------------
                timer1.Start();
                //------------------------------
                // When using the timer notification event of device,
                // please use the following function.
                //------------------------------
                //cnt.NotifyTimer(Id, 100, hMainWnd);
            }
        }

        //---------------------------------------------
        // Preset counter
        //---------------------------------------------
        private void btnCntPreset_Click(object sender, System.EventArgs e)
        {
            string    str;
            string    error_string = new string(' ', 256);
            int       ret;
            short[]   ChNo = new short[CHANNEL_NUM];
            uint[]    PresetData = new uint[CHANNEL_NUM];
            short     i;

            //------------------------------
            // Set preset value
            //------------------------------
            for (i=0; i<ChannelNum; i++) 
            {
                ChNo[i] = i;
                PresetData[i] = (uint)i * 0x100;
            }

            //------------------------------
            // Set preset
            //------------------------------
            ret = cnt.Preset(Id, ChNo, ChannelNum, PresetData);
            cnt.GetErrorString(ret, out error_string);
            str = string.Format("CntPreset ret = {0:d}: {1}", ret, error_string);
            txtRet.Text = str;
        }

        //---------------------------------------------
        // Reset counter
        //---------------------------------------------
        private void btnCntReset_Click(object sender, System.EventArgs e)
        {
            string    str;
            string    error_string = new string(' ', 256);
            int       ret;
            short[]   ChNo = new short[CHANNEL_NUM];
            short     i;

            //------------------------------
            // Set parameter
            //------------------------------
            for (i=0; i<ChannelNum; i++) 
            {
                ChNo[i] = i;
            }

            //------------------------------
            // Reset counter
            //------------------------------
            ret = cnt.ZeroClearCount(Id, ChNo, ChannelNum);
            cnt.GetErrorString(ret, out error_string);
            str = string.Format("CntZeroClearCount ret = {0:d}: {1}", ret, error_string);
            txtRet.Text = str;
        }

        //---------------------------------------------
        // Stop count
        //---------------------------------------------
        private void btnCntStop_Click(object sender, System.EventArgs e)
        {
            string    str;
            string    error_string = new string(' ', 256);
            int       ret;
            short[]   ChNo = new short[CHANNEL_NUM];
            short     i;

            //------------------------------
            // Set parameter
            //------------------------------
            for (i=0; i<ChannelNum; i++) 
            {
                ChNo[i] = i;
            }

            //------------------------------
            // Stop counter
            //------------------------------
            ret = cnt.StopCount(Id, ChNo, ChannelNum);
            cnt.GetErrorString(ret, out error_string);
            str = string.Format("CntStopCount ret = {0:d}: {1}", ret, error_string);
            txtRet.Text = str;

            //------------------------------
            // Consider a device that does not have a timer notification function,
            // we use the window timer.
            // Please switch and use, according to the use.
            //------------------------------
            //------------------------------
            // Stop timer
            //------------------------------
            timer1.Stop();
            //------------------------------
            // When using the timer notification event of device,
            // please use the following function.
            //------------------------------
            //cnt.StopNotifyTimer(Id);
        }

        //---------------------------------------------
        // CntExit()
        //---------------------------------------------
        private void btnCntExit_Click(object sender, System.EventArgs e)
        {
            string    str;
            string    error_string = new string(' ', 256);
            int        ret;

            //------------------------------
            // Consider a device that does not have a timer notification function,
            // we use the window timer.
            // Please switch and use, according to the use.
            //------------------------------
            //------------------------------
            // Stop timer
            //------------------------------
            timer1.Stop();
            //------------------------------
            // When using the timer notification event of device,
            // please use the following function.
            //------------------------------
            //cnt.StopNotifyTimer(Id);

            //------------------------------
            // Counter termination processing
            //------------------------------
            ret = cnt.Exit(Id);
            cnt.GetErrorString(ret, out error_string);
            str = string.Format("CntExit ret = {0:d}: {1}", ret, error_string);
            txtRet.Text = str;
        }

        //---------------------------------------------
        //  Exit sample program
        //---------------------------------------------
        private void btnExit_Click(object sender, System.EventArgs e)
        {
            int    ret;

            //------------------------------
            // Consider a device that does not have a timer notification function,
            // we use the window timer.
            // Please switch and use, according to the use.
            //------------------------------
            //------------------------------
            // Stop timer
            //------------------------------
            timer1.Stop();
            //------------------------------
            // When using the timer notification event of device,
            // please use the following function.
            //------------------------------
            //cnt.StopNotifyTimer(Id);

            //------------------------------
            // Counter termination processing
            //------------------------------
            ret = cnt.Exit(Id);
            this.Close();
        }

        //---------------------------------------------
        // Consider a device that does not have a timer notification function,
        // we use the window timer.
        // When using the timer notification function, please comment out the following method.
        //---------------------------------------------
        private void timer1_Tick(object sender, EventArgs e)
        {
            short[] ChNo = new short[CHANNEL_NUM];
            uint[]  CntData = new uint[CHANNEL_NUM];
            uint[]  StatusData = new uint[CHANNEL_NUM];
            short   i;
            int     ret;
            string  str;
            string  error_string = new string(' ', 256);

            //------------------------------
            // Set parameter
            //------------------------------
            for (i = 0; i < ChannelNum; i++)
            {
                ChNo[i] = i;
                CntData[i] = 0;
                StatusData[i] = 0;
            }
            //------------------------------
            // Get count value
            //------------------------------
            ret = cnt.ReadCount(Id, ChNo, ChannelNum, CntData);
            if (ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                str = string.Format("CntReadCount ret = {0:d}: {1}", ret, error_string);
                txtRet.Text = str;
                //------------------------------
                // Stop timer
                //------------------------------
                timer1.Stop();
            }
            //------------------------------
            // Get count status
            //------------------------------
            for (i = 0; i < ChannelNum; i++)
            {
                ret = cnt.ReadStatusEx(Id, i, out StatusData[i]);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    str = string.Format("CntReadStatus ret = {0:d}: {1}", ret, error_string);
                    txtRet.Text = str;
                    //------------------------------
                    // Stop timer
                    //------------------------------
                    timer1.Stop();
                }
            }
            //------------------------------
            // Display data
            //------------------------------
            for (i = 0; i < ChannelNum; i++)
            {
                str = string.Format("{0:D8}", CntData[i]);
                txt_Count[i].Text = str;
                str = string.Format("{0:X8}H", StatusData[i]);
                txt_Status[i].Text = str;
            }
        }

        //---------------------------------------------
        // Counter event notification processing
        //---------------------------------------------
        protected override void WndProc(ref Message m)
        {
            uint[]            CntData = new uint[CHANNEL_NUM];
            uint[]            StatusData = new uint[CHANNEL_NUM];
            string            str;
            string            error_string=new string(' ', 256);

            switch (m.Msg)
            {                
                    // Count Match
                case (int)CcntConst.CNTM_COUNTUP_CH0:    // Count match: 0CH
                    CountMatch[0]++;
                    if(((m.WParam.ToInt32() & 0x0ffff0000) >> 16) == 1 )
                    {
                        str = string.Format("Event:\nCount Match!\nChannel No.: 0, UP, Count: {0:X8}(Hex)\n Matched: {1:d} times", Convert.ToUInt32(m.LParam.ToInt64() & 0x00000000ffffffff), CountMatch[0]);
                    }
                    else
                    {
                        str = string.Format("Event:\nCount Match!\nChannel No.: 0, DOWN, Count: {0:X8}(Hex)\n Matched: {1:d} times", Convert.ToUInt32(m.LParam.ToInt64() & 0x00000000ffffffff), CountMatch[0]);
                    }
                    lblNotify.Text = str;
                    break;
                    // Counter Error    
                case (int)CcntConst.CNTM_COUNTUP_CH1:    // Count match: 1CH
                    CountMatch[1]++;
                    if(((m.WParam.ToInt32() & 0x0ffff0000) >> 16) == 1 )
                    {
                        str = string.Format("Event:\nCount Match!\nChannel No.: 1, UP, Count: {0:X8}(Hex)\n Matched: {1:d} times", Convert.ToUInt32(m.LParam.ToInt64() & 0x00000000ffffffff), CountMatch[1]);
                    }
                    else
                    {
                        str = string.Format("Event:\nCount Match!\nChannel No.: 1, DOWN, Count: {0:X8}(Hex)\n Matched: {1:d} times", Convert.ToUInt32(m.LParam.ToInt64() & 0x00000000ffffffff), CountMatch[1]);
                    }
                    lblNotify.Text = str;
                    break;
                case (int)CcntConst.CNTM_COUNTUP_CH2:    // Count match: 2CH
                    CountMatch[2]++;
                    if(((m.WParam.ToInt32() & 0x0ffff0000) >> 16) == 1 )
                    {
                        str = string.Format("Event:\nCount Match!\nChannel No.: 2, UP, Count: {0:X8}(Hex)\n Matched: {1:d} times", Convert.ToUInt32(m.LParam.ToInt64() & 0x00000000ffffffff), CountMatch[2]);
                    }
                    else
                    {
                        str = string.Format("Event:\nCount Match!\nChannel No.: 2, DOWN, Count: {0:X8}(Hex)\n Matched: {1:d} times", Convert.ToUInt32(m.LParam.ToInt64() & 0x00000000ffffffff), CountMatch[2]);
                    }
                    lblNotify.Text = str;
                    break;
                case (int)CcntConst.CNTM_COUNTUP_CH3:    // Count match: 3CH
                    CountMatch[3]++;
                    if(((m.WParam.ToInt32() & 0x0ffff0000) >> 16) == 1 )
                    {
                        str = string.Format("Event:\nCount Match!\nChannel No.: 3, UP, Count: {0:X8}(Hex)\n Matched: {1:d} times", Convert.ToUInt32(m.LParam.ToInt64() & 0x00000000ffffffff), CountMatch[3]);
                    }
                    else
                    {
                        str = string.Format("Event:\nCount Match!\nChannel No.: 3, DOWN, Count: {0:X8}(Hex)\n Matched: {1:d} times", Convert.ToUInt32(m.LParam.ToInt64() & 0x00000000ffffffff), CountMatch[3]);
                    }
                    lblNotify.Text = str;
                    break;
                case (int)CcntConst.CNTM_COUNTER_ERROR:    // Counter error
                switch(m.LParam.ToInt32()  & 0x0000ffff)
                {
                    case (int)CcntConst.CNT_AI_ERROR:        // Abnormal input
                        str = string.Format("Event:\nAbnormal input error!\nChannel No.: {0:d}", (m.WParam.ToInt32() & 0x0ffff0000) >> 16);
                        lblNotify.Text = str;
                        break;
                    case (int)CcntConst.CNT_FLT_ERROR:        // Filter
                        str = string.Format("Event:\nFilter error!\nChannel No.: {0:d}", (m.WParam.ToInt32() & 0x0ffff0000) >> 16);
                        lblNotify.Text = str;
                        break;
                    case (int)CcntConst.CNT_ALM_ERROR:        // Alarm
                        str = string.Format("Event:\nAlarm error!\nChannel No.: {0:d}", (m.WParam.ToInt32() & 0x0ffff0000) >> 16);
                        lblNotify.Text = str;
                        break;
                }
                    break;
                case (int)CcntConst.CNTM_CARRY_BORROW:        // Carry/Borrow
                switch(m.LParam.ToInt32()  & 0x0000ffff)
                {
                    case (int)CcntConst.CNT_CB_CARRY:        // Carry
                        str = string.Format("Event:\nCarry!\nChannel No.: {0:d}", (m.WParam.ToInt32() & 0x0ffff0000) >> 16);
                        lblNotify.Text = str;
                        break;
                    case (int)CcntConst.CNT_CB_BORROW:        // Borrow
                        str = string.Format("Event:\nBorrow!\nChannel No.: {0:d}", (m.WParam.ToInt32() & 0x0ffff0000) >> 16);
                        lblNotify.Text = str;
                        break;
                }
                    break;

                /*
                //------------------------------
                // Consider a device that does not have a timer notification function,
                // When using the timer notification event of device,
                // please use the following message.
                //------------------------------
                case (int)CcntConst.CNTM_TIMER:        // Timer notification
                    short[] ChNo = new short[CHANNEL_NUM];
                    short   i;
                    int     ret;
                    //------------------------------
                    // Set parameter
                    //------------------------------
                    for (i=0; i<ChannelNum; i++) 
                    {
                        ChNo[i] = i;
                        CntData[i] = 0;
                        StatusData[i] = 0;
                    }
                    //------------------------------
                    // Get count value
                    //------------------------------
                    ret = cnt.ReadCount(Id, ChNo, ChannelNum, CntData);
                    if (ret != 0) 
                    {
                        cnt.GetErrorString(ret, out error_string);
                        str = string.Format("CntReadCount ret = {0:d}: {1}", ret, error_string);
                        txtRet.Text = str;
                        cnt.StopNotifyTimer(Id);
                    }
                    //------------------------------
                    // Get count status
                    //------------------------------
                    for (i=0; i<ChannelNum; i++) 
                    {
                        ret = cnt.ReadStatusEx(Id, i, out StatusData[i]);
                        if (ret != 0) 
                        {
                            cnt.GetErrorString(ret, out error_string);
                            str = string.Format("CntReadStatus ret = {0:d}: {1}", ret, error_string);
                            txtRet.Text = str;
                            cnt.StopNotifyTimer(Id);
                        }
                    }
                    //------------------------------
                    // Display data
                    //------------------------------
                    for (i = 0; i < ChannelNum; i++)
                    {
                        str = string.Format("{0:D8}", CntData[i]);
                        txt_Count[i].Text = str;
                        str = string.Format("{0:X8}H", StatusData[i]);
                        txt_Status[i].Text = str;
                    }
                    break;
                */
            }
            base.WndProc(ref m);
        }
    }
}
