﻿Option Strict Off
Option Explicit On
Imports System.Text

Public Class Form1
    Dim Id As Short ' デバイスID
    Const MAX_SAMPLES As Integer = 1000 ' 最大サンプリング数
    Dim Data(MAX_SAMPLES) As Single ' 取得データの配列
    Dim Samples As Integer  ' 取得データ数

    '==================================
    ' フォームのロード
    '==================================
    Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        '----------------------------------
        ' 入力レンジコンボボックスの初期化
        '----------------------------------
        ComboRange.Items.Add(("-20.0 - +20.0V"))
        ComboRange.Items.Add(("-10.0 - +10.0V"))
        ComboRange.Items.Add(("-5.0 - +5.0V"))
        ComboRange.Items.Add(("-2.0 - +2.0V"))
        ComboRange.Items.Add(("-1.0 - +1.0V"))
        ComboRange.Items.Add(("-0.5 - + 0.5V"))
        ComboRange.Items.Add(("-0.2 - +0.2V"))
        ComboRange.Items.Add(("-0.1 - +0.1V"))
        ComboRange.Items.Add(("-0.05 - +0.05V"))
        ComboRange.Items.Add(("-0.02 - +0.02V"))

        Samples = 0
    End Sub

    '==================================
    ' 初期化処理
    '==================================
    Private Sub CommandInit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandInit.Click
        Dim Ret As Integer     ' 戻り値
        Dim Ret2 As Integer    ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)      ' エラー文字列

        '----------------------------------
        ' 初期化処理
        '----------------------------------
        Ret = DigInit(TextDevice.Text, Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigInit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' デバイスのリセット
        '----------------------------------
        Ret = DigResetDevice(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigResetDevice = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo.Text = "初期化処理 : 正常終了"
    End Sub

    '==================================
    ' 入力レンジの設定
    '==================================
    Private Sub ComboRange_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles ComboRange.SelectedIndexChanged
        Dim Range As Single
        Dim Index As Short
        Index = ComboRange.SelectedIndex
        Select Case Index
            Case 0 : Range = DIG_RANGE_40VPP
            Case 1 : Range = DIG_RANGE_20VPP
            Case 2 : Range = DIG_RANGE_10VPP
            Case 3 : Range = DIG_RANGE_4VPP
            Case 4 : Range = DIG_RANGE_2VPP
            Case 5 : Range = DIG_RANGE_1VPP
            Case 6 : Range = DIG_RANGE_400MVPP
            Case 7 : Range = DIG_RANGE_200MVPP
            Case 8 : Range = DIG_RANGE_100MVPP
            Case 9 : Range = DIG_RANGE_40MVPP
        End Select

        '----------------------------------
        ' チャネル0の入力レンジ設定
        '----------------------------------
        Dim Ret As Integer        ' 戻り値
        Dim Ret2 As Integer       ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)         ' エラー文字列

        Ret = DigSetRange(Id, DIG_CHANNEL0, Range)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetRange = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo.Text = "入力レンジ設定 : 正常終了"
    End Sub

    '==================================
    ' 収集開始
    '==================================
    Private Sub CommandStart_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandStart.Click
        Dim Ret As Integer        ' 戻り値
        Dim Ret2 As Integer       ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)         ' エラー文字列

        '----------------------------------
        ' 変数を初期化
        '----------------------------------
        Samples = 0
        ListData.Items.Clear()

        '----------------------------------
        ' 動作モード設定：データロガーモード
        '----------------------------------
        Ret = DigSetDigitizerMode(Id, DIG_MODE_LOGGER)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetDigitizerMode = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' サンプリングクロックソース設定：ボード内水晶発振機
        '----------------------------------
        Ret = DigSetClockSource(Id, DIG_ECU_SRC_CLOCK)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetClockSource = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' サンプリングクロック設定：1000usec (設定値 * 10 + 10(nsec))
        '----------------------------------
        Ret = DigSetSampleClock(Id, 99999)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetSampleClock = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' レコード収集回数設定：1回
        '----------------------------------
        Ret = DigSetRecordNumber(Id, 1)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetRecordNumber = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' プリトリガサンプル数設定：1000サンプル
        '----------------------------------
        Ret = DigSetPreSampleTimes(Id, MAX_SAMPLES)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetPreSampleTimes = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' ポストトリガサンプル数設定：なし
        '----------------------------------
        Ret = DigSetPostSampleTimes(Id, 0)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetPostSampleTimes = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' チャネル設定：0チャネルのみ有効
        '----------------------------------
        ' 0チャネルを有効にする
        Ret = DigSetChannel(Id, DIG_CHANNEL0, DIG_ENABLE)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetChannel = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        ' 1チャネルを無効にする
        Ret = DigSetChannel(Id, DIG_CHANNEL1, DIG_DISABLE)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetChannel = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' プリトリガ種類の設定：自動
        '----------------------------------
        Ret = DigSetPreTriggerType(Id, DIG_TRIGGERTYPE_AUTO)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetPreTriggerType = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' 動作開始
        '----------------------------------
        Ret = DigStart(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigStart = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextInfo.Text = "収集開始 : 正常終了"

        '----------------------------------
        ' 動作中ステータスがOFFになるまでポーリング
        '----------------------------------
        Dim Status As Integer

        Do
            System.Windows.Forms.Application.DoEvents()          ' ダイアログ操作を有効にするための処理
            '----------------------------------
            ' ドライバメモリ内サンプル数取得
            '----------------------------------
            Ret = DigGetDriverMemorySamples(Id, Samples)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigGetDriverMemorySamples = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If
            LabelCount.Text = CStr(Samples)

            '----------------------------------
            ' ステータスの取得
            '----------------------------------
            Ret = DigGetStatus(Id, Status)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigGetStatus = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If
            LabelStatus.Text = Hex(Status) & "H"
        Loop While (Status And DIG_STATUS_BUSY) = DIG_STATUS_BUSY

        '----------------------------------
        ' 動作停止後にドライバメモリ内に格納されているサンプル数取得
        '----------------------------------
        Ret = DigGetDriverMemorySamples(Id, Samples)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetDriverMemorySamples = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' 収集データ取得
        '----------------------------------
        Dim TriggerPoint As Integer       ' ポストトリガ成立後の先頭データ位置
        If Samples > 0 Then
            ' 取得サンプリング数がデータ格納配列サイズを超えないよう調整
            If Samples > MAX_SAMPLES Then
                Samples = MAX_SAMPLES
            End If
            Ret = DigReadSamples(Id, Samples, Data, TriggerPoint)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigReadSamples = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If
        End If
        '----------------------------------
        ' 取得データ表示
        '----------------------------------
        Dim i As Integer
        For i = 0 To Samples - 1
            ListData.Items.Add(i + 1 & "           " & String.Format("{0:F3}", Data(i)))
        Next i

    End Sub

    '==================================
    ' 状態取得
    '==================================
    Private Sub CommandInfo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandInfo.Click
        Dim Ret As Integer        ' 戻り値
        Dim Ret2 As Integer       ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)         ' エラー文字列
        Dim Count As Integer          ' サンプリング数
        Dim Status As Integer         ' ステータス

        '----------------------------------
        ' ドライバメモリ内サンプル数取得
        '----------------------------------
        Ret = DigGetDriverMemorySamples(Id, Count)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetDriverMemorySamples = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        LabelCount.Text = CStr(Count)

        '----------------------------------
        ' ステータスの取得
        '----------------------------------
        Ret = DigGetStatus(Id, Status)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetStatus = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        LabelStatus.Text = Hex(Status) & "H"

        TextInfo.Text = "状態取得 : 正常終了"
    End Sub

    '==================================
    ' 収集停止
    '==================================
    Private Sub CommandStop_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandStop.Click
        Dim Ret As Integer        ' 戻り値
        Dim Ret2 As Integer       ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)         ' エラー文字列

        Ret = DigStop(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigExit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo.Text = "収集停止 : 正常終了"
    End Sub

    '==================================
    ' ファイル保存
    '==================================
    Private Sub CommandSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandSave.Click
        ' データが存在しない場合、エラーを返します。
        If Samples = 0 Then
            TextInfo.Text = "収集データが存在しません。"
            Exit Sub
        End If

        '----------------------------------
        ' ファイル保存
        '----------------------------------
        Dim FileName As String       ' ファイル名文字列
        FileName = "DataLogger.csv"       ' ファイル名の設定

        ' ファイルを作成します。
        ' 文字コードShift-JISでファイルを上書き
        Dim sw As New System.IO.StreamWriter(FileName, False, System.Text.Encoding.UTF8)
        Try
            Dim i As Integer
            For i = 0 To Samples - 1
                ' ファイルにデータを書き込みます。
                sw.WriteLine(String.Format("{0:F3}", Data(i)))
            Next i
        Finally
            ' ファイルを閉じます。
            sw.Close()
        End Try

        TextInfo.Text = "ファイル保存 : 正常終了"
    End Sub

    '==================================
    ' 終了処理
    '==================================
    Private Sub CommandExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandExit.Click
        Dim Ret As Integer        ' 戻り値
        Dim Ret2 As Integer       ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)         ' エラー文字列

        Ret = DigExit(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigExit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo.Text = "終了処理 : 正常終了"
    End Sub

    '==================================
    ' フォームのアンロード
    '==================================
    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        Dim Ret As Integer        ' 戻り値
        Ret = DigExit(Id)
    End Sub
End Class
