﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
// カウンタヘッダー
using CcntCs;

namespace CounterCallback
{
    /// <summary>
    /// CounterCallback の概要の説明です。
    /// </summary>
    public class CounterCallback : System.Windows.Forms.Form
    {
        public System.Windows.Forms.Button btnExit;
        public System.Windows.Forms.GroupBox Frame2;
        public System.Windows.Forms.Label lblNotify;
        public System.Windows.Forms.Button btnCntExit;
        public System.Windows.Forms.Button btnCntNotify;
        public System.Windows.Forms.Button btnCntMode;
        public System.Windows.Forms.Button btnCntInit;
        public System.Windows.Forms.Label Label6;
        public System.Windows.Forms.TextBox txtRet;
        public System.Windows.Forms.GroupBox Frame1;
        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.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 btnCntEvent;
        private System.ComponentModel.IContainer components = null;
        private System.Windows.Forms.Timer timer1;

        public static short                     Id;                                    // デバイスID
        public static short                     ChannelNum;                            // 総チャネル数
        uint[]                                  CountMatch = new uint[CHANNEL_NUM];    // カウント一致回数
        public const int                        CHANNEL_NUM = 4;
        const int                               WM_NOTIFY_COUNTUP = 0x0500;
        const int                               WM_NOTIFY_ERROR = 0x0501;
        const int                               WM_NOTIFY_CARRY = 0x0502;
        private System.Windows.Forms.TextBox[]  txt_Count = new System.Windows.Forms.TextBox[CHANNEL_NUM];    // カウント値コントロール
        private System.Windows.Forms.TextBox[]  txt_Status = new System.Windows.Forms.TextBox[CHANNEL_NUM];   // カウントステータスコントロール

        //コールバックルーチンの宣言
        static public  GCHandle                hGC_CountUp;
        static public  GCHandle                hGC_CounterError;
        static public  GCHandle                hGC_CarryBorrow;
        static public  GCHandle                hGC_Timer;
        static public  PCOUNTUPCALLBACK        dele_CountUp;
        static public  PCOUNTERERRORCALLBACK   dele_CounterError;
        static public  PCARRYBORROWCALLBACK    dele_CarryBorrow;
        //------------------------------
        // デバイスのタイマー通知イベントを利用される場合は、
        // 以下の宣言をご使用ください
        //-----------------------------
        //static public    PTIMERCALLBACK  dele_Timer;
        static public      IntPtr          pCountUp;
        static public      IntPtr          pCounterError;
        static public      IntPtr          pCarryBorrow;
        static public      IntPtr          pTimer;
        
        public System.Windows.Forms.TextBox txtCountStatus0;
        public System.Windows.Forms.TextBox txtCountStatus3;
        public System.Windows.Forms.TextBox txtCountStatus2;
        public System.Windows.Forms.TextBox txtCountStatus1;
        
        Ccnt                                cnt = new Ccnt();


        public CounterCallback()
        {
            //
            // Windows フォーム デザイナ サポートに必要です。
            //
            InitializeComponent();

            //-----------------------------------
            // コントロール配列設定
            //-----------------------------------
            //-----------------------------------
            // カウント値コントロール
            //-----------------------------------
            txt_Count[0] = txtCountValue0;
            txt_Count[1] = txtCountValue1;
            txt_Count[2] = txtCountValue2;
            txt_Count[3] = txtCountValue3;
            //-----------------------------------
            // カウントステータスコントロール
            //-----------------------------------
            txt_Status[0] = txtCountStatus0;
            txt_Status[1] = txtCountStatus1;
            txt_Status[2] = txtCountStatus2;
            txt_Status[3] = txtCountStatus3;
        }

