﻿Option Strict Off
Option Explicit On 
Imports System.Runtime.InteropServices
Imports System.IO
Imports System.Text

Public Class Sampling
	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 btnExit As System.Windows.Forms.Button
	Public WithEvents btnCntExit As System.Windows.Forms.Button
	Public WithEvents btnStatus As System.Windows.Forms.Button
	Public WithEvents btnCount As System.Windows.Forms.Button
	Public WithEvents btnData As System.Windows.Forms.Button
	Public WithEvents btnStop As System.Windows.Forms.Button
	Public WithEvents btnStart As System.Windows.Forms.Button
	Public WithEvents btnCondition As System.Windows.Forms.Button
	Public WithEvents btnCntMode As System.Windows.Forms.Button
	Public WithEvents btnCntInit As System.Windows.Forms.Button
	Public WithEvents txtReturn As System.Windows.Forms.TextBox
	Public WithEvents txtError As System.Windows.Forms.TextBox
	Public WithEvents txtStatus As System.Windows.Forms.TextBox
	Public WithEvents txtTransCount As System.Windows.Forms.TextBox
	Public WithEvents txtDeviceName As System.Windows.Forms.TextBox
	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 Label2 As System.Windows.Forms.Label
	Public WithEvents Label1 As System.Windows.Forms.Label
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.btnExit = New System.Windows.Forms.Button()
		Me.btnCntExit = New System.Windows.Forms.Button()
		Me.btnStatus = New System.Windows.Forms.Button()
		Me.btnCount = New System.Windows.Forms.Button()
		Me.btnData = New System.Windows.Forms.Button()
		Me.btnStop = New System.Windows.Forms.Button()
		Me.btnStart = New System.Windows.Forms.Button()
		Me.btnCondition = New System.Windows.Forms.Button()
		Me.btnCntMode = New System.Windows.Forms.Button()
		Me.btnCntInit = New System.Windows.Forms.Button()
		Me.txtReturn = New System.Windows.Forms.TextBox()
		Me.txtError = New System.Windows.Forms.TextBox()
		Me.txtStatus = New System.Windows.Forms.TextBox()
		Me.txtTransCount = New System.Windows.Forms.TextBox()
		Me.txtDeviceName = New System.Windows.Forms.TextBox()
		Me.Label5 = New System.Windows.Forms.Label()
		Me.Label4 = New System.Windows.Forms.Label()
		Me.Label3 = New System.Windows.Forms.Label()
		Me.Label2 = New System.Windows.Forms.Label()
		Me.Label1 = New System.Windows.Forms.Label()
		Me.SuspendLayout()
		'
		'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, 279)
		Me.btnExit.Name = "btnExit"
		Me.btnExit.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnExit.Size = New System.Drawing.Size(94, 22)
		Me.btnExit.TabIndex = 54
		Me.btnExit.Text = "Exit"
		'
		'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, 249)
		Me.btnCntExit.Name = "btnCntExit"
		Me.btnCntExit.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnCntExit.Size = New System.Drawing.Size(94, 22)
		Me.btnCntExit.TabIndex = 53
		Me.btnCntExit.Text = "CntExit"
		'
		'btnStatus
		'
		Me.btnStatus.BackColor = System.Drawing.SystemColors.Control
		Me.btnStatus.Cursor = System.Windows.Forms.Cursors.Default
		Me.btnStatus.ForeColor = System.Drawing.SystemColors.ControlText
		Me.btnStatus.Location = New System.Drawing.Point(306, 219)
		Me.btnStatus.Name = "btnStatus"
		Me.btnStatus.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnStatus.Size = New System.Drawing.Size(94, 22)
		Me.btnStatus.TabIndex = 52
		Me.btnStatus.Text = "Status"
		'
		'btnCount
		'
		Me.btnCount.BackColor = System.Drawing.SystemColors.Control
		Me.btnCount.Cursor = System.Windows.Forms.Cursors.Default
		Me.btnCount.ForeColor = System.Drawing.SystemColors.ControlText
		Me.btnCount.Location = New System.Drawing.Point(306, 189)
		Me.btnCount.Name = "btnCount"
		Me.btnCount.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnCount.Size = New System.Drawing.Size(94, 22)
		Me.btnCount.TabIndex = 51
		Me.btnCount.Text = "Count"
		'
		'btnData
		'
		Me.btnData.BackColor = System.Drawing.SystemColors.Control
		Me.btnData.Cursor = System.Windows.Forms.Cursors.Default
		Me.btnData.ForeColor = System.Drawing.SystemColors.ControlText
		Me.btnData.Location = New System.Drawing.Point(306, 159)
		Me.btnData.Name = "btnData"
		Me.btnData.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnData.Size = New System.Drawing.Size(94, 22)
		Me.btnData.TabIndex = 50
		Me.btnData.Text = "Data..."
		'
		'btnStop
		'
		Me.btnStop.BackColor = System.Drawing.SystemColors.Control
		Me.btnStop.Cursor = System.Windows.Forms.Cursors.Default
		Me.btnStop.ForeColor = System.Drawing.SystemColors.ControlText
		Me.btnStop.Location = New System.Drawing.Point(306, 129)
		Me.btnStop.Name = "btnStop"
		Me.btnStop.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnStop.Size = New System.Drawing.Size(94, 22)
		Me.btnStop.TabIndex = 49
		Me.btnStop.Text = "Stop"
		'
		'btnStart
		'
		Me.btnStart.BackColor = System.Drawing.SystemColors.Control
		Me.btnStart.Cursor = System.Windows.Forms.Cursors.Default
		Me.btnStart.ForeColor = System.Drawing.SystemColors.ControlText
		Me.btnStart.Location = New System.Drawing.Point(306, 99)
		Me.btnStart.Name = "btnStart"
		Me.btnStart.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnStart.Size = New System.Drawing.Size(94, 22)
		Me.btnStart.TabIndex = 48
		Me.btnStart.Text = "Start"
		'
		'btnCondition
		'
		Me.btnCondition.BackColor = System.Drawing.SystemColors.Control
		Me.btnCondition.Cursor = System.Windows.Forms.Cursors.Default
		Me.btnCondition.ForeColor = System.Drawing.SystemColors.ControlText
		Me.btnCondition.Location = New System.Drawing.Point(306, 69)
		Me.btnCondition.Name = "btnCondition"
		Me.btnCondition.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnCondition.Size = New System.Drawing.Size(94, 22)
		Me.btnCondition.TabIndex = 47
		Me.btnCondition.Text = "Condition..."
		'
		'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, 39)
		Me.btnCntMode.Name = "btnCntMode"
		Me.btnCntMode.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnCntMode.Size = New System.Drawing.Size(94, 22)
		Me.btnCntMode.TabIndex = 46
		Me.btnCntMode.Text = "Counter Mode"
		'
		'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, 9)
		Me.btnCntInit.Name = "btnCntInit"
		Me.btnCntInit.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnCntInit.Size = New System.Drawing.Size(94, 22)
		Me.btnCntInit.TabIndex = 45
		Me.btnCntInit.Text = "CntInit"
		'
		'txtReturn
		'
		Me.txtReturn.AcceptsReturn = True
		Me.txtReturn.AutoSize = False
		Me.txtReturn.BackColor = System.Drawing.SystemColors.Window
		Me.txtReturn.Cursor = System.Windows.Forms.Cursors.IBeam
		Me.txtReturn.ForeColor = System.Drawing.SystemColors.WindowText
		Me.txtReturn.Location = New System.Drawing.Point(87, 281)
		Me.txtReturn.MaxLength = 0
		Me.txtReturn.Name = "txtReturn"
		Me.txtReturn.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.txtReturn.Size = New System.Drawing.Size(204, 20)
		Me.txtReturn.TabIndex = 59
		Me.txtReturn.Text = ""
		'
		'txtError
		'
		Me.txtError.AcceptsReturn = True
		Me.txtError.AutoSize = False
		Me.txtError.BackColor = System.Drawing.SystemColors.Window
		Me.txtError.Cursor = System.Windows.Forms.Cursors.IBeam
		Me.txtError.ForeColor = System.Drawing.SystemColors.WindowText
		Me.txtError.Location = New System.Drawing.Point(87, 257)
		Me.txtError.MaxLength = 0
		Me.txtError.Name = "txtError"
		Me.txtError.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.txtError.Size = New System.Drawing.Size(204, 20)
		Me.txtError.TabIndex = 58
		Me.txtError.Text = ""
		'
		'txtStatus
		'
		Me.txtStatus.AcceptsReturn = True
		Me.txtStatus.AutoSize = False
		Me.txtStatus.BackColor = System.Drawing.SystemColors.Window
		Me.txtStatus.Cursor = System.Windows.Forms.Cursors.IBeam
		Me.txtStatus.ForeColor = System.Drawing.SystemColors.WindowText
		Me.txtStatus.Location = New System.Drawing.Point(87, 233)
		Me.txtStatus.MaxLength = 0
		Me.txtStatus.Name = "txtStatus"
		Me.txtStatus.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.txtStatus.Size = New System.Drawing.Size(204, 20)
		Me.txtStatus.TabIndex = 57
		Me.txtStatus.Text = ""
		'
		'txtTransCount
		'
		Me.txtTransCount.AcceptsReturn = True
		Me.txtTransCount.AutoSize = False
		Me.txtTransCount.BackColor = System.Drawing.SystemColors.Window
		Me.txtTransCount.Cursor = System.Windows.Forms.Cursors.IBeam
		Me.txtTransCount.ForeColor = System.Drawing.SystemColors.WindowText
		Me.txtTransCount.Location = New System.Drawing.Point(87, 209)
		Me.txtTransCount.MaxLength = 0
		Me.txtTransCount.Name = "txtTransCount"
		Me.txtTransCount.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.txtTransCount.Size = New System.Drawing.Size(204, 20)
		Me.txtTransCount.TabIndex = 56
		Me.txtTransCount.Text = ""
		'
		'txtDeviceName
		'
		Me.txtDeviceName.AcceptsReturn = True
		Me.txtDeviceName.AutoSize = False
		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(92, 10)
		Me.txtDeviceName.MaxLength = 0
		Me.txtDeviceName.Name = "txtDeviceName"
		Me.txtDeviceName.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.txtDeviceName.Size = New System.Drawing.Size(61, 20)
		Me.txtDeviceName.TabIndex = 55
		Me.txtDeviceName.Text = ""
		'
		'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, 283)
		Me.Label5.Name = "Label5"
		Me.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.Label5.Size = New System.Drawing.Size(73, 13)
		Me.Label5.TabIndex = 64
		Me.Label5.Text = "Return Value:"
		'
		'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, 259)
		Me.Label4.Name = "Label4"
		Me.Label4.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.Label4.Size = New System.Drawing.Size(32, 13)
		Me.Label4.TabIndex = 63
		Me.Label4.Text = "Error:"
		'
		'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, 235)
		Me.Label3.Name = "Label3"
		Me.Label3.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.Label3.Size = New System.Drawing.Size(39, 13)
		Me.Label3.TabIndex = 62
		Me.Label3.Text = "Status:"
		'
		'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, 211)
		Me.Label2.Name = "Label2"
		Me.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.Label2.Size = New System.Drawing.Size(38, 13)
		Me.Label2.TabIndex = 61
		Me.Label2.Text = "Count:"
		'
		'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(8, 13)
		Me.Label1.Name = "Label1"
		Me.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.Label1.Size = New System.Drawing.Size(75, 13)
		Me.Label1.TabIndex = 60
		Me.Label1.Text = "Device Name:"
		'
		'Sampling
		'
		Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
		Me.ClientSize = New System.Drawing.Size(412, 310)
		Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnExit, Me.btnCntExit, Me.btnStatus, Me.btnCount, Me.btnData, Me.btnStop, Me.btnStart, Me.btnCondition, Me.btnCntMode, Me.btnCntInit, Me.txtReturn, Me.txtError, Me.txtStatus, Me.txtTransCount, Me.txtDeviceName, Me.Label5, Me.Label4, Me.Label3, Me.Label2, Me.Label1})
		Me.MaximizeBox = False
		Me.MinimizeBox = False
		Me.Name = "Sampling"
		Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
		Me.Text = "Sampling"
		Me.ResumeLayout(False)

	End Sub

