﻿Option Strict Off
Option Explicit On
Imports System.Text

Public Class Form1
    Dim Id1 As Short ' Device1 ID
    Dim Id2 As Short ' Device2 ID
    Const MAX_SAMPLES As Short = 1000 ' Number of the maximam samples
    Dim Data1(MAX_SAMPLES) As Single ' Array of retrieving data for Device1
    Dim Data2(MAX_SAMPLES) As Single ' Array of retrieving data for Device2
    Dim Samples1 As Integer ' Number of retrieved samples for Device1
    Dim Samples2 As Integer ' Number of retrieved samples for Device2

    '==================================
    ' Form load
    '==================================
    Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        '----------------------------------
        ' Get the input range from combobox
        '----------------------------------
        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"))

        '----------------------------------
        ' Initialize the number of acquisition samples
        '----------------------------------
        Samples1 = 0
        Samples2 = 0
    End Sub

    '==================================
    ' Init handling
    '==================================
    Private Sub CommandInit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandInit.Click
        Dim Ret As Integer    ' Return value
        Dim Ret2 As Integer   ' Return value2
        Dim ErrorString As New StringBuilder("", 256)     ' Error string

        '-----------------------------------------------------
        ' Device1
        '-----------------------------------------------------
        '----------------------------------
        ' Init handling
        '----------------------------------
        Ret = DigInit(TextDevice1.Text, Id1)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo1.Text = "DigInit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' Reset device
        '----------------------------------
        Ret = DigResetDevice(Id1)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo1.Text = "DigResetDevice = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo1.Text = "Init : Normality completion"

        '-----------------------------------------------------
        ' Device2
        '-----------------------------------------------------
        '----------------------------------
        ' Init handling
        '----------------------------------
        Ret = DigInit(TextDevice2.Text, Id2)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo2.Text = "DigInit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' Reset device
        '----------------------------------
        Ret = DigResetDevice(Id2)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo2.Text = "DigResetDevice = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo2.Text = "Init : Normality completion"
    End Sub

    '==================================
    ' Set input range for device1
    '==================================
    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

        '----------------------------------
        ' Set input range of channel0
        '----------------------------------
        Dim Ret As Integer    ' Return value
        Dim Ret2 As Integer   ' Return value2
        Dim ErrorString As New StringBuilder("", 256)     ' Error string

        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 = "Set input range : Normality completion"
    End Sub

    '==================================
    ' Set input range for device2
    '==================================
    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

        '----------------------------------
        ' Set input range of channel0
        '----------------------------------
        Dim Ret As Integer    ' Return value
        Dim Ret2 As Integer   ' Return value2
        Dim ErrorString As New StringBuilder("", 256)     ' Error string

        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 = "Set input range : Normality completion"
    End Sub

    '==================================
    ' Start acquisition
    '==================================
    Private Sub CommandStart_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandStart.Click
        Dim Ret As Integer    ' Return value
        Dim Ret2 As Integer   ' Return value2
        Dim ErrorString As New StringBuilder("", 256)     ' Error string

        '----------------------------------
        ' Initialize variable
        '----------------------------------
        Samples1 = 0
        Samples2 = 0
        ListData1.Items.Clear()
        ListData2.Items.Clear()

        '-----------------------------------------------------
        ' Set deivce1
        '-----------------------------------------------------
        '----------------------------------
        ' Set operation mode : Data logger mode
        '----------------------------------
        Ret = DigSetDigitizerMode(Id1, DIG_MODE_LOGGER)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo1.Text = "DigSetDigitizerMode = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' Set Event Controller Unit : Synchronous bus signal line 1 - pre-trigger satisfaction
        '----------------------------------
        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

        '----------------------------------
        ' Set sampling clock source : Crystal oscillator of board
        '----------------------------------
        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

        '----------------------------------
        ' Set sampling clock : 1000usec (set value * 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

        '----------------------------------
        ' Set the number of acquiring records : 1
        '----------------------------------
        Ret = DigSetRecordNumber(Id1, 1)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo1.Text = "DigSetRecordNumber = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' Set the number of pre-trigger samples : 1000
        '----------------------------------
        Ret = DigSetPreSampleTimes(Id1, MAX_SAMPLES)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo1.Text = "DigSetPreSampleTimes = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' Set the number of post-trigger samples : 0
        '----------------------------------
        Ret = DigSetPostSampleTimes(Id1, 0)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo1.Text = "DigSetPostSampleTimes = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' Set channel : Enable channel0 only
        '----------------------------------
        ' Enable channel0
        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

        ' Disable channel1
        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

        '----------------------------------
        ' Set the type of pre-trigger : Auto
        '----------------------------------
        Ret = DigSetPreTriggerType(Id1, DIG_TRIGGERTYPE_AUTO)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo1.Text = "DigSetPreTriggerType = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '-----------------------------------------------------
        ' Set deivce2
        '-----------------------------------------------------
        '----------------------------------
        ' Set operation mode : Data logger mode
        '----------------------------------
        Ret = DigSetDigitizerMode(Id2, DIG_MODE_LOGGER)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo2.Text = "DigSetDigitizerMode = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' Set sampling clock source : Crystal oscillator of board
        '----------------------------------
        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

        '----------------------------------
        ' Set sampling clock : 1000usec (set value * 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

        '----------------------------------
        ' Set the number of acquiring records : 1
        '----------------------------------
        Ret = DigSetRecordNumber(Id2, 1)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo2.Text = "DigSetRecordNumber = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' Set the number of pre-trigger samples : 1000
        '----------------------------------
        Ret = DigSetPreSampleTimes(Id2, MAX_SAMPLES)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo2.Text = "DigSetPreSampleTimes = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' Set the number of post-trigger samples : 0
        '----------------------------------
        Ret = DigSetPostSampleTimes(Id2, 0)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo2.Text = "DigSetPostSampleTimes = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' Set channel : Enable channel0 only
        '----------------------------------
        ' Enable channel0
        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

        ' Disable channel1
        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

        '----------------------------------
        ' Set the type of pre-trigger : Digital edge
        '----------------------------------
        Ret = DigSetPreTriggerType(Id2, DIG_TRIGGERTYPE_DIEDGE)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo2.Text = "DigSetPreTriggerType = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' Set pre-trigger : Signal source = Synchronous bus 1, Direction = Rising edge, Count = 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

        '----------------------------------
        ' Operation start : Start device2 first
        '----------------------------------
        Ret = DigStart(Id2)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo2.Text = "DigStart = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextInfo2.Text = "Start acquisition : Normality completion"

        Ret = DigStart(Id1)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo1.Text = "DigStart = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextInfo1.Text = "Start acquisition: Normality completion"

        '----------------------------------
        ' Polling until the operation status is changed to be OFF
        '----------------------------------
        Dim Status1 As Integer
        Dim Status2 As Integer

        Do
            System.Windows.Forms.Application.DoEvents()    ' In order to enable the dialog operation

            '---------------------------------------
            ' Device1
            '---------------------------------------
            '----------------------------------
            ' Get the number of samples stored in driver memory
            '----------------------------------
            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)

            '----------------------------------
            ' Get the operation status
            '----------------------------------
            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"

            '---------------------------------------
            ' Device2
            '---------------------------------------
            '----------------------------------
            ' Get the number of samples stored in driver memory
            '----------------------------------
            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)

            '----------------------------------
            ' Get the operation status
            '----------------------------------
            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

        '---------------------------------------
        ' Device1
        '---------------------------------------
        '----------------------------------
        ' After operation stop, get the number of samples which are being stored in driver memory
        '----------------------------------
        Ret = DigGetDriverMemorySamples(Id1, Samples1)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo1.Text = "DigGetDriverMemorySamples = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' Retrieve the acquisition data
        '----------------------------------
        Dim TriggerPoint1 As Integer    ' Position of the first data after the establishment of the post-trigger
        If Samples1 > 0 Then
            ' Adjust to prevent the retrieved number of sampling from exceeding the size of array for storing data
            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
        '----------------------------------
        ' Display the retrieved data
        '----------------------------------
        Dim i As Integer
        For i = 0 To Samples1 - 1
            ListData1.Items.Add((i + 1 & "           " & String.Format("{0:F3}", Data1(i))))
        Next i


        '---------------------------------------
        ' Device2
        '---------------------------------------
        '----------------------------------
        ' After operation stop, get the number of samples which are being stored in driver memory
        '----------------------------------
        Ret = DigGetDriverMemorySamples(Id2, Samples2)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo2.Text = "DigGetDriverMemorySamples = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' Retrieve the acquisition data
        '----------------------------------
        Dim TriggerPoint2 As Integer    ' Position of the first data after the establishment of the post-trigger
        If Samples2 > 0 Then
            ' Adjust to prevent the retrieved number of sampling from exceeding the size of array for storing data
            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
        '----------------------------------
        ' Display the retrieved data
        '----------------------------------
        For i = 0 To Samples2 - 1
            ListData2.Items.Add((i + 1 & "           " & String.Format("{0:F3}", Data2(i))))
        Next i

    End Sub

    '==================================
    ' Get status
    '==================================
    Private Sub CommandInfo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandInfo.Click
        Dim Ret As Integer    ' Return Value
        Dim Ret2 As Integer   ' Return Value2
        Dim ErrorString As New StringBuilder("", 256)     ' Error string
        Dim Count As Integer    ' Number of sampling
        Dim Status As Integer   ' Status

        '---------------------------------------
        ' Device1
        '---------------------------------------
        '----------------------------------
        ' Get the number of samples stored in driver memory
        '----------------------------------
        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

        '----------------------------------
        ' Get the operation status
        '----------------------------------
        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 = "Get status : Normality completion"

        '---------------------------------------
        ' Device2
        '---------------------------------------
        '----------------------------------
        ' Get the number of samples stored in driver memory
        '----------------------------------
        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

        '----------------------------------
        ' Get the operation status
        '----------------------------------
        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 = "Get status : Normality completion"
    End Sub

    '==================================
    ' Stop acquisition
    '==================================
    Private Sub CommandStop_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandStop.Click
        Dim Ret As Integer    ' Return value
        Dim Ret2 As Integer   ' Return value2
        Dim ErrorString As New StringBuilder("", 256)     ' Error string

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

        TextInfo1.Text = "Stop acquisition : Normality completion"

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

        TextInfo2.Text = "Stop acquisition : Normality completion"
    End Sub

    '==================================
    ' Exit handling
    '==================================
    Private Sub CommandExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandExit.Click
        Dim Ret As Integer    ' Return value
        Dim Ret2 As Integer   ' Return value2
        Dim ErrorString As New StringBuilder("", 256)     ' Error string

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

        TextInfo1.Text = "Exit : Normality completion"

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

        TextInfo2.Text = "Exit : Normality completion"
    End Sub

    '==================================
    ' Form unload
    '==================================
    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        Dim Ret As Integer    ' Return value
        Ret = DigExit(Id1)
        Ret = DigExit(Id2)
    End Sub
End Class