        /// <summary>
        /// 使用されているリソースに後処理を実行します。
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディタで変更しないでください。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.btnExit = new System.Windows.Forms.Button();
            this.Frame2 = new System.Windows.Forms.GroupBox();
            this.lblNotify = new System.Windows.Forms.Label();
            this.btnCntExit = new System.Windows.Forms.Button();
            this.btnCntNotify = new System.Windows.Forms.Button();
            this.btnCntMode = new System.Windows.Forms.Button();
            this.btnCntInit = new System.Windows.Forms.Button();
            this.Label6 = new System.Windows.Forms.Label();
            this.txtRet = new System.Windows.Forms.TextBox();
            this.Frame1 = new System.Windows.Forms.GroupBox();
            this.txtCountStatus0 = new System.Windows.Forms.TextBox();
            this.txtCountStatus3 = new System.Windows.Forms.TextBox();
            this.txtCountStatus2 = new System.Windows.Forms.TextBox();
            this.txtCountStatus1 = 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.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.btnCntEvent = new System.Windows.Forms.Button();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.Frame2.SuspendLayout();
            this.Frame1.SuspendLayout();
            this.SuspendLayout();
            // 
            // 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(304, 323);
            this.btnExit.Name = "btnExit";
            this.btnExit.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnExit.Size = new System.Drawing.Size(105, 24);
            this.btnExit.TabIndex = 9;
            this.btnExit.Text = "Exit";
            this.btnExit.UseVisualStyleBackColor = false;
            this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
            // 
            // 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(11, 184);
            this.Frame2.Name = "Frame2";
            this.Frame2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Frame2.Size = new System.Drawing.Size(273, 129);
            this.Frame2.TabIndex = 36;
            this.Frame2.TabStop = false;
            this.Frame2.Text = "通知：";
            // 
            // 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, 24);
            this.lblNotify.Name = "lblNotify";
            this.lblNotify.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.lblNotify.Size = new System.Drawing.Size(249, 89);
            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(304, 288);
            this.btnCntExit.Name = "btnCntExit";
            this.btnCntExit.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntExit.Size = new System.Drawing.Size(105, 24);
            this.btnCntExit.TabIndex = 8;
            this.btnCntExit.Text = "CntExit";
            this.btnCntExit.UseVisualStyleBackColor = false;
            this.btnCntExit.Click += new System.EventHandler(this.btnCntExit_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(304, 78);
            this.btnCntNotify.Name = "btnCntNotify";
            this.btnCntNotify.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntNotify.Size = new System.Drawing.Size(105, 24);
            this.btnCntNotify.TabIndex = 2;
            this.btnCntNotify.Text = "通知...";
            this.btnCntNotify.UseVisualStyleBackColor = false;
            this.btnCntNotify.Click += new System.EventHandler(this.btnCntNotify_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(304, 43);
            this.btnCntMode.Name = "btnCntMode";
            this.btnCntMode.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntMode.Size = new System.Drawing.Size(105, 24);
            this.btnCntMode.TabIndex = 1;
            this.btnCntMode.Text = "カウンタモード...";
            this.btnCntMode.UseVisualStyleBackColor = false;
            this.btnCntMode.Click += new System.EventHandler(this.btnCntMode_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(304, 8);
            this.btnCntInit.Name = "btnCntInit";
            this.btnCntInit.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntInit.Size = new System.Drawing.Size(105, 24);
            this.btnCntInit.TabIndex = 0;
            this.btnCntInit.Text = "CntInit";
            this.btnCntInit.UseVisualStyleBackColor = false;
            this.btnCntInit.Click += new System.EventHandler(this.btnCntInit_Click);
            // 
            // 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(11, 329);
            this.Label6.Name = "Label6";
            this.Label6.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label6.Size = new System.Drawing.Size(43, 12);
            this.Label6.TabIndex = 37;
            this.Label6.Text = "戻り値：";
            // 
            // 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(72, 324);
            this.txtRet.MaxLength = 0;
            this.txtRet.Name = "txtRet";
            this.txtRet.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtRet.Size = new System.Drawing.Size(217, 22);
            this.txtRet.TabIndex = 12;
            // 
            // Frame1
            // 
            this.Frame1.BackColor = System.Drawing.SystemColors.Control;
            this.Frame1.Controls.Add(this.txtCountStatus0);
            this.Frame1.Controls.Add(this.txtCountStatus3);
            this.Frame1.Controls.Add(this.txtCountStatus2);
            this.Frame1.Controls.Add(this.txtCountStatus1);
            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(11, 48);
            this.Frame1.Name = "Frame1";
            this.Frame1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Frame1.Size = new System.Drawing.Size(273, 129);
            this.Frame1.TabIndex = 11;
            this.Frame1.TabStop = false;
            this.Frame1.Text = "カウント値：";
            // 
            // 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, 24);
            this.txtCountStatus0.MaxLength = 0;
            this.txtCountStatus0.Name = "txtCountStatus0";
            this.txtCountStatus0.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountStatus0.Size = new System.Drawing.Size(88, 22);
            this.txtCountStatus0.TabIndex = 24;
            // 
            // 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, 96);
            this.txtCountStatus3.MaxLength = 0;
            this.txtCountStatus3.Name = "txtCountStatus3";
            this.txtCountStatus3.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountStatus3.Size = new System.Drawing.Size(88, 22);
            this.txtCountStatus3.TabIndex = 27;
            // 
            // 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, 72);
            this.txtCountStatus2.MaxLength = 0;
            this.txtCountStatus2.Name = "txtCountStatus2";
            this.txtCountStatus2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountStatus2.Size = new System.Drawing.Size(88, 22);
            this.txtCountStatus2.TabIndex = 26;
            // 
            // 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, 48);
            this.txtCountStatus1.MaxLength = 0;
            this.txtCountStatus1.Name = "txtCountStatus1";
            this.txtCountStatus1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountStatus1.Size = new System.Drawing.Size(88, 22);
            this.txtCountStatus1.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(64, 24);
            this.txtCountValue0.MaxLength = 0;
            this.txtCountValue0.Name = "txtCountValue0";
            this.txtCountValue0.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountValue0.Size = new System.Drawing.Size(88, 22);
            this.txtCountValue0.TabIndex = 12;
            // 
            // 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(64, 96);
            this.txtCountValue3.MaxLength = 0;
            this.txtCountValue3.Name = "txtCountValue3";
            this.txtCountValue3.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountValue3.Size = new System.Drawing.Size(88, 22);
            this.txtCountValue3.TabIndex = 15;
            // 
            // 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(64, 72);
            this.txtCountValue2.MaxLength = 0;
            this.txtCountValue2.Name = "txtCountValue2";
            this.txtCountValue2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountValue2.Size = new System.Drawing.Size(88, 22);
            this.txtCountValue2.TabIndex = 14;
            // 
            // 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(64, 48);
            this.txtCountValue1.MaxLength = 0;
            this.txtCountValue1.Name = "txtCountValue1";
            this.txtCountValue1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountValue1.Size = new System.Drawing.Size(88, 22);
            this.txtCountValue1.TabIndex = 13;
            // 
            // 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, 29);
            this.Label2.Name = "Label2";
            this.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label2.Size = new System.Drawing.Size(33, 12);
            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, 101);
            this.Label5.Name = "Label5";
            this.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label5.Size = new System.Drawing.Size(33, 12);
            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, 77);
            this.Label4.Name = "Label4";
            this.Label4.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label4.Size = new System.Drawing.Size(33, 12);
            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, 53);
            this.Label3.Name = "Label3";
            this.Label3.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label3.Size = new System.Drawing.Size(33, 12);
            this.Label3.TabIndex = 13;
            this.Label3.Text = "CH1：";
            // 
            // 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(304, 253);
            this.btnCntStop.Name = "btnCntStop";
            this.btnCntStop.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntStop.Size = new System.Drawing.Size(105, 24);
            this.btnCntStop.TabIndex = 7;
            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(304, 218);
            this.btnCntReset.Name = "btnCntReset";
            this.btnCntReset.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntReset.Size = new System.Drawing.Size(105, 24);
            this.btnCntReset.TabIndex = 6;
            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(304, 183);
            this.btnCntPreset.Name = "btnCntPreset";
            this.btnCntPreset.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntPreset.Size = new System.Drawing.Size(105, 24);
            this.btnCntPreset.TabIndex = 5;
            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(304, 148);
            this.btnCntStart.Name = "btnCntStart";
            this.btnCntStart.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntStart.Size = new System.Drawing.Size(105, 24);
            this.btnCntStart.TabIndex = 4;
            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(88, 9);
            this.txtDeviceName.MaxLength = 0;
            this.txtDeviceName.Name = "txtDeviceName";
            this.txtDeviceName.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtDeviceName.Size = new System.Drawing.Size(113, 22);
            this.txtDeviceName.TabIndex = 10;
            // 
            // 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(11, 14);
            this.Label1.Name = "Label1";
            this.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label1.Size = new System.Drawing.Size(61, 12);
            this.Label1.TabIndex = 23;
            this.Label1.Text = "デバイス名：";
            // 
            // 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(304, 113);
            this.btnCntEvent.Name = "btnCntEvent";
            this.btnCntEvent.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntEvent.Size = new System.Drawing.Size(105, 24);
            this.btnCntEvent.TabIndex = 3;
            this.btnCntEvent.Text = "イベント設定...";
            this.btnCntEvent.UseVisualStyleBackColor = false;
            this.btnCntEvent.Click += new System.EventHandler(this.btnCntEvent_Click);
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // 
            // CounterCallback
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
            this.ClientSize = new System.Drawing.Size(424, 358);
            this.Controls.Add(this.Frame2);
            this.Controls.Add(this.btnCntExit);
            this.Controls.Add(this.btnCntNotify);
            this.Controls.Add(this.btnCntMode);
            this.Controls.Add(this.btnCntInit);
            this.Controls.Add(this.Label6);
            this.Controls.Add(this.txtRet);
            this.Controls.Add(this.Frame1);
            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.btnCntEvent);
            this.Controls.Add(this.btnExit);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "CounterCallback";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "カウンタ";
            this.Load += new System.EventHandler(this.CounterCallback_Load);
            this.Frame2.ResumeLayout(false);
            this.Frame1.ResumeLayout(false);
            this.Frame1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        /// <summary>
        /// アプリケーションのメイン エントリ ポイントです。
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new CounterCallback());
        }

        //===================================================
        //    ダイアログ初期化
        //===================================================
        unsafe private void CounterCallback_Load(object sender, System.EventArgs e)
        {
            short  ch;

            //-----------------------------------
            // 画面表示の初期化
            //-----------------------------------
            txtDeviceName.Text = "CNT000";
            //-----------------------------------
            // 各チャネルごとに
            //-----------------------------------
            for(ch = 0; ch < CHANNEL_NUM; ch++){
                //------------------
                // カウント値コントロール
                //------------------
                txt_Count[ch].Text = "";
                //------------------
                // カウントステータスコントロール
                //------------------
                txt_Status[ch].Text = "";
            }
            //-----------------------------------
            // イベント通知用デリゲート初期化
            //-----------------------------------
            dele_CountUp       = new PCOUNTUPCALLBACK(SetCountUpCallBackProc);
            dele_CounterError  = new PCOUNTERERRORCALLBACK(CounterErrorCallBackProc);
            dele_CarryBorrow   = new PCARRYBORROWCALLBACK(CarryBorrowCallBackProc);
            //-----------------------------------
            // デリゲートが開放されないように設定します。
            //-----------------------------------
            if(hGC_CountUp.IsAllocated == false)
            {
                hGC_CountUp        = GCHandle.Alloc(dele_CountUp);
            }
            if(hGC_CounterError.IsAllocated == false)
            {
                hGC_CounterError   = GCHandle.Alloc(dele_CounterError);
            }
            if(hGC_CarryBorrow.IsAllocated == false)
            {
                hGC_CarryBorrow    = GCHandle.Alloc(dele_CarryBorrow);
            }
            /*
            //------------------------------
            // デバイスのタイマー通知イベントを利用される場合は、
            // 以下の設定をご使用ください
            //------------------------------
            //-----------------------------------
            // タイマーイベント通知用デリゲート初期化
            //-----------------------------------
            //dele_Timer   = new PTIMERCALLBACK(TimerCallBackProc);
            //-----------------------------------
            // デリゲートが開放されないように設定します。
            //-----------------------------------
            if(hGC_Timer.IsAllocated == false)
            {
                hGC_Timer   = GCHandle.Alloc(dele_Timer);
            }
            */
        }

        //---------------------------------------------
        // 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;

            //-----------------------------------
            //    デバイス名取得
            //-----------------------------------
            buf = txtDeviceName.Text;

            //------------------------------
            // 初期化処理
            //------------------------------
            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;
            }
            //-----------------------------
            // 総チャネル数を取得
            //-----------------------------
            cnt.GetMaxChannels(Id, out ChannelNum);
            //-----------------------------
            // サンプルのコントロール数を超える場合、総チャネル数をコントロール数に補正
            //-----------------------------
            if(ChannelNum > CHANNEL_NUM){
                ChannelNum = CHANNEL_NUM;
            }
            //-----------------------------
            // カウンタ、ステータス表示を総チャネル数に合わせて切り替え
            //-----------------------------
            for(i = 0; i < CHANNEL_NUM; i++){
                //-----------------------------
                // 総チャネル数を超えた場合表示を無効化
                //-----------------------------
                if(i >= ChannelNum){
                    txt_Count[i].Enabled = false;
                    txt_Status[i].Enabled = false;
                //-----------------------------
                // 総チャネル数以下の場合表示を有効化
                //-----------------------------
                }else{
                    txt_Count[i].Enabled = true;
                    txt_Status[i].Enabled = true;
                }
            }
        }

        //---------------------------------------------
        // カウンタモード設定画面表示
        //---------------------------------------------
        private void btnCntMode_Click(object sender, System.EventArgs e)
        {
            //------------------------------
            // カウンタモード設定画面表示
            //------------------------------
            ModeDlg    dlg = new ModeDlg();
            dlg.ShowDialog();
            dlg.Dispose();
        }

        //---------------------------------------------
        // 通知設定画面表示
        //---------------------------------------------
        private void btnCntNotify_Click(object sender, System.EventArgs e)
        {
            //------------------------------
            // 通知設定画面表示
            //------------------------------
            NotifyDlg dlg = new NotifyDlg();
            dlg.ShowDialog();
            dlg.Dispose();
        }

        //---------------------------------------------
        // イベント設定画面表示
        //---------------------------------------------
        private void btnCntEvent_Click(object sender, System.EventArgs e)
        {
            //------------------------------
            // イベント設定画面表示
            //------------------------------
            EventDlg dlg = new EventDlg();
            dlg.ShowDialog();
            dlg.Dispose();
        }

        //---------------------------------------------
        // カウント開始
        //---------------------------------------------
        unsafe 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;

            //-----------------------------
            // コールバックルーチン
            //-----------------------------
            // カウント一致
            pCountUp    = Marshal.GetFunctionPointerForDelegate(dele_CountUp);
            ret = cnt.CountUpCallbackProc(Id, pCountUp, null);
            /*
            //-----------------------------
            // イベント通知機能のないデバイスに合わせてエラーチェックをコメントアウトしています
            // エラーチェックが必要な場合はコメントアウトを解除してください
            //-----------------------------
            if(ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                str = string.Format("CntSetCountUpCallbackProc ret = {0:d}: {1}", ret, error_string);
                txtRet.Text = str;
                return;
            }   
            */
            // カウンタ エラー
            pCounterError    = Marshal.GetFunctionPointerForDelegate(dele_CounterError);
            ret = cnt.CounterErrorCallbackProc(Id, pCounterError, null);
            /*
            //-----------------------------
            // イベント通知機能のないデバイスに合わせてエラーチェックをコメントアウトしています
            // エラーチェックが必要な場合はコメントアウトを解除してください
            //-----------------------------
            if(ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                str = string.Format("CntSetCounterErrorCallbackProc ret = {0:d}: {1}", ret, error_string);
                txtRet.Text = str;
                return;
            }   
            */
            // キャリー/ボロー
            pCarryBorrow    = Marshal.GetFunctionPointerForDelegate(dele_CarryBorrow);
            ret = cnt.CarryBorrowCallbackProc(Id, pCarryBorrow, null);
            /*
            //-----------------------------
            // イベント通知機能のないデバイスに合わせてエラーチェックをコメントアウトしています
            // エラーチェックが必要な場合はコメントアウトを解除してください
            //-----------------------------
            if(ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                str = string.Format("CntCarryBorrowCallbackProc ret = {0:d}: {1}", ret, error_string);
                txtRet.Text = str;
                return;
            }
            */
            /*
            //------------------------------
            // デバイスのタイマー通知イベントを利用される場合は、
            // 以下の関数をご使用ください
            //------------------------------
            // タイマー
            pTimer    = Marshal.GetFunctionPointerForDelegate(dele_Timer);
            // タイマー通知
            ret = cnt.TimerCallbackProc(Id, pTimer, null);
            if(ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                str = string.Format("CntTimerCallbackProc ret = {0:d}: {1}", ret, error_string);
                txtRet.Text = str;
                return;
            }
            */

            //------------------------------
            // パラメータのセット
            //------------------------------
            for (i=0; i<ChannelNum; i++) 
            {
                ChNo[i] = i;
            }

            //------------------------------
            // カウント開始処理
            //------------------------------
            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) 
            {
                //------------------------------
                // タイマー通知機能を持たないデバイスを考慮し、
                // ウィンドウタイマーを使用しております。
                // 用途に合わせて、切り替えてご使用ください。
                //------------------------------
                //-----------------------------
                // タイマーを起動
                //-----------------------------
                timer1.Start();
                //------------------------------
                // デバイスのタイマー通知イベントを利用される場合は、
                // 以下の関数をご使用ください
                //------------------------------
                //cnt.NotifyTimer(Id, 100, this.Handle);
            }
        }

        //---------------------------------------------
        // カウンタプリセット
        //---------------------------------------------
        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;
            //------------------------------
            // プリセット値のセット
            //------------------------------
            for (i=0; i<ChannelNum; i++) 
            {
                ChNo[i] = i;
                PresetData[i] = (uint)i * 0x100;
            }

            //------------------------------
            // プリセット設定処理
            //------------------------------
            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;
        }

        //---------------------------------------------
        // カウンタリセット
        //---------------------------------------------
        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;

            //------------------------------
            // パラメータのセット
            //------------------------------
            for (i=0; i<ChannelNum; i++) 
            {
                ChNo[i] = i;
            }

            //------------------------------
            // カウンタリセット処理
            //------------------------------
            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;
        }

        //---------------------------------------------
        // カウント停止
        //---------------------------------------------
        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;

            //------------------------------
            // パラメータのセット
            //------------------------------
            for (i=0; i<ChannelNum; i++) 
            {
                ChNo[i] = i;
            }

            //------------------------------
            // カウント停止処理
            //------------------------------
            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;

            //------------------------------
            // タイマー通知機能を持たないデバイスを考慮し、
            // ウィンドウタイマーを使用しております。
            // 用途に合わせて、切り替えてご使用ください。
            //------------------------------
            //------------------------------
            // タイマーを停止
            //------------------------------
            timer1.Stop();
            //------------------------------
            // デバイスのタイマー通知イベントを利用される場合は、
            // 以下の関数をご使用ください
            //------------------------------
            //cnt.StopNotifyTimer(Id);
        }

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

            //------------------------------
            // タイマー通知機能を持たないデバイスを考慮し、
            // ウィンドウタイマーを使用しております。
            // 用途に合わせて、切り替えてご使用ください。
            //------------------------------
            //------------------------------
            // タイマーを停止
            //------------------------------
            timer1.Stop();
            //------------------------------
            // デバイスのタイマー通知イベントを利用される場合は、
            // 以下の関数をご使用ください
            //------------------------------
            //cnt.StopNotifyTimer(Id);

            //------------------------------
            // カウンタ終了処理
            //------------------------------
            ret = cnt.Exit(Id);
            cnt.GetErrorString(ret, out error_string);
            str = string.Format("CntExit ret = {0:d}: {1}", ret, error_string);
            txtRet.Text = str;
        }

        //---------------------------------------------
        // サンプルプログラム終了
        //---------------------------------------------
        private void btnExit_Click(object sender, System.EventArgs e)
        {
            int    ret;

            //------------------------------
            // タイマー通知機能を持たないデバイスを考慮し、
            // ウィンドウタイマーを使用しております。
            // 用途に合わせて、切り替えてご使用ください。
            //------------------------------
            //------------------------------
            // タイマーを停止
            //------------------------------
            timer1.Stop();
            //------------------------------
            // デバイスのタイマー通知イベントを利用される場合は、
            // 以下の関数をご使用ください
            //------------------------------
            //cnt.StopNotifyTimer(Id);

            //------------------------------
            // カウンタ終了処理
            //------------------------------
            ret = cnt.Exit(Id);

            //-----------------------------
            // イベント通知用デリゲートのガベージコレクションハンドルを開放
            //-----------------------------
            if(hGC_CountUp.IsAllocated == true)
            {
                hGC_CountUp.Free();
            }
            if(hGC_CounterError.IsAllocated == true)
            {
                hGC_CounterError.Free();
            }
            if(hGC_CarryBorrow.IsAllocated == true)
            {
                hGC_CarryBorrow.Free();
            }
            if(hGC_Timer.IsAllocated == true)
            {
                hGC_Timer.Free();
            }

            this.Close();
        }

        delegate void SetTextDelegate(string str);
        delegate void SetCountDataDelegate(uint[] CntData, uint[] StatusData);

        //---------------------------------------------------------
        // NotifyLabel設定(コントロール操作用同期関数)
        //---------------------------------------------------------
        void SetNotifyText(string str)
        {
            lblNotify.Text = str;
        }
        //---------------------------------------------------------
        // RetTextBox設定(コントロール操作用同期関数)
        //---------------------------------------------------------
        void SetRetText(string str)
        {
            txtRet.Text = str;
        }
        //---------------------------------------------------------
        // カウントデータ設定(コントロール操作用同期関数)
        //---------------------------------------------------------
        void SetCountDataText(uint[] CntData, uint[] StatusData)
        {
            string str;
            short  i;

            //------------------------------
            // データ表示処理
            //------------------------------
            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;
            }
        }

        //---------------------------------------------------------
        //  コールバックルーチン
        //---------------------------------------------------------
        unsafe public void SetCountUpCallBackProc(short Id, int wParam, int lParam, void *Param)
        {
            string str;
            int    MatchCh;

            MatchCh = (int)(wParam & 0x0000ffff);
            switch (MatchCh)
            {
                case 0:    // カウント一致: 0CH
                    CountMatch[0]++;
                    if(((wParam & 0x0ffff0000) >> 16) == 1 )
                    {
                        str = string.Format("イベント:\nカウント一致!\nチャネル番号: 0, UP, カウント: {0:X8}(Hex)\n 一致回数: {1:d} 回", lParam, CountMatch[0]);
                    }
                    else
                    {
                        str = string.Format("イベント:\nカウント一致!\nチャネル番号: 0, DOWN, カウント: {0:X8}(Hex)\n 一致回数: {1:d} 回", lParam, CountMatch[0]);
                    }
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
                case 1:    // カウント一致: 1CH
                    CountMatch[1]++;
                    if(((wParam & 0x0ffff0000) >> 16) == 1 )
                    {
                        str = string.Format("イベント:\nカウント一致!\nチャネル番号: 1, UP, カウント: {0:X8}(Hex)\n 一致回数: {1:d} 回", lParam, CountMatch[1]);
                    }
                    else
                    {
                        str = string.Format("イベント:\nカウント一致!\nチャネル番号: 1, DOWN, カウント: {0:X8}(Hex)\n 一致回数: {1:d} 回", lParam, CountMatch[1]);
                    }
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
                case 2:    // カウント一致: 2CH
                    CountMatch[2]++;
                    if(((wParam & 0x0ffff0000) >> 16) == 1 )
                    {
                        str = string.Format("イベント:\nカウント一致!\nチャネル番号: 2, UP, カウント: {0:X8}(Hex)\n 一致回数: {1:d} 回", lParam, CountMatch[2]);
                    }
                    else
                    {
                        str = string.Format("イベント:\nカウント一致!\nチャネル番号: 2, DOWN, カウント: {0:X8}(Hex)\n 一致回数: {1:d} 回", lParam, CountMatch[2]);
                    }
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
                case 3:    // カウント一致: 3CH
                    CountMatch[3]++;
                    if(((wParam & 0x0ffff0000) >> 16) == 1 )
                    {
                        str = string.Format("イベント:\nカウント一致!\nチャネル番号: 3, UP, カウント: {0:X8}(Hex)\n 一致回数: {1:d} 回", lParam, CountMatch[3]);
                    }
                    else
                    {
                        str = string.Format("イベント:\nカウント一致!\nチャネル番号: 3, DOWN, カウント: {0:X8}(Hex)\n 一致回数: {1:d} 回", lParam, CountMatch[3]);
                    }
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
            }

            return;
        }

        unsafe public void CounterErrorCallBackProc(short Id, int wParam, int lParam, void *Param)
        {
            string str;
            int    ErrType;
            int    ErrCh;

            ErrType    = (int)(lParam & 0x0000ffff);
            ErrCh      = (int)(wParam & 0x0000ffff);
            switch (ErrType)
            {
                case (int)CcntConst.CNT_AI_ERROR:        // 異常入力
                    str = string.Format("イベント:\n異常入力エラー!\nチャネル番号: {0:d}", ErrCh);
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
                case (int)CcntConst.CNT_FLT_ERROR:        // フィルター
                    str = string.Format("イベント:\nフィルタエラー!\nチャネル番号: {0:d}", ErrCh);
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
                case (int)CcntConst.CNT_ALM_ERROR:        // アラーム
                    str = string.Format("イベント:\nアラームエラー!\nチャネル番号: {0:d}", ErrCh);
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
            }

            return;
        }

        unsafe public void CarryBorrowCallBackProc(short Id, int wParam, int lParam, void *Param)
        {
            string str;
            int    NotifyFac;
            int    CarryCh;

            NotifyFac  = (int)(lParam & 0x0000ffff);
            CarryCh    = (int)(wParam & 0x0000ffff);
            switch (NotifyFac)
            {
                case (int)CcntConst.CNT_CB_CARRY:        // キャリー
                    str = string.Format("イベント:\nキャリー!\nチャネル番号: {0:d}", CarryCh);
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
                case (int)CcntConst.CNT_CB_BORROW:        // ボロー
                    str = string.Format("イベント:\nボロー!\nチャネル番号: {0:d}", CarryCh);
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
            }

            return;
        }

        //---------------------------------------------------------
        // タイマー通知機能を持たないデバイスを考慮し、
        // ウィンドウタイマーを使用しております。
        // 用途に合わせて、切り替えてご使用ください。
        //---------------------------------------------------------
        //---------------------------------------------------------
        // ウィンドウタイマー用メソッド
        //---------------------------------------------------------
        private void timer1_Tick(object sender, System.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);

            //------------------------------
            // パラメータのセット
            //------------------------------
            for (i = 0; i < ChannelNum; i++)
            {
                ChNo[i] = i;
                CntData[i] = 0;
                StatusData[i] = 0;
            }
            //------------------------------
            // カウント値取得処理
            //------------------------------
            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;
                timer1.Stop();
            }
            //------------------------------
            // ステータス取得処理
            //------------------------------
            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;
                    timer1.Stop();
                }
            }
            //------------------------------
            // データ表示処理
            //------------------------------
            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;
            }
        }

        /*
        //---------------------------------------------------------
        // デバイスのタイマー通知イベントを利用される場合、
        // 以下のコールバックルーチンをご使用ください。
        //---------------------------------------------------------
        //---------------------------------------------------------
        // タイマー通知用コールバックルーチン
        //---------------------------------------------------------
        unsafe public void TimerCallBackProc(short Id, int wParam, int lParam, void *Param)
        {
            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);
            
            //------------------------------
            // パラメータのセット
            //------------------------------
            for (i=0; i<ChannelNum; i++) 
            {
                ChNo[i] = i;
                CntData[i] = 0;
                StatusData[i] = 0;
            }
            //------------------------------
            // カウント値取得処理
            //------------------------------
            ret = cnt.ReadCount(Id, ChNo, ChannelNum, CntData);
            if (ret != 0) 
            {
                cnt.GetErrorString(ret, out error_string);
                str = string.Format("CntReadCnt ret = {0:d}: {1}", ret, error_string);
                this.Invoke(new SetTextDelegate(SetRetText), new Object[] {str});
                cnt.StopNotifyTimer(Id);
            }
            //------------------------------
            // ステータス取得処理
            //------------------------------
            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);
                    this.Invoke(new SetTextDelegate(SetRetText), new Object[] {str});
                    cnt.StopNotifyTimer(Id);
                }
            }
            //------------------------------
            // データ表示処理
            //------------------------------
            this.Invoke(new SetCountDataDelegate(SetCountDataText), new Object[] {CntData, StatusData});
            return;
        }
        */ 
    }
}
