﻿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 フォーム デザイナで生成されたコード "

	Public Sub New()
		MyBase.New()

		' この呼び出しは Windows フォーム デザイナで必要です。
		InitializeComponent()

		' InitializeComponent() 呼び出しの後に初期化を追加します。

	End Sub

	' Form は dispose をオーバーライドしてコンポーネント一覧を消去します。
	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

	' Windows フォーム デザイナで必要です。
	Private components As System.ComponentModel.IContainer

	' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
	' Windows フォーム デザイナを使って変更してください。  
	' コード エディタは使用しないでください。
	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(352, 272)
		Me.btnExit.Name = "btnExit"
		Me.btnExit.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnExit.Size = New System.Drawing.Size(89, 23)
		Me.btnExit.TabIndex = 34
		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(352, 243)
		Me.btnCntExit.Name = "btnCntExit"
		Me.btnCntExit.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnCntExit.Size = New System.Drawing.Size(89, 23)
		Me.btnCntExit.TabIndex = 33
		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(352, 214)
		Me.btnStatus.Name = "btnStatus"
		Me.btnStatus.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnStatus.Size = New System.Drawing.Size(89, 23)
		Me.btnStatus.TabIndex = 32
		Me.btnStatus.Text = "転送ステータス"
		'
		'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(352, 184)
		Me.btnCount.Name = "btnCount"
		Me.btnCount.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnCount.Size = New System.Drawing.Size(89, 23)
		Me.btnCount.TabIndex = 31
		Me.btnCount.Text = "転送カウント"
		'
		'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(352, 155)
		Me.btnData.Name = "btnData"
		Me.btnData.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnData.Size = New System.Drawing.Size(89, 23)
		Me.btnData.TabIndex = 30
		Me.btnData.Text = "取得データ..."
		'
		'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(352, 126)
		Me.btnStop.Name = "btnStop"
		Me.btnStop.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnStop.Size = New System.Drawing.Size(89, 23)
		Me.btnStop.TabIndex = 29
		Me.btnStop.Text = "転送ストップ"
		'
		'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(352, 96)
		Me.btnStart.Name = "btnStart"
		Me.btnStart.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnStart.Size = New System.Drawing.Size(89, 23)
		Me.btnStart.TabIndex = 28
		Me.btnStart.Text = "転送スタート"
		'
		'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(352, 67)
		Me.btnCondition.Name = "btnCondition"
		Me.btnCondition.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnCondition.Size = New System.Drawing.Size(89, 23)
		Me.btnCondition.TabIndex = 27
		Me.btnCondition.Text = "条件設定..."
		'
		'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(352, 38)
		Me.btnCntMode.Name = "btnCntMode"
		Me.btnCntMode.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnCntMode.Size = New System.Drawing.Size(89, 23)
		Me.btnCntMode.TabIndex = 26
		Me.btnCntMode.Text = "カウンタモード"
		'
		'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(352, 8)
		Me.btnCntInit.Name = "btnCntInit"
		Me.btnCntInit.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.btnCntInit.Size = New System.Drawing.Size(89, 23)
		Me.btnCntInit.TabIndex = 25
		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(80, 275)
		Me.txtReturn.MaxLength = 0
		Me.txtReturn.Name = "txtReturn"
		Me.txtReturn.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.txtReturn.Size = New System.Drawing.Size(249, 18)
		Me.txtReturn.TabIndex = 39
		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(80, 250)
		Me.txtError.MaxLength = 0
		Me.txtError.Name = "txtError"
		Me.txtError.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.txtError.Size = New System.Drawing.Size(249, 18)
		Me.txtError.TabIndex = 38
		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(80, 226)
		Me.txtStatus.MaxLength = 0
		Me.txtStatus.Name = "txtStatus"
		Me.txtStatus.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.txtStatus.Size = New System.Drawing.Size(249, 18)
		Me.txtStatus.TabIndex = 37
		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(80, 202)
		Me.txtTransCount.MaxLength = 0
		Me.txtTransCount.Name = "txtTransCount"
		Me.txtTransCount.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.txtTransCount.Size = New System.Drawing.Size(249, 18)
		Me.txtTransCount.TabIndex = 36
		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(80, 11)
		Me.txtDeviceName.MaxLength = 0
		Me.txtDeviceName.Name = "txtDeviceName"
		Me.txtDeviceName.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.txtDeviceName.Size = New System.Drawing.Size(121, 18)
		Me.txtDeviceName.TabIndex = 35
		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, 278)
		Me.Label5.Name = "Label5"
		Me.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.Label5.Size = New System.Drawing.Size(43, 12)
		Me.Label5.TabIndex = 44
		Me.Label5.Text = "戻り値："
		'
		'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, 253)
		Me.Label4.Name = "Label4"
		Me.Label4.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.Label4.Size = New System.Drawing.Size(39, 12)
		Me.Label4.TabIndex = 43
		Me.Label4.Text = "エラー："
		'
		'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, 229)
		Me.Label3.Name = "Label3"
		Me.Label3.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.Label3.Size = New System.Drawing.Size(58, 12)
		Me.Label3.TabIndex = 42
		Me.Label3.Text = "ステータス："
		'
		'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, 205)
		Me.Label2.Name = "Label2"
		Me.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.Label2.Size = New System.Drawing.Size(71, 12)
		Me.Label2.TabIndex = 41
		Me.Label2.Text = "転送カウント："
		'
		'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, 14)
		Me.Label1.Name = "Label1"
		Me.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.Label1.Size = New System.Drawing.Size(62, 12)
		Me.Label1.TabIndex = 40
		Me.Label1.Text = "デバイス名："
		'
		'Sampling
		'
		Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
		Me.ClientSize = New System.Drawing.Size(449, 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 = "サンプリング"
		Me.ResumeLayout(False)

	End Sub

#End Region

	Dim SamplingData(DATA_NUM - 1, CHANNEL_NUM - 1) As UInt32	' バスマスタ用バッファ

	Dim gCh As GCHandle
	Dim pSamplingData As IntPtr

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

	'===================================================
	'	ダイアログ初期化
	'===================================================
	Private Sub Sampling_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		'-----------------------------------
		'	画面表示の初期化
		'-----------------------------------
		txtDeviceName.Text = "CNT000"

		'-----------------------------------
		' PinnedハンドルをSamplingDataに分配する
		'-----------------------------------
		If gCh.IsAllocated = False Then
			gCh = GCHandle.Alloc(SamplingData, GCHandleType.Pinned)
		End If

	End Sub

	'===================================================
	'	終了時処理
	'===================================================
	Private Sub Sampling_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed

		Dim ret As Integer

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

		'-----------------------------------
		'	終了処理
		'-----------------------------------
		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

		'-----------------------------------
		'	デバイス名取得
		'-----------------------------------
		buf = txtDeviceName.Text
		'-----------------------------------
		'	初期化処理
		'-----------------------------------
		ret = CntInit(buf, Id)
		CntGetErrorString(ret, error_string)
		str = String.Format("CntInit ret = {0:d}: {1}", ret, error_string)
		txtReturn.Text = str

	End Sub

	'---------------------------------------------
	' カウンタモード設定画面表示
	'---------------------------------------------
	Private Sub btnCntMode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCntMode.Click

		'------------------------------
		' カウンタモード設定画面表示
		'------------------------------
		dlgMode.Show()

	End Sub

	'===================================================
	'	条件設定
	'===================================================
	Private Sub btnCondition_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCondition.Click

		'-----------------------------------
		'	条件設定ダイアログを開く
		'-----------------------------------
		dlgCondition.Show()

	End Sub

	'===================================================
	'	転送スタート
	'===================================================
	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

		'-----------------------------------
		' メッセージ通知設定
		'-----------------------------------
		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
		'-----------------------------------
		'	FIFOのリセット(入力)
		'-----------------------------------
		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
		'-----------------------------------
		'	データバッファを設定
		'-----------------------------------
		Array.Clear(SamplingData, 0, SamplingData.Length)
		'-----------------------------------
		' Pinnedハンドルでオブジェクトのアドレスを取得
		'-----------------------------------
		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
		'------------------------------
		' パラメータのセット
		'------------------------------
		For i = 0 To CHANNEL_NUM - 1
			ChNo(i) = i
		Next i
		'------------------------------
		' カウント開始処理
		'------------------------------
        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
		'-----------------------------------
		'	転送スタート
		'-----------------------------------
		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

	'===================================================
	'	転送ストップ
	'===================================================
	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

		'-----------------------------------
		'	転送ストップ
		'-----------------------------------
		ret = CntSamplingStop(Id)
		CntGetErrorString(ret, error_string)
		str = String.Format("CntSamplingStop ret = {0:d}: {1}", ret, error_string)
		txtReturn.Text = str

	End Sub

	'===================================================
	'	データファイル表示
	'===================================================
	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"

		'-----------------------------------
		' ファイルに転送されたデータを書き込みます
		'-----------------------------------
		'-----------------------------------
		' ファイルを開く
		'-----------------------------------
		Try
			fp = File.CreateText(file_name)
		Catch ex As Exception
			txtReturn.Text = "fopen : file open error."
			Exit Sub
		End Try

		'-----------------------------------
		' データ書き込み
		'-----------------------------------
		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
		'-----------------------------------
		' ファイルを閉じる
		'-----------------------------------
		fp.Close()

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

	End Sub

	'===================================================
	'	転送カウント
	'===================================================
	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

		'-----------------------------------
		' 転送カウント取得
		'-----------------------------------
		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
			' 転送カウント値を符号無しで表示するため、一度16進文字例変換を経由
			str = String.Format("{0:d}", Convert.ToUInt32(Hex(count), 16))
			txtTransCount.Text = str
		End If

	End Sub

	'===================================================
	'	転送ステータス
	'===================================================
	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

		'-----------------------------------
		' 転送ステータス取得
		'-----------------------------------
		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

	'===================================================
	' カウンタ イベント通知処理
	'===================================================
	Protected Overrides Sub WndProc(ByRef m As Message)

		Select Case m.Msg
			Case CNTM_SAMPLING_COUNT			' 指定個数転送完了
				btnCount_Click(btnCount, New System.EventArgs())
			Case CNTM_SAMPLING_STOP			 ' 転送完了
				txtStatus.Text = "Stopped!"
				btnCount_Click(btnCount, New System.EventArgs())
		End Select

		MyBase.WndProc(m)

	End Sub
End Class