#End Region

	Dim SamplingData(DATA_NUM - 1, CHANNEL_NUM - 1) As UInt32	' Buffer for bus-master

	Dim gCh As GCHandle
	Dim pSamplingData As IntPtr

	Dim dlgMode As New ModeDlg()
	Dim dlgCondition As New ConditionDlg()

	'===================================================
	' Dialog initialization
	'===================================================
	Private Sub Sampling_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		'-----------------------------------
		' Initialize screen display
		'-----------------------------------
		txtDeviceName.Text = "CNT000"

		'-----------------------------------
		' Specify Pinned handle to SamplingData
		'-----------------------------------
		If gCh.IsAllocated = False Then
			gCh = GCHandle.Alloc(SamplingData, GCHandleType.Pinned)
		End If

	End Sub

	'===================================================
	' Termination processing
	'===================================================
	Private Sub Samplling_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed

		Dim ret As Integer

		'-----------------------------------
		' GCHandle.Free()
		'-----------------------------------
		If gCh.IsAllocated = True Then
			gCh.Free()
		End If

		'-----------------------------------
		' Termination processing
		'-----------------------------------
		ret = CntExit(Id)

	End Sub

	'===================================================
	' CntInit()
	'===================================================
	Private Sub btnCntInit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCntInit.Click

		Dim str As String
		Dim buf As New String("", 100)
        Dim error_string As New StringBuilder("", 256)
        Dim ret As Integer

        '-----------------------------------
        ' Get device name
        '-----------------------------------
        buf = txtDeviceName.Text
        '-----------------------------------
        ' Initialization
        '-----------------------------------
        ret = CntInit(buf, Id)
        CntGetErrorString(ret, error_string)
        str = String.Format("CntInit ret = {0:d}: {1}", ret, error_string)
        txtReturn.Text = str

    End Sub

    '---------------------------------------------
    ' Dispaly "Counter Mode Setting" dialog
    '---------------------------------------------
    Private Sub btnCntMode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCntMode.Click

        '------------------------------
        ' Dispaly "Counter Mode Setting" dialog
        '------------------------------
        dlgMode.Show()

    End Sub

    '===================================================
    ' Display "Infinite Sampling Condition Setting" dialog
    '===================================================
    Private Sub btnCondition_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCondition.Click

        '-----------------------------------
        ' Display "Infinite Sampling Condition Setting" dialog
        '-----------------------------------
        dlgCondition.Show()

    End Sub

    '===================================================
    ' Transfer start
    '===================================================
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

        Dim ChNo(CHANNEL_NUM - 1), i As Short
        Dim error_string As New StringBuilder("", 256)
        Dim str As String
        Dim ret As Integer

        '-----------------------------------
        ' Set message notification
        '-----------------------------------
        ret = CntNotifySamplingStop(Id, Me.Handle)
        If ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntNotifySamplingStop ret = {0:d}: {1}", ret, error_string)
            txtReturn.Text = str
            Exit Sub
        End If
        ret = CntNotifySamplingCount(Id, ConditionDlg.NumberOfData, Me.Handle)
        If ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntNotifySamplingCount ret = {0:d}: {1}", ret, error_string)
            txtReturn.Text = str
            Exit Sub
        End If
        '-----------------------------------
        ' Reset FIFO (Input)
        '-----------------------------------
        ret = CntSamplingFifoReset(Id)
        If ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntSamplingFifoReset ret = {0:d}: {1}", ret, error_string)
            txtReturn.Text = str
            Exit Sub
        End If
        '-----------------------------------
        ' Set data buffer
        '-----------------------------------
        Array.Clear(SamplingData, 0, SamplingData.Length)
        '-----------------------------------
        ' Get object address by Pinned handle
        '-----------------------------------
        pSamplingData = gCh.AddrOfPinnedObject()
        ret = CntSamplingSetBuffer(Id, pSamplingData, CHANNEL_NUM, DATA_NUM, CNTS_WRITE_ONCE)
        If ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntSamplingSetBuffer ret = {0:d}: {1}", ret, error_string)
            txtReturn.Text = str
            Exit Sub
        End If
        '------------------------------
        ' Set parameter
        '------------------------------
        For i = 0 To CHANNEL_NUM - 1
            ChNo(i) = i
        Next i
        '------------------------------
        ' Start count
        '------------------------------
        ret = CntStartCount(Id, ChNo, CHANNEL_NUM)
        If ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntStartCount ret = {0:d}: {1}", ret, error_string)
            txtReturn.Text = str
            Exit Sub
        End If
        '-----------------------------------
        ' Transfer start
        '-----------------------------------
        ret = CntSamplingStart(Id)
        If ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntSamplingStart ret = {0:d}: {1}", ret, error_string)
            txtReturn.Text = str
            Exit Sub
        End If
        txtTransCount.Text = ""
        txtStatus.Text = ""
        txtError.Text = ""

    End Sub

    '===================================================
    ' Transfer stop
    '===================================================
    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click

        Dim error_string As New StringBuilder("", 256)
        Dim str As String
        Dim ret As Integer

        '-----------------------------------
        ' Transfer stop
        '-----------------------------------
        ret = CntSamplingStop(Id)
        CntGetErrorString(ret, error_string)
        str = String.Format("CntSamplingStop ret = {0:d}: {1}", ret, error_string)
        txtReturn.Text = str

    End Sub

    '===================================================
    ' Display data file
    '===================================================
    Private Sub btnData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnData.Click

        Dim buf As New String("", 256)
        Dim i, j As Short
        Dim fp As StreamWriter
        Dim file_name As String = "SamplingData.csv"

        '-----------------------------------
        ' Write transferred data to file
        '-----------------------------------
        '-----------------------------------
        ' Open file
        '-----------------------------------
        Try
            fp = File.CreateText(file_name)
        Catch ex As Exception
            txtReturn.Text = "fopen : file open error."
            Exit Sub
        End Try

        '-----------------------------------
        ' Write data
        '-----------------------------------
        For i = 0 To DATA_NUM - 1
            buf = String.Format("{0:d8}: , ", i)
            For j = 0 To CHANNEL_NUM - 2
                buf = String.Format("{0}{1:X8}, ", buf, SamplingData(i, j))
            Next j
            buf = String.Format("{0}{1:X8}", buf, SamplingData(i, j))
            fp.WriteLine(buf)
        Next i
        '-----------------------------------
        ' Close file
        '-----------------------------------
        fp.Close()

        '-----------------------------------
        ' notepad
        '-----------------------------------
        Process.Start("notepad.exe", file_name)

    End Sub

    '===================================================
    ' Transfer count
    '===================================================
    Private Sub btnCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCount.Click

        Dim count As Integer = 0
        Dim carry As Integer = 0
        Dim error_string As New StringBuilder("", 256)
        Dim str As String
        Dim ret As Integer

        '-----------------------------------
        ' Get transfer count
        '-----------------------------------
        ret = CntGetSamplingCount(Id, count, carry)
        CntGetErrorString(ret, error_string)
        str = String.Format("CntGetSamplingCount ret = {0:d}: {1}", ret, error_string)
        txtReturn.Text = str
        If ret = CNT_ERR_SUCCESS Then
			' In order to display the count value to unsigned, convert it once through hexadecimal characters
            str = String.Format("{0:d}", Convert.ToUInt32(Hex(count), 16))
            txtTransCount.Text = str
        End If

    End Sub

    '===================================================
    ' Transfer status
    '===================================================
    Private Sub btnStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStatus.Click

        Dim status, err As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim str As String
        Dim ret As Integer

        '-----------------------------------
        ' Get transfer status
        '-----------------------------------
        ret = CntGetSamplingStatus(Id, status, err)
        CntGetErrorString(ret, error_string)
        str = String.Format("CntGetSamplingStatus ret = {0:d}: {1}", ret, error_string)
        txtReturn.Text = str
        If ret <> CNT_ERR_SUCCESS Then
            Exit Sub
        End If

        ' Status
        str = ""
        If status And CNTS_STATUS_BMSTOP Then str = str & "BmStop,"
        If status And CNTS_STATUS_CNTSTART Then str = str & "CntStart,"
        If status And CNTS_STATUS_CNTSTOP Then str = str & "CntStop,"
        If status And CNTS_STATUS_TRGIN Then str = str & "TrgIn,"
        If status And CNTS_STATUS_OVERRUN Then str = str & "Overrun"
        txtStatus.Text = str
        ' Error
        str = ""
        If err And CNTS_STATUS_FIFOFULL Then str = str & "FifoFull,"
        If err And CNTS_STATUS_SGOVERIN Then str = str & "S/GOverIn,"
        If err And CNTS_STATUS_TRGERR Then str = str & "TrgErr,"
        If err And CNTS_STATUS_CLKERR Then str = str & "ClkErr,"
        If err And CNTS_STATUS_SLAVEHALT Then str = str & "SlaveHalt,"
        If err And CNTS_STATUS_MASTERHALT Then str = str & "MasterHalt"

        txtError.Text = str

    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 str As String
        Dim ret As Integer

        '-----------------------------------
        ' CntExit
        '-----------------------------------
        ret = CntExit(Id)
        CntGetErrorString(ret, error_string)
        str = String.Format("CntExit ret = {0:d}: {1}", ret, error_string)
        txtReturn.Text = str

    End Sub

    '===================================================
    ' Exit
    '===================================================
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        Me.Close()

    End Sub

    '===================================================
    ' Processing of counter event notification
    '===================================================
    Protected Overrides Sub WndProc(ByRef m As Message)

        Select Case m.Msg
            Case CNTM_SAMPLING_COUNT  ' Stop transfer by specified times
                btnCount_Click(btnCount, New System.EventArgs())
            Case CNTM_SAMPLING_STOP   ' Transfer stopped
                txtStatus.Text = "Stopped!"
                btnCount_Click(btnCount, New System.EventArgs())
        End Select

        MyBase.WndProc(m)

    End Sub
End Class
