﻿' High-performance Counter Sample
Imports System.Text
Imports VB = Microsoft.VisualBasic

Public Class frmCounter
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Public WithEvents Frame2 As System.Windows.Forms.GroupBox
    Public WithEvents lblNotify As System.Windows.Forms.Label
    Public WithEvents btnCntExit As System.Windows.Forms.Button
    Public WithEvents btnCntInit As System.Windows.Forms.Button
    Public WithEvents btnCntNotify As System.Windows.Forms.Button
    Public WithEvents btnCntMode As System.Windows.Forms.Button
    Public WithEvents btnCntStart As System.Windows.Forms.Button
    Public WithEvents Label6 As System.Windows.Forms.Label
    Public WithEvents btnExit As System.Windows.Forms.Button
    Public WithEvents txtRet As System.Windows.Forms.TextBox
    Public WithEvents btnCntStop As System.Windows.Forms.Button
    Public WithEvents btnCntPreset As System.Windows.Forms.Button
    Public WithEvents Frame1 As System.Windows.Forms.GroupBox
    Public WithEvents txtCountStatus3 As System.Windows.Forms.TextBox
    Public WithEvents txtCountStatus2 As System.Windows.Forms.TextBox
    Public WithEvents txtCountStatus1 As System.Windows.Forms.TextBox
    Public WithEvents txtCountStatus0 As System.Windows.Forms.TextBox
    Public WithEvents txtCountValue0 As System.Windows.Forms.TextBox
    Public WithEvents txtCountValue3 As System.Windows.Forms.TextBox
    Public WithEvents txtCountValue2 As System.Windows.Forms.TextBox
    Public WithEvents txtCountValue1 As System.Windows.Forms.TextBox
    Public WithEvents Label2 As System.Windows.Forms.Label
    Public WithEvents Label5 As System.Windows.Forms.Label
    Public WithEvents Label4 As System.Windows.Forms.Label
    Public WithEvents Label3 As System.Windows.Forms.Label
    Public WithEvents btnCntReset As System.Windows.Forms.Button
    Public WithEvents txtDeviceName As System.Windows.Forms.TextBox
    Public WithEvents Label1 As System.Windows.Forms.Label
    Private WithEvents timer1 As System.Windows.Forms.Timer
    Public WithEvents btnCntEvent As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.Frame2 = New System.Windows.Forms.GroupBox
        Me.lblNotify = New System.Windows.Forms.Label
        Me.btnCntExit = New System.Windows.Forms.Button
        Me.btnCntInit = New System.Windows.Forms.Button
        Me.btnCntNotify = New System.Windows.Forms.Button
        Me.btnCntMode = New System.Windows.Forms.Button
        Me.btnCntStart = New System.Windows.Forms.Button
        Me.Label6 = New System.Windows.Forms.Label
        Me.btnExit = New System.Windows.Forms.Button
        Me.txtRet = New System.Windows.Forms.TextBox
        Me.btnCntStop = New System.Windows.Forms.Button
        Me.btnCntPreset = New System.Windows.Forms.Button
        Me.Frame1 = New System.Windows.Forms.GroupBox
        Me.txtCountStatus3 = New System.Windows.Forms.TextBox
        Me.txtCountStatus2 = New System.Windows.Forms.TextBox
        Me.txtCountStatus1 = New System.Windows.Forms.TextBox
        Me.txtCountStatus0 = New System.Windows.Forms.TextBox
        Me.txtCountValue0 = New System.Windows.Forms.TextBox
        Me.txtCountValue3 = New System.Windows.Forms.TextBox
        Me.txtCountValue2 = New System.Windows.Forms.TextBox
        Me.txtCountValue1 = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label5 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.btnCntReset = New System.Windows.Forms.Button
        Me.txtDeviceName = New System.Windows.Forms.TextBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.btnCntEvent = New System.Windows.Forms.Button
        Me.timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.Frame2.SuspendLayout()
        Me.Frame1.SuspendLayout()
        Me.SuspendLayout()
        '
        'Frame2
        '
        Me.Frame2.BackColor = System.Drawing.SystemColors.Control
        Me.Frame2.Controls.Add(Me.lblNotify)
        Me.Frame2.ForeColor = System.Drawing.SystemColors.ControlText
        Me.Frame2.Location = New System.Drawing.Point(13, 215)
        Me.Frame2.Name = "Frame2"
        Me.Frame2.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Frame2.Size = New System.Drawing.Size(273, 150)
        Me.Frame2.TabIndex = 68
        Me.Frame2.TabStop = False
        Me.Frame2.Text = "Notification:"
        '
        'lblNotify
        '
        Me.lblNotify.BackColor = System.Drawing.SystemColors.Control
        Me.lblNotify.Cursor = System.Windows.Forms.Cursors.Default
        Me.lblNotify.ForeColor = System.Drawing.SystemColors.ControlText
        Me.lblNotify.Location = New System.Drawing.Point(8, 28)
        Me.lblNotify.Name = "lblNotify"
        Me.lblNotify.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.lblNotify.Size = New System.Drawing.Size(249, 103)
        Me.lblNotify.TabIndex = 20
        Me.lblNotify.Text = "Notification:"
        '
        'btnCntExit
        '
        Me.btnCntExit.BackColor = System.Drawing.SystemColors.Control
        Me.btnCntExit.Cursor = System.Windows.Forms.Cursors.Default
        Me.btnCntExit.ForeColor = System.Drawing.SystemColors.ControlText
        Me.btnCntExit.Location = New System.Drawing.Point(306, 336)
        Me.btnCntExit.Name = "btnCntExit"
        Me.btnCntExit.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.btnCntExit.Size = New System.Drawing.Size(105, 28)
        Me.btnCntExit.TabIndex = 65
        Me.btnCntExit.Text = "CntExit"
        Me.btnCntExit.UseVisualStyleBackColor = False
        '
        'btnCntInit
        '
        Me.btnCntInit.BackColor = System.Drawing.SystemColors.Control
        Me.btnCntInit.Cursor = System.Windows.Forms.Cursors.Default
        Me.btnCntInit.ForeColor = System.Drawing.SystemColors.ControlText
        Me.btnCntInit.Location = New System.Drawing.Point(306, 11)
        Me.btnCntInit.Name = "btnCntInit"
        Me.btnCntInit.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.btnCntInit.Size = New System.Drawing.Size(105, 28)
        Me.btnCntInit.TabIndex = 55
        Me.btnCntInit.Text = "CntInit"
        Me.btnCntInit.UseVisualStyleBackColor = False
        '
        'btnCntNotify
        '
        Me.btnCntNotify.BackColor = System.Drawing.SystemColors.Control
        Me.btnCntNotify.Cursor = System.Windows.Forms.Cursors.Default
        Me.btnCntNotify.ForeColor = System.Drawing.SystemColors.ControlText
        Me.btnCntNotify.Location = New System.Drawing.Point(306, 92)
        Me.btnCntNotify.Name = "btnCntNotify"
        Me.btnCntNotify.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.btnCntNotify.Size = New System.Drawing.Size(105, 28)
        Me.btnCntNotify.TabIndex = 59
        Me.btnCntNotify.Text = "Notification..."
        Me.btnCntNotify.UseVisualStyleBackColor = False
        '
        'btnCntMode
        '
        Me.btnCntMode.BackColor = System.Drawing.SystemColors.Control
        Me.btnCntMode.Cursor = System.Windows.Forms.Cursors.Default
        Me.btnCntMode.ForeColor = System.Drawing.SystemColors.ControlText
        Me.btnCntMode.Location = New System.Drawing.Point(306, 51)
        Me.btnCntMode.Name = "btnCntMode"
        Me.btnCntMode.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.btnCntMode.Size = New System.Drawing.Size(105, 28)
        Me.btnCntMode.TabIndex = 58
        Me.btnCntMode.Text = "Counter Mode..."
        Me.btnCntMode.UseVisualStyleBackColor = False
        '
        'btnCntStart
        '
        Me.btnCntStart.BackColor = System.Drawing.SystemColors.Control
        Me.btnCntStart.Cursor = System.Windows.Forms.Cursors.Default
        Me.btnCntStart.ForeColor = System.Drawing.SystemColors.ControlText
        Me.btnCntStart.Location = New System.Drawing.Point(306, 174)
        Me.btnCntStart.Name = "btnCntStart"
        Me.btnCntStart.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.btnCntStart.Size = New System.Drawing.Size(105, 27)
        Me.btnCntStart.TabIndex = 61
        Me.btnCntStart.Text = "CntStart"
        Me.btnCntStart.UseVisualStyleBackColor = False
        '
        'Label6
        '
        Me.Label6.AutoSize = True
        Me.Label6.BackColor = System.Drawing.SystemColors.Control
        Me.Label6.Cursor = System.Windows.Forms.Cursors.Default
        Me.Label6.ForeColor = System.Drawing.SystemColors.ControlText
        Me.Label6.Location = New System.Drawing.Point(13, 384)
        Me.Label6.Name = "Label6"
        Me.Label6.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Label6.Size = New System.Drawing.Size(72, 13)
        Me.Label6.TabIndex = 69
        Me.Label6.Text = "Return Value:"
        '
        'btnExit
        '
        Me.btnExit.BackColor = System.Drawing.SystemColors.Control
        Me.btnExit.Cursor = System.Windows.Forms.Cursors.Default
        Me.btnExit.ForeColor = System.Drawing.SystemColors.ControlText
        Me.btnExit.Location = New System.Drawing.Point(306, 377)
        Me.btnExit.Name = "btnExit"
        Me.btnExit.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.btnExit.Size = New System.Drawing.Size(105, 28)
        Me.btnExit.TabIndex = 66
        Me.btnExit.Text = "Exit"
        Me.btnExit.UseVisualStyleBackColor = False
        '
        'txtRet
        '
        Me.txtRet.AcceptsReturn = True
        Me.txtRet.BackColor = System.Drawing.SystemColors.Window
        Me.txtRet.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.txtRet.ForeColor = System.Drawing.SystemColors.WindowText
        Me.txtRet.Location = New System.Drawing.Point(87, 378)
        Me.txtRet.MaxLength = 0
        Me.txtRet.Name = "txtRet"
        Me.txtRet.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.txtRet.Size = New System.Drawing.Size(206, 20)
        Me.txtRet.TabIndex = 70
        '
        'btnCntStop
        '
        Me.btnCntStop.BackColor = System.Drawing.SystemColors.Control
        Me.btnCntStop.Cursor = System.Windows.Forms.Cursors.Default
        Me.btnCntStop.ForeColor = System.Drawing.SystemColors.ControlText
        Me.btnCntStop.Location = New System.Drawing.Point(306, 296)
        Me.btnCntStop.Name = "btnCntStop"
        Me.btnCntStop.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.btnCntStop.Size = New System.Drawing.Size(105, 28)
        Me.btnCntStop.TabIndex = 64
        Me.btnCntStop.Text = "CntStop"
        Me.btnCntStop.UseVisualStyleBackColor = False
        '
        'btnCntPreset
        '
        Me.btnCntPreset.BackColor = System.Drawing.SystemColors.Control
        Me.btnCntPreset.Cursor = System.Windows.Forms.Cursors.Default
        Me.btnCntPreset.ForeColor = System.Drawing.SystemColors.ControlText
        Me.btnCntPreset.Location = New System.Drawing.Point(306, 214)
        Me.btnCntPreset.Name = "btnCntPreset"
        Me.btnCntPreset.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.btnCntPreset.Size = New System.Drawing.Size(105, 28)
        Me.btnCntPreset.TabIndex = 62
        Me.btnCntPreset.Text = "CntPreset"
        Me.btnCntPreset.UseVisualStyleBackColor = False
        '
        'Frame1
        '
        Me.Frame1.BackColor = System.Drawing.SystemColors.Control
        Me.Frame1.Controls.Add(Me.txtCountStatus3)
        Me.Frame1.Controls.Add(Me.txtCountStatus2)
        Me.Frame1.Controls.Add(Me.txtCountStatus1)
        Me.Frame1.Controls.Add(Me.txtCountStatus0)
        Me.Frame1.Controls.Add(Me.txtCountValue0)
        Me.Frame1.Controls.Add(Me.txtCountValue3)
        Me.Frame1.Controls.Add(Me.txtCountValue2)
        Me.Frame1.Controls.Add(Me.txtCountValue1)
        Me.Frame1.Controls.Add(Me.Label2)
        Me.Frame1.Controls.Add(Me.Label5)
        Me.Frame1.Controls.Add(Me.Label4)
        Me.Frame1.Controls.Add(Me.Label3)
        Me.Frame1.ForeColor = System.Drawing.SystemColors.ControlText
        Me.Frame1.Location = New System.Drawing.Point(13, 57)
        Me.Frame1.Name = "Frame1"
        Me.Frame1.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Frame1.Size = New System.Drawing.Size(273, 150)
        Me.Frame1.TabIndex = 67
        Me.Frame1.TabStop = False
        Me.Frame1.Text = "Count Value/Status:"
        '
        'txtCountStatus3
        '
        Me.txtCountStatus3.AcceptsReturn = True
        Me.txtCountStatus3.BackColor = System.Drawing.SystemColors.Window
        Me.txtCountStatus3.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.txtCountStatus3.ForeColor = System.Drawing.SystemColors.WindowText
        Me.txtCountStatus3.Location = New System.Drawing.Point(160, 112)
        Me.txtCountStatus3.MaxLength = 0
        Me.txtCountStatus3.Name = "txtCountStatus3"
        Me.txtCountStatus3.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.txtCountStatus3.Size = New System.Drawing.Size(104, 20)
        Me.txtCountStatus3.TabIndex = 28
        '
        'txtCountStatus2
        '
        Me.txtCountStatus2.AcceptsReturn = True
        Me.txtCountStatus2.BackColor = System.Drawing.SystemColors.Window
        Me.txtCountStatus2.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.txtCountStatus2.ForeColor = System.Drawing.SystemColors.WindowText
        Me.txtCountStatus2.Location = New System.Drawing.Point(160, 84)
        Me.txtCountStatus2.MaxLength = 0
        Me.txtCountStatus2.Name = "txtCountStatus2"
        Me.txtCountStatus2.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.txtCountStatus2.Size = New System.Drawing.Size(104, 20)
        Me.txtCountStatus2.TabIndex = 27
        '
        'txtCountStatus1
        '
        Me.txtCountStatus1.AcceptsReturn = True
        Me.txtCountStatus1.BackColor = System.Drawing.SystemColors.Window
        Me.txtCountStatus1.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.txtCountStatus1.ForeColor = System.Drawing.SystemColors.WindowText
        Me.txtCountStatus1.Location = New System.Drawing.Point(160, 56)
        Me.txtCountStatus1.MaxLength = 0
        Me.txtCountStatus1.Name = "txtCountStatus1"
        Me.txtCountStatus1.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.txtCountStatus1.Size = New System.Drawing.Size(104, 20)
        Me.txtCountStatus1.TabIndex = 26
        '
        'txtCountStatus0
        '
        Me.txtCountStatus0.AcceptsReturn = True
        Me.txtCountStatus0.BackColor = System.Drawing.SystemColors.Window
        Me.txtCountStatus0.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.txtCountStatus0.ForeColor = System.Drawing.SystemColors.WindowText
        Me.txtCountStatus0.Location = New System.Drawing.Point(160, 28)
        Me.txtCountStatus0.MaxLength = 0
        Me.txtCountStatus0.Name = "txtCountStatus0"
        Me.txtCountStatus0.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.txtCountStatus0.Size = New System.Drawing.Size(104, 20)
        Me.txtCountStatus0.TabIndex = 25
        '
        'txtCountValue0
        '
        Me.txtCountValue0.AcceptsReturn = True
        Me.txtCountValue0.BackColor = System.Drawing.SystemColors.Window
        Me.txtCountValue0.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.txtCountValue0.ForeColor = System.Drawing.SystemColors.WindowText
        Me.txtCountValue0.Location = New System.Drawing.Point(48, 28)
        Me.txtCountValue0.MaxLength = 0
        Me.txtCountValue0.Name = "txtCountValue0"
        Me.txtCountValue0.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.txtCountValue0.Size = New System.Drawing.Size(104, 20)
        Me.txtCountValue0.TabIndex = 24
        '
        'txtCountValue3
        '
        Me.txtCountValue3.AcceptsReturn = True
        Me.txtCountValue3.BackColor = System.Drawing.SystemColors.Window
        Me.txtCountValue3.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.txtCountValue3.ForeColor = System.Drawing.SystemColors.WindowText
        Me.txtCountValue3.Location = New System.Drawing.Point(48, 112)
        Me.txtCountValue3.MaxLength = 0
        Me.txtCountValue3.Name = "txtCountValue3"
        Me.txtCountValue3.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.txtCountValue3.Size = New System.Drawing.Size(104, 20)
        Me.txtCountValue3.TabIndex = 18
        '
        'txtCountValue2
        '
        Me.txtCountValue2.AcceptsReturn = True
        Me.txtCountValue2.BackColor = System.Drawing.SystemColors.Window
        Me.txtCountValue2.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.txtCountValue2.ForeColor = System.Drawing.SystemColors.WindowText
        Me.txtCountValue2.Location = New System.Drawing.Point(48, 84)
        Me.txtCountValue2.MaxLength = 0
        Me.txtCountValue2.Name = "txtCountValue2"
        Me.txtCountValue2.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.txtCountValue2.Size = New System.Drawing.Size(104, 20)
        Me.txtCountValue2.TabIndex = 17
        '
        'txtCountValue1
        '
        Me.txtCountValue1.AcceptsReturn = True
        Me.txtCountValue1.BackColor = System.Drawing.SystemColors.Window
        Me.txtCountValue1.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.txtCountValue1.ForeColor = System.Drawing.SystemColors.WindowText
        Me.txtCountValue1.Location = New System.Drawing.Point(48, 56)
        Me.txtCountValue1.MaxLength = 0
        Me.txtCountValue1.Name = "txtCountValue1"
        Me.txtCountValue1.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.txtCountValue1.Size = New System.Drawing.Size(104, 20)
        Me.txtCountValue1.TabIndex = 16
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.BackColor = System.Drawing.SystemColors.Control
        Me.Label2.Cursor = System.Windows.Forms.Cursors.Default
        Me.Label2.ForeColor = System.Drawing.SystemColors.ControlText
        Me.Label2.Location = New System.Drawing.Point(8, 34)
        Me.Label2.Name = "Label2"
        Me.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Label2.Size = New System.Drawing.Size(31, 13)
        Me.Label2.TabIndex = 23
        Me.Label2.Text = "CH0:"
        '
        'Label5
        '
        Me.Label5.AutoSize = True
        Me.Label5.BackColor = System.Drawing.SystemColors.Control
        Me.Label5.Cursor = System.Windows.Forms.Cursors.Default
        Me.Label5.ForeColor = System.Drawing.SystemColors.ControlText
        Me.Label5.Location = New System.Drawing.Point(8, 117)
        Me.Label5.Name = "Label5"
        Me.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Label5.Size = New System.Drawing.Size(31, 13)
        Me.Label5.TabIndex = 15
        Me.Label5.Text = "CH3:"
        '
        'Label4
        '
        Me.Label4.AutoSize = True
        Me.Label4.BackColor = System.Drawing.SystemColors.Control
        Me.Label4.Cursor = System.Windows.Forms.Cursors.Default
        Me.Label4.ForeColor = System.Drawing.SystemColors.ControlText
        Me.Label4.Location = New System.Drawing.Point(8, 90)
        Me.Label4.Name = "Label4"
        Me.Label4.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Label4.Size = New System.Drawing.Size(31, 13)
        Me.Label4.TabIndex = 14
        Me.Label4.Text = "CH2:"
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.BackColor = System.Drawing.SystemColors.Control
        Me.Label3.Cursor = System.Windows.Forms.Cursors.Default
        Me.Label3.ForeColor = System.Drawing.SystemColors.ControlText
        Me.Label3.Location = New System.Drawing.Point(8, 62)
        Me.Label3.Name = "Label3"
        Me.Label3.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Label3.Size = New System.Drawing.Size(31, 13)
        Me.Label3.TabIndex = 13
        Me.Label3.Text = "CH1:"
        '
        'btnCntReset
        '
        Me.btnCntReset.BackColor = System.Drawing.SystemColors.Control
        Me.btnCntReset.Cursor = System.Windows.Forms.Cursors.Default
        Me.btnCntReset.ForeColor = System.Drawing.SystemColors.ControlText
        Me.btnCntReset.Location = New System.Drawing.Point(306, 255)
        Me.btnCntReset.Name = "btnCntReset"
        Me.btnCntReset.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.btnCntReset.Size = New System.Drawing.Size(105, 28)
        Me.btnCntReset.TabIndex = 63
        Me.btnCntReset.Text = "CntReset"
        Me.btnCntReset.UseVisualStyleBackColor = False
        '
        'txtDeviceName
        '
        Me.txtDeviceName.AcceptsReturn = True
        Me.txtDeviceName.BackColor = System.Drawing.SystemColors.Window
        Me.txtDeviceName.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.txtDeviceName.ForeColor = System.Drawing.SystemColors.WindowText
        Me.txtDeviceName.Location = New System.Drawing.Point(90, 12)
        Me.txtDeviceName.MaxLength = 0
        Me.txtDeviceName.Name = "txtDeviceName"
        Me.txtDeviceName.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.txtDeviceName.Size = New System.Drawing.Size(113, 20)
        Me.txtDeviceName.TabIndex = 57
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.BackColor = System.Drawing.SystemColors.Control
        Me.Label1.Cursor = System.Windows.Forms.Cursors.Default
        Me.Label1.ForeColor = System.Drawing.SystemColors.ControlText
        Me.Label1.Location = New System.Drawing.Point(13, 18)
        Me.Label1.Name = "Label1"
        Me.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Label1.Size = New System.Drawing.Size(75, 13)
        Me.Label1.TabIndex = 56
        Me.Label1.Text = "Device Name:"
        '
        'btnCntEvent
        '
        Me.btnCntEvent.BackColor = System.Drawing.SystemColors.Control
        Me.btnCntEvent.Cursor = System.Windows.Forms.Cursors.Default
        Me.btnCntEvent.ForeColor = System.Drawing.SystemColors.ControlText
        Me.btnCntEvent.Location = New System.Drawing.Point(306, 133)
        Me.btnCntEvent.Name = "btnCntEvent"
        Me.btnCntEvent.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.btnCntEvent.Size = New System.Drawing.Size(105, 28)
        Me.btnCntEvent.TabIndex = 60
        Me.btnCntEvent.Text = "Event..."
        Me.btnCntEvent.UseVisualStyleBackColor = False
        '
        'timer1
        '
        '
        'frmCounter
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(424, 416)
        Me.Controls.Add(Me.Frame2)
        Me.Controls.Add(Me.btnCntExit)
        Me.Controls.Add(Me.btnCntInit)
        Me.Controls.Add(Me.btnCntNotify)
        Me.Controls.Add(Me.btnCntMode)
        Me.Controls.Add(Me.btnCntStart)
        Me.Controls.Add(Me.Label6)
        Me.Controls.Add(Me.btnExit)
        Me.Controls.Add(Me.txtRet)
        Me.Controls.Add(Me.btnCntStop)
        Me.Controls.Add(Me.btnCntPreset)
        Me.Controls.Add(Me.Frame1)
        Me.Controls.Add(Me.btnCntReset)
        Me.Controls.Add(Me.txtDeviceName)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.btnCntEvent)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "frmCounter"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Counter"
        Me.Frame2.ResumeLayout(False)
        Me.Frame1.ResumeLayout(False)
        Me.Frame1.PerformLayout()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region
    'Return value of function
    'ID
    Public Shared Id As Short
    Public Shared ChannelNum As Short
    Public Shared hMainWnd As IntPtr
    Const CHANNEL_NUM As Short = 4

    Dim CountMatch(CHANNEL_NUM) As UInteger
    Dim gMode As New frmMode()
    Dim g_Event As New frmEvent()
    Dim gNotify As New frmNotify()
    Dim txt_Count(CHANNEL_NUM) As TextBox
    Dim txt_Status(CHANNEL_NUM) As TextBox

    Private Sub frmCounter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim i As Short

        Me.Left = (Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2

        '-----------------------------------
        ' 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
        '-----------------------------------
        ' Initialize dialog display
        '-----------------------------------
        txtDeviceName.Text = "CNT000"
        '-----------------------------------
        ' For each channel
        '-----------------------------------
        For i = 0 To CHANNEL_NUM - 1
            '-----------------
            ' Count value control
            '-----------------
            txt_Count(i).Text = ""
            '-----------------
            ' Count status control
            '-----------------
            txt_Status(i).Text = ""
        Next i
        '-----------------------------------
        ' Initializing Parameters
        '-----------------------------------
        ChannelNum = CHANNEL_NUM
        '-----------------------------------
        ' Copy window's handle
        '-----------------------------------
        hMainWnd = Me.Handle

    End Sub

    '---------------------------------------------
    ' CntInit()
    '---------------------------------------------
    Private Sub btnCntInit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCntInit.Click
        Dim buf As New String("", 100)
        Dim error_string As New StringBuilder("", 256)
        Dim ret As Integer
        Dim i As Short

        '------------------------------
        ' Initialization
        '------------------------------
        ret = CntInit(txtDeviceName.Text, Id)
        CntGetErrorString(ret, error_string)
        txtRet.Text = "CntInit ret = " & ret & ": " & error_string.ToString()
        
        If (ret <> CNT_ERR_SUCCESS) Then
            Exit Sub
        End If

        '------------------------------
        ' Get number of channels
        '------------------------------
        Call CntGetMaxChannels(Id, ChannelNum)

        '------------------------------
        ' If the sample control count is exceeded, 
        ' the total channel count is corrected to the control count.
        '------------------------------
        if (ChannelNum > CHANNEL_NUM) Then
            ChannelNum = CHANNEL_NUM
        End If

        '------------------------------
        ' Toggles the count value and status display according to the total number of channels.
        '------------------------------
        For i = 0 To CHANNEL_NUM - 1
            '------------------------------
            ' Disables the display when the number of loops exceeds the total number of channels.
            '------------------------------
            if (i >= ChannelNum) Then
                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
            End If
        Next i

    End Sub

    '---------------------------------------------
    ' Display Counter Mode Setting dialog
    '---------------------------------------------
    Private Sub btnCntMode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCntMode.Click
        '------------------------------
        ' Display Counter Mode Setting dialog
        '------------------------------
        gMode.Show()
    End Sub

    '---------------------------------------------
    ' Display Notification Setting dialog
    '---------------------------------------------
    Private Sub btnCntNotify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCntNotify.Click
        '------------------------------
        ' Display Notification Setting dialog
        '------------------------------
        gNotify.Show()
    End Sub

    '---------------------------------------------
    ' Display Event Setting dialog
    '---------------------------------------------
    Private Sub btnCntEvent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCntEvent.Click
        '------------------------------
        ' Display Event Setting dialog
        '------------------------------
        g_Event.Show()
    End Sub

    '---------------------------------------------
    ' Start count
    '---------------------------------------------
    Private Sub btnCntStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCntStart.Click
        Dim error_string As New StringBuilder("", 256)
        Dim ret As Integer
        Dim ChNo(CHANNEL_NUM) As Short
        Dim i As Short

        '------------------------------
        ' Set parameters
        '------------------------------
        For i = 0 To ChannelNum - 1
            ChNo(i) = i
        Next i

        '------------------------------
        ' Start count
        '------------------------------
        ret = CntStartCount(Id, ChNo, ChannelNum)
        CntGetErrorString(ret, error_string)
        txtRet.Text = "CntStartCount ret = " & ret & ": " & error_string.ToString()

        If ret = 0 Then
            '------------------------------
            ' 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.
            '------------------------------
            'CntNotifyTimer(Id, 100, Me.Handle.ToInt32)
        End If
    End Sub

    '---------------------------------------------
    ' Preset counter
    '---------------------------------------------
    Private Sub btnCntPreset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCntPreset.Click
        Dim error_string As New StringBuilder("", 256)
        Dim ret As Integer
        Dim ChNo(CHANNEL_NUM) As Short
        Dim PresetData(CHANNEL_NUM) As Integer
        Dim i As Short

        '------------------------------
        ' Set preset value
        '------------------------------
        For i = 0 To ChannelNum - 1
            ChNo(i) = i
            PresetData(i) = i * 100
        Next i

        '------------------------------
        ' Set preset
        '------------------------------
        ret = CntPreset(Id, ChNo, ChannelNum, PresetData)
        CntGetErrorString(ret, error_string)
        txtRet.Text = "CntPreset ret = " & ret & ": " & error_string.ToString()
    End Sub

    '---------------------------------------------
    ' Reset counter
    '---------------------------------------------
    Private Sub btnCntReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCntReset.Click
        Dim error_string As New StringBuilder("", 256)
        Dim ret As Integer
        Dim ChNo(CHANNEL_NUM) As Short
        Dim i As Short

        '------------------------------
        ' Set parameters
        '------------------------------
        For i = 0 To ChannelNum - 1
            ChNo(i) = i
        Next i

        '------------------------------
        ' Reset counter
        '------------------------------
        ret = CntZeroClearCount(Id, ChNo, ChannelNum)
        CntGetErrorString(ret, error_string)
        txtRet.Text = "CntZeroClearCount ret = " & ret & ": " & error_string.ToString()
    End Sub

    '---------------------------------------------
    ' Stop count
    '---------------------------------------------
    Private Sub btnCntStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCntStop.Click
        Dim error_string As New StringBuilder("", 256)
        Dim ret As Integer
        Dim ChNo(CHANNEL_NUM) As Short
        Dim i As Short

        '------------------------------
        ' Set parameters
        '------------------------------
        For i = 0 To ChannelNum - 1
            ChNo(i) = i
        Next i

        '------------------------------
        ' Stop counter
        '------------------------------
        ret = CntStopCount(Id, ChNo, ChannelNum)
        CntGetErrorString(ret, error_string)
        txtRet.Text = "CntStopCount ret = " & ret & ": " & error_string.ToString()

        '------------------------------
        ' 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.
        '------------------------------
        'CntStopNotifyTimer(Id)

    End Sub

    '---------------------------------------------
    ' CntExit()
    '---------------------------------------------
    Private Sub btnCntExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCntExit.Click
        Dim error_string As New StringBuilder("", 256)
        Dim ret As Integer

        '------------------------------
        ' 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.
        '------------------------------
        'CntStopNotifyTimer(Id)

        '------------------------------
        ' Counter termination processing
        '------------------------------
        ret = CntExit(Id)
        CntGetErrorString(ret, error_string)
        txtRet.Text = "CntExit ret = " & ret & ": " & error_string.ToString()
    End Sub

    '---------------------------------------------
    ' Exit sample program
    '---------------------------------------------
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Dim ret As Integer

        '------------------------------
        ' 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.
        '------------------------------
        'CntStopNotifyTimer(Id)

        '------------------------------
        ' Counter termination processing
        '------------------------------
        ret = CntExit(Id)
        End
    End Sub

    '------------------------------
    ' 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 Sub timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer1.Tick

        Dim ChNo(CHANNEL_NUM) As Short
        Dim CntData(CHANNEL_NUM) As Integer
        Dim StatusData(CHANNEL_NUM) As Integer
        Dim i As Short
        Dim ret As Integer
        Dim error_string As New StringBuilder("", 256)

        For i = 0 To ChannelNum - 1
            ChNo(i) = i
            CntData(i) = 0
            StatusData(i) = 0
        Next i
        '------------------------------
        ' Get count value
        '------------------------------
        ret = CntReadCount(Id, ChNo, ChannelNum, CntData)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            txtRet.Text = String.Format("CntReadCnt ret = {0:D}: " & error_string.ToString(), ret)
            '------------------------------
            ' Stop timer
            '------------------------------
            timer1.Stop()
        End If
        '------------------------------
        ' Get count status
        '------------------------------
        For i = 0 To ChannelNum - 1
            ret = CntReadStatusEx(Id, i, StatusData(i))
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                txtRet.Text = String.Format("CntReadStatus ret = {0:D}: " & error_string.ToString(), ret)
                '------------------------------
                ' Stop timer
                '------------------------------
                timer1.Stop()
            End If
        Next i

        '------------------------------
        ' Display data
        '------------------------------
        For i = 0 To ChannelNum - 1
            ' In order to display the count data to unsigned, convert it once through hexadecimal characters
            txt_Count(i).Text = String.Format("{0:D}", Convert.ToUInt32(Hex(CntData(i)), 16))
            txt_Status(i).Text = String.Format("{0:X8}H", StatusData(i))
        Next i

    End Sub

    '---------------------------------------------
    ' Counter event notification processing
    '---------------------------------------------
    Protected Overrides Sub WndProc(ByRef m As Message)

        Dim CntData(CHANNEL_NUM) As Integer
        Dim StatusData(CHANNEL_NUM) As Integer
        Dim error_string As New StringBuilder("", 256)

        Select Case m.Msg

            Case CNTM_COUNTUP_CH0 ' Count match: 0CH
                CountMatch(0) = CountMatch(0) + 1
                If HiLoWord(m.WParam.ToInt32, 1) = 1 Then
                    lblNotify.Text = String.Format("Count Match! Channel No.: 0, UP, " & vbCrLf & "Count: {0:X8}(Hex) Matched: {1:D}times", Hex(Val(m.LParam.ToString)), CountMatch(0))
                Else
                    lblNotify.Text = String.Format("Count Match! Channel No.: 0, DOWN, " & vbCrLf & "Count: {0:X8}(Hex) Matched: {1:D}times", Hex(Val(m.LParam.ToString)), CountMatch(0))
                End If

            Case CNTM_COUNTUP_CH1 ' Count match: 1CH
                CountMatch(1) = CountMatch(1) + 1
                If HiLoWord(m.WParam.ToInt32, 1) = 1 Then
                    lblNotify.Text = String.Format("Count Match! Channel No.: 1, UP, " & vbCrLf & "Count: {0:X8}(Hex) Matched: {1:D}times", Hex(Val(m.LParam.ToString)), CountMatch(1))
                Else
                    lblNotify.Text = String.Format("Count Match! Channel No.: 1, DOWN, " & vbCrLf & "Count: {0:X8}(Hex) Matched: {1:D}times", Hex(Val(m.LParam.ToString)), CountMatch(1))
                End If

            Case CNTM_COUNTUP_CH2 ' Count match: 2CH
                CountMatch(2) = CountMatch(2) + 1
                If HiLoWord(m.WParam.ToInt32, 1) = 1 Then
                    lblNotify.Text = String.Format("Count Match! Channel No.: 2, UP, " & vbCrLf & "Count: {0:X8}(Hex) Matched: {1:D}times", Hex(Val(m.LParam.ToString)), CountMatch(2))
                Else
                    lblNotify.Text = String.Format("Count Match! Channel No.: 2, DOWN, " & vbCrLf & "Count: {0:X8}(Hex) Matched: {1:D}times", Hex(Val(m.LParam.ToString)), CountMatch(2))
                End If

            Case CNTM_COUNTUP_CH3 ' Count match: 3CH
                CountMatch(3) = CountMatch(3) + 1
                If HiLoWord(m.WParam.ToInt32, 1) = 1 Then
                    lblNotify.Text = String.Format("Count Match! Channel No.: 3, UP, " & vbCrLf & "Count: {0:X8}(Hex) Matched: {1:D}times", Hex(Val(m.LParam.ToString)), CountMatch(3))
                Else
                    lblNotify.Text = String.Format("Count Match! Channel No.: 3, DOWN, " & vbCrLf & "Count: {0:X8}(Hex) Matched: {1:D}times", Hex(Val(m.LParam.ToString)), CountMatch(3))
                End If

            Case CNTM_COUNTER_ERROR ' Counter error
                Select Case HiLoWord(m.LParam.ToInt32, 0)
                    Case CNT_AI_ERROR  ' Abnormal input
                        lblNotify.Text = String.Format("Abnormal input error! Channel No.: {0:D}", HiLoWord(m.WParam.ToInt32, 1))
                    Case CNT_FLT_ERROR ' Filter
                        lblNotify.Text = String.Format("Filter error! Channel No.: {0:D}", HiLoWord(m.WParam.ToInt32, 1))
                    Case CNT_ALM_ERROR ' Alarm
                        lblNotify.Text = String.Format("Alarm error! Channel No.: {0:D}", HiLoWord(m.WParam.ToInt32, 1))
                End Select
            Case CNTM_CARRY_BORROW ' Carry/Borrow
                Select Case HiLoWord(m.LParam.ToInt32, 0)
                    Case CNT_CB_CARRY  ' Carry
                        lblNotify.Text = String.Format("Carry! Channel No.: {0:D}", HiLoWord(m.WParam.ToInt32, 1))
                    Case CNT_CB_BORROW ' Borrow
                        lblNotify.Text = String.Format("Borrow! Channel No.: {0:D}", HiLoWord(m.WParam.ToInt32, 1))
                End Select

            '------------------------------
            ' When using the timer notification event of device,
            ' please use the following message.
            '------------------------------
            'Case CNTM_TIMER    ' Timer notification
            '    Dim ChNo(CHANNEL_NUM) As Short
            '    Dim i As Short
            '    Dim ret As Integer
            '    '------------------------------
            '    ' Set parameters
            '    '------------------------------
            '    For i = 0 To CHANNEL_NUM - 1
            '        ChNo(i) = i
            '        CntData(i) = 0
            '        StatusData(i) = 0
            '    Next i
            '    ChNum = CHANNEL_NUM
            '    '------------------------------
            '    ' Get count value
            '    '------------------------------
            '    ret = CntReadCount(Id, ChNo, ChNum, CntData)
            '    If ret <> 0 Then
            '        CntGetErrorString(ret, error_string)
            '        txtRet.Text = String.Format("CntReadCnt ret = {0:D}: " & error_string.ToString(), ret)
            '        CntStopNotifyTimer(Id)
            '    End If
            '    For i = 0 To CHANNEL_NUM - 1
            '        ret = CntReadStatusEx(Id, i, StatusData(i))
            '        If ret <> 0 Then
            '            CntGetErrorString(ret, error_string)
            '            txtRet.Text = String.Format("CntReadStatus ret = {0:D}: " & error_string.ToString(), ret)
            '            CntStopNotifyTimer(Id)
            '        End If
            '    Next i

            '    '------------------------------
            '    ' Display data
            '    '------------------------------
            '    For i = 0 To ChannelNum - 1
            '        ' In order to display the count data to unsigned, convert it once through hexadecimal characters
            '        txt_Count(i).Text = String.Format("{0:D}", Convert.ToUInt32(Hex(CntData(i)), 16))
            '        txt_Status(i).Text = String.Format("{0:X8}H", StatusData(i))
            '    Next i
        End Select
        MyBase.WndProc(m)
    End Sub

    '---------------------------------------------------------
    ' Subroutine: Get HIWORD, LOWORD
    '---------------------------------------------------------
    Function HiLoWord(ByRef Number As Integer, ByRef HiLo As Short) As Integer

        Dim Hi As Integer
        Dim Lo As Integer
        Dim StrData As String
        Dim i As Short

        StrData = Hex(Number)
        If Len(StrData) < 8 Then
            For i = 1 To 8 - Len(StrData)
                StrData = "0" & StrData
            Next i
        End If

        Lo = Val("&h" & VB.Right(StrData, 4))
        Hi = Val("&h" & VB.Left(StrData, 4))
        If HiLo = 0 Then
            HiLoWord = Lo
        ElseIf HiLo = 1 Then
            HiLoWord = Hi
        End If

    End Function
End Class
