﻿Option Strict Off
Option Explicit On
Imports System.Text

Public Class Form1
    Dim Id1 As Short    ' デバイス1ID
    Dim Id2 As Short    ' デバイス2ID
    Const MAX_SAMPLES As Short = 1000   ' 最大サンプリング数
    Dim Data1(MAX_SAMPLES) As Single    ' デバイス1取得データの配列
    Dim Data2(MAX_SAMPLES) As Single    ' デバイス2取得データの配列
    Dim Samples1 As Integer ' デバイス1取得データ数
    Dim Samples2 As Integer ' デバイス2取得データ数

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

        ComboRange2.Items.Add(("-20.0 - +20.0V"))
        ComboRange2.Items.Add(("-10.0 - +10.0V"))
        ComboRange2.Items.Add(("-5.0 - +5.0V"))
        ComboRange2.Items.Add(("-2.0 - +2.0V"))
        ComboRange2.Items.Add(("-1.0 - +1.0V"))
        ComboRange2.Items.Add(("-0.5 - + 0.5V"))
        ComboRange2.Items.Add(("-0.2 - +0.2V"))
        ComboRange2.Items.Add(("-0.1 - +0.1V"))
        ComboRange2.Items.Add(("-0.05 - +0.05V"))
        ComboRange2.Items.Add(("-0.02 - +0.02V"))

        '----------------------------------
        ' 収集サンプル数の初期化
        '----------------------------------
        Samples1 = 0
        Samples2 = 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)     ' エラー文字列

        '-----------------------------------------------------
        ' デバイス1
        '-----------------------------------------------------
        '----------------------------------
        ' 初期化処理
        '----------------------------------
        Ret = DigInit(TextDevice1.Text, Id1)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo1.Text = "DigInit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

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

        TextInfo1.Text = "初期化処理 : 正常終了"

        '-----------------------------------------------------
        ' デバイス2
        '-----------------------------------------------------
        '----------------------------------
        ' 初期化処理
        '----------------------------------
        Ret = DigInit(TextDevice2.Text, Id2)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo2.Text = "DigInit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

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

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

    '==================================
    ' デバイス1入力レンジの設定
    '==================================
    Private Sub ComboRange1_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles ComboRange1.SelectedIndexChanged
        Dim Range As Single
        Dim Index As Short
        Index = ComboRange1.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(Id1, DIG_CHANNEL0, Range)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo1.Text = "DigSetRange = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

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

    '==================================
    ' デバイス2入力レンジの設定
    '==================================
    Private Sub ComboRange2_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles ComboRange2.SelectedIndexChanged
        Dim Range As Single
        Dim Index As Short
        Index = ComboRange2.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(Id2, DIG_CHANNEL0, Range)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo2.Text = "DigSetRange = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo2.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)     ' エラー文字列

        '----------------------------------
        ' 変数を初期化
        '----------------------------------
        Samples1 = 0
        Samples2 = 0
        ListData1.Items.Clear()
        ListData2.Items.Clear()

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

        '----------------------------------
        ' イベントコントローラユニット設定：同期バス信号線1 -プリトリガ成立
        '----------------------------------
        Ret = DigSetEcuSignal(Id1, DIG_ECU_DEST_SYNC1, DIG_ECU_SRC_PRETRG)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo1.Text = "DigSetEcuSignal = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        '----------------------------------
        ' プリトリガ種類の設定：デジタルエッジ
        '----------------------------------
        Ret = DigSetPreTriggerType(Id2, DIG_TRIGGERTYPE_DIEDGE)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo2.Text = "DigSetPreTriggerType = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' プリトリガの設定：信号源=同期バス1, 方向=無効, 回数=1回
        '----------------------------------
        Ret = DigSetPreTriggerDiEdge(Id2, DIG_ECU_SRC_SYNC1, 0, 1)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo2.Text = "DigSetPreTriggerDiEdge = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' 動作開始：デバイス2を先にスタートさせます。
        '----------------------------------
        Ret = DigStart(Id2)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo2.Text = "DigStart = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextInfo2.Text = "収集開始 : 正常終了"

        Ret = DigStart(Id1)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo1.Text = "DigStart = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextInfo1.Text = "収集開始 : 正常終了"

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

        Do
            System.Windows.Forms.Application.DoEvents()          ' ダイアログ操作を有効にするための処理

            '---------------------------------------
            ' デバイス1
            '---------------------------------------
            '----------------------------------
            ' ドライバメモリ内サンプル数取得
            '----------------------------------
            Ret = DigGetDriverMemorySamples(Id1, Samples1)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo1.Text = "DigGetDriverMemorySamples = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If
            LabelCount1.Text = CStr(Samples1)

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

            '---------------------------------------
            ' デバイス2
            '---------------------------------------
            '----------------------------------
            ' ドライバメモリ内サンプル数取得
            '----------------------------------
            Ret = DigGetDriverMemorySamples(Id2, Samples2)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo2.Text = "DigGetDriverMemorySamples = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If
            LabelCount2.Text = CStr(Samples2)

            '----------------------------------
            ' ステータスの取得
            '----------------------------------
            Ret = DigGetStatus(Id2, Status2)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo2.Text = "DigGetStatus = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If
            LabelStatus2.Text = Hex(Status2) & "H"
        Loop While ((Status1 Or Status2) And DIG_STATUS_BUSY) = DIG_STATUS_BUSY

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

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


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

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

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

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

        TextInfo1.Text = "状態取得 : 正常終了"

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

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

        TextInfo2.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)     ' エラー文字列

        '---------------------------------------
        ' デバイス1
        '---------------------------------------
        Ret = DigStop(Id1)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo1.Text = "DigExit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo1.Text = "収集停止 : 正常終了"

        '---------------------------------------
        ' デバイス2
        '---------------------------------------
        Ret = DigStop(Id2)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo2.Text = "DigExit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo2.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)     ' エラー文字列

        '---------------------------------------
        ' デバイス1
        '---------------------------------------
        Ret = DigExit(Id1)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo1.Text = "DigExit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo1.Text = "終了処理 : 正常終了"

        '---------------------------------------
        ' デバイス2
        '---------------------------------------
        Ret = DigExit(Id2)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo2.Text = "DigExit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

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

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