﻿Option Strict Off
Option Explicit On
Imports System.Text

Public Class Form1
    Dim Id As Short ' Device ID
    Const MAX_SAMPLES As Integer = 10000 ' The maximum number of acquiring samples
    Dim Data(MAX_SAMPLES) As Single ' Array of retrieving data
    Dim Samples As Integer ' Number of retrieved samples
    Dim TotalSamples As Integer ' Total number of samples

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

    '==================================
    ' 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

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

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

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

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

        '----------------------------------
        ' 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(Id, DIG_CHANNEL0, Range)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetRange = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo.Text = "Set input range : Normality completion"
    End Sub

    '==================================
    ' Acquisition start
    '==================================
    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
        '----------------------------------
        Samples = 0
        TotalSamples = 0

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

        '----------------------------------
        ' Set sampling clock source : Crystal oscillator of board
        '----------------------------------
        Ret = DigSetClockSource(Id, DIG_ECU_SRC_CLOCK)

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

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

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

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

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

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

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

        '---------------------------------
        ' Set the type of post-trigger : Software
        '---------------------------------
        Ret = DigSetPostTriggerType(Id, DIG_TRIGGERTYPE_SOFT)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigSetPostTriggerType = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' Operation start
        '----------------------------------
        Ret = DigStart(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigStart = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextInfo.Text = "Acquisition start : Normality completion"

        '----------------------------------
        ' Polling until the operation status is changed to be OFF
        '----------------------------------
        Dim Status As Integer
        Dim TriggerPoint As Integer    ' Position of the first data after the establishment of the post-trigger

        Do
            System.Windows.Forms.Application.DoEvents()    ' In order to enable the dialog operation
            '----------------------------------
            ' Get the number of samples stored in driver memory
            '----------------------------------
            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)
            '----------------------------------
            ' Get the total number of acquired samples from start
            '----------------------------------
            Ret = DigGetDigitizedSamples(Id, TotalSamples)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigGetDigitizedSamples = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If
            LabelTotal.Text = CStr(TotalSamples)
            '----------------------------------
            ' Retrieve the acquisition data
            '----------------------------------
            If Samples > 0 Then
                ' Adjust to prevent the retrieved number of sampling from exceeding the size of array for storing data
                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
            '----------------------------------
            ' Get the operation status
            '----------------------------------
            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

        '----------------------------------
        ' After operation stop, get the number of samples which are being stored in driver memory
        '----------------------------------
        Ret = DigGetDriverMemorySamples(Id, Samples)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetDriverMemorySamples = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        '----------------------------------
        ' Get the total number of acquired samples from start
        '----------------------------------
        Ret = DigGetDigitizedSamples(Id, TotalSamples)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetDigitizedSamples = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        LabelTotal.Text = CStr(TotalSamples)
        '----------------------------------
        ' Retrieve the acquisition data
        '----------------------------------
        If Samples > 0 Then
            ' Adjust to prevent the retrieved number of sampling from exceeding the size of array for storing data
            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

    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 retrieved samples
        Dim Total As Integer    ' Total number of samples
        Dim Status As Integer   ' Status

        '----------------------------------
        ' Get the number of samples stored in driver memory
        '----------------------------------
        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)
        '----------------------------------
        ' Get the total number of acquired samples from start
        '----------------------------------
        Ret = DigGetDigitizedSamples(Id, Total)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetDigitizedSamples = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        LabelTotal.Text = CStr(Total)
        '----------------------------------
        ' Get the operation status
        '----------------------------------
        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 = "Get status : Normality completion"
    End Sub

    '==================================
    ' Acquisition stop
    '==================================
    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

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

        TextInfo.Text = "Acquisition stop : 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

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

        TextInfo.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(Id)
    End Sub
End Class
