Option Strict Off
Option Explicit On 
Imports System.Text
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    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

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Public WithEvents CommandLoad As System.Windows.Forms.Button
    Public WithEvents Graph As System.Windows.Forms.PictureBox
    Public WithEvents TextDeviceInfo As System.Windows.Forms.TextBox
    Public WithEvents TextDevice As System.Windows.Forms.TextBox
    Public WithEvents CommandInit As System.Windows.Forms.Button
    Public WithEvents CommandStart As System.Windows.Forms.Button
    Public WithEvents CommandInfo As System.Windows.Forms.Button
    Public WithEvents CommandStop As System.Windows.Forms.Button
    Public WithEvents CommandExit As System.Windows.Forms.Button
    Public WithEvents TextInfo As System.Windows.Forms.TextBox
    Public WithEvents LabelStatus 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.CommandLoad = New System.Windows.Forms.Button()
        Me.Graph = New System.Windows.Forms.PictureBox()
        Me.TextDeviceInfo = New System.Windows.Forms.TextBox()
        Me.TextDevice = New System.Windows.Forms.TextBox()
        Me.CommandInit = New System.Windows.Forms.Button()
        Me.CommandStart = New System.Windows.Forms.Button()
        Me.CommandInfo = New System.Windows.Forms.Button()
        Me.CommandStop = New System.Windows.Forms.Button()
        Me.CommandExit = New System.Windows.Forms.Button()
        Me.TextInfo = New System.Windows.Forms.TextBox()
        Me.LabelStatus = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'CommandLoad
        '
        Me.CommandLoad.BackColor = System.Drawing.SystemColors.Control
        Me.CommandLoad.Cursor = System.Windows.Forms.Cursors.Default
        Me.CommandLoad.ForeColor = System.Drawing.SystemColors.ControlText
        Me.CommandLoad.Location = New System.Drawing.Point(12, 94)
        Me.CommandLoad.Name = "CommandLoad"
        Me.CommandLoad.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.CommandLoad.Size = New System.Drawing.Size(120, 29)
        Me.CommandLoad.TabIndex = 2
        Me.CommandLoad.Text = "Read Configuration"
        '
        'Graph
        '
        Me.Graph.BackColor = System.Drawing.SystemColors.Window
        Me.Graph.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Graph.Cursor = System.Windows.Forms.Cursors.Default
        Me.Graph.ForeColor = System.Drawing.SystemColors.WindowText
        Me.Graph.Location = New System.Drawing.Point(148, 14)
        Me.Graph.Name = "Graph"
        Me.Graph.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Graph.Size = New System.Drawing.Size(289, 256)
        Me.Graph.TabIndex = 22
        Me.Graph.TabStop = False
        '
        'TextDeviceInfo
        '
        Me.TextDeviceInfo.AcceptsReturn = True
        Me.TextDeviceInfo.AutoSize = False
        Me.TextDeviceInfo.BackColor = System.Drawing.SystemColors.Window
        Me.TextDeviceInfo.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.TextDeviceInfo.ForeColor = System.Drawing.SystemColors.WindowText
        Me.TextDeviceInfo.Location = New System.Drawing.Point(225, 294)
        Me.TextDeviceInfo.MaxLength = 0
        Me.TextDeviceInfo.Multiline = True
        Me.TextDeviceInfo.Name = "TextDeviceInfo"
        Me.TextDeviceInfo.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.TextDeviceInfo.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.TextDeviceInfo.Size = New System.Drawing.Size(212, 75)
        Me.TextDeviceInfo.TabIndex = 8
        Me.TextDeviceInfo.Text = ""
        '
        'TextDevice
        '
        Me.TextDevice.AcceptsReturn = True
        Me.TextDevice.AutoSize = False
        Me.TextDevice.BackColor = System.Drawing.SystemColors.Window
        Me.TextDevice.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.TextDevice.ForeColor = System.Drawing.SystemColors.WindowText
        Me.TextDevice.Location = New System.Drawing.Point(12, 30)
        Me.TextDevice.MaxLength = 0
        Me.TextDevice.Name = "TextDevice"
        Me.TextDevice.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.TextDevice.Size = New System.Drawing.Size(120, 21)
        Me.TextDevice.TabIndex = 0
        Me.TextDevice.Text = "DIG000"
        '
        'CommandInit
        '
        Me.CommandInit.BackColor = System.Drawing.SystemColors.Control
        Me.CommandInit.Cursor = System.Windows.Forms.Cursors.Default
        Me.CommandInit.ForeColor = System.Drawing.SystemColors.ControlText
        Me.CommandInit.Location = New System.Drawing.Point(12, 58)
        Me.CommandInit.Name = "CommandInit"
        Me.CommandInit.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.CommandInit.Size = New System.Drawing.Size(120, 29)
        Me.CommandInit.TabIndex = 1
        Me.CommandInit.Text = "Init"
        '
        'CommandStart
        '
        Me.CommandStart.BackColor = System.Drawing.SystemColors.Control
        Me.CommandStart.Cursor = System.Windows.Forms.Cursors.Default
        Me.CommandStart.ForeColor = System.Drawing.SystemColors.ControlText
        Me.CommandStart.Location = New System.Drawing.Point(12, 130)
        Me.CommandStart.Name = "CommandStart"
        Me.CommandStart.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.CommandStart.Size = New System.Drawing.Size(120, 29)
        Me.CommandStart.TabIndex = 3
        Me.CommandStart.Text = "Start Acquisition"
        '
        'CommandInfo
        '
        Me.CommandInfo.BackColor = System.Drawing.SystemColors.Control
        Me.CommandInfo.Cursor = System.Windows.Forms.Cursors.Default
        Me.CommandInfo.ForeColor = System.Drawing.SystemColors.ControlText
        Me.CommandInfo.Location = New System.Drawing.Point(12, 166)
        Me.CommandInfo.Name = "CommandInfo"
        Me.CommandInfo.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.CommandInfo.Size = New System.Drawing.Size(120, 29)
        Me.CommandInfo.TabIndex = 4
        Me.CommandInfo.Text = "Get Information"
        '
        'CommandStop
        '
        Me.CommandStop.BackColor = System.Drawing.SystemColors.Control
        Me.CommandStop.Cursor = System.Windows.Forms.Cursors.Default
        Me.CommandStop.ForeColor = System.Drawing.SystemColors.ControlText
        Me.CommandStop.Location = New System.Drawing.Point(12, 202)
        Me.CommandStop.Name = "CommandStop"
        Me.CommandStop.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.CommandStop.Size = New System.Drawing.Size(120, 29)
        Me.CommandStop.TabIndex = 5
        Me.CommandStop.Text = "Stop Acquisition"
        '
        'CommandExit
        '
        Me.CommandExit.BackColor = System.Drawing.SystemColors.Control
        Me.CommandExit.Cursor = System.Windows.Forms.Cursors.Default
        Me.CommandExit.ForeColor = System.Drawing.SystemColors.ControlText
        Me.CommandExit.Location = New System.Drawing.Point(12, 238)
        Me.CommandExit.Name = "CommandExit"
        Me.CommandExit.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.CommandExit.Size = New System.Drawing.Size(120, 29)
        Me.CommandExit.TabIndex = 6
        Me.CommandExit.Text = "Exit"
        '
        'TextInfo
        '
        Me.TextInfo.AcceptsReturn = True
        Me.TextInfo.AutoSize = False
        Me.TextInfo.BackColor = System.Drawing.SystemColors.Window
        Me.TextInfo.Cursor = System.Windows.Forms.Cursors.IBeam
        Me.TextInfo.ForeColor = System.Drawing.SystemColors.WindowText
        Me.TextInfo.Location = New System.Drawing.Point(12, 294)
        Me.TextInfo.MaxLength = 0
        Me.TextInfo.Multiline = True
        Me.TextInfo.Name = "TextInfo"
        Me.TextInfo.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.TextInfo.Size = New System.Drawing.Size(212, 75)
        Me.TextInfo.TabIndex = 7
        Me.TextInfo.Text = ""
        '
        'LabelStatus
        '
        Me.LabelStatus.BackColor = System.Drawing.SystemColors.Control
        Me.LabelStatus.Cursor = System.Windows.Forms.Cursors.Default
        Me.LabelStatus.ForeColor = System.Drawing.SystemColors.ControlText
        Me.LabelStatus.Location = New System.Drawing.Point(304, 278)
        Me.LabelStatus.Name = "LabelStatus"
        Me.LabelStatus.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.LabelStatus.Size = New System.Drawing.Size(105, 20)
        Me.LabelStatus.TabIndex = 24
        Me.LabelStatus.Text = "0H"
        '
        'Label2
        '
        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(225, 278)
        Me.Label2.Name = "Label2"
        Me.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Label2.Size = New System.Drawing.Size(57, 20)
        Me.Label2.TabIndex = 23
        Me.Label2.Text = "Status"
        '
        'Label1
        '
        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(12, 14)
        Me.Label1.Name = "Label1"
        Me.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Label1.Size = New System.Drawing.Size(120, 20)
        Me.Label1.TabIndex = 20
        Me.Label1.Text = "Device Name"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(448, 382)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.CommandLoad, Me.Graph, Me.TextDeviceInfo, Me.TextDevice, Me.CommandInit, Me.CommandStart, Me.CommandInfo, Me.CommandStop, Me.CommandExit, Me.TextInfo, Me.LabelStatus, Me.Label2, Me.Label1})
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.Text = "Load Configuration File"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim Id As Short ' Device ID
    Const MAX_SAMPLES As Short = 1024 ' The maximam number of samples
    Dim Data(MAX_SAMPLES) As Single   ' Array of retrieving data
    Dim Records As Integer    ' Number of retrieved record
    Dim RecordLength As Integer    ' Record length
    Dim Channels As Short   ' Number of channels
    Dim TriggerChannel As Short   ' Post-trigger channel
    Dim Range As Single   ' Input range
    Dim bmp As Bitmap    ' Bitmap

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        bmp = New Bitmap(Graph.Size.Width, Graph.Size.Height)    ' Create the bitmap which is compatible with the graph
    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

    '==================================
    ' Read setting file
    '==================================
    Private Sub CommandLoad_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandLoad.Click
        Dim Ret As Integer    ' Return value
        Dim Ret2 As Integer   ' Return value2
        Dim ErrorString As New StringBuilder("", 256)     ' Error string
        Dim FilePath As String    ' File path

        '----------------------------------
        ' Show FileOpen dialog
        '----------------------------------
        Dim LoadDialog As New OpenFileDialog()
        ' Determine the choices that appear in the "Save as file type" or "Files of type" box in the dialog box
        LoadDialog.Filter = "xml files (*.xml)|*.xml"
        If LoadDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            FilePath = LoadDialog.FileName    ' Get the file path
            '---------------------------------
            ' Read the setting file
            '---------------------------------
            Ret = DigLoadConfig(Id, FilePath)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigLoadConfig = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If
            TextInfo.Text = "Read Setting file : Normality completion"
        End If
    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

        '----------------------------------
        ' Get the setup information and display
        '----------------------------------
        CommandInfo_Click(CommandInfo, New System.EventArgs())

        '----------------------------------
        ' 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 = "Start acquisition : Normality completion"

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

        Do
            System.Windows.Forms.Application.DoEvents()    ' In order to enable the dialog operation
            '----------------------------------
            ' Get the number of records which are being stored in driver memory
            '----------------------------------
            Ret = DigGetDriverMemoryRecords(Id, Records)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigGetDriverMemoryRecords = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If

            '----------------------------------
            ' If there are records in driver memory, retrieve the data from the new side
            '----------------------------------
            If Records > 0 Or Records = -1 Then
                Ret = DigReadLatestRecords(Id, DIG_RECORD_DATA, 1, Data, Nothing, Nothing)
                If Ret <> 0 Then
                    Ret2 = DigGetErrorString(Ret, ErrorString)
                    TextInfo.Text = "DigReadLatestRecords = " & Ret & " : " & ErrorString.ToString()
                    Exit Sub
                End If
                '----------------------------------
                ' Draw graph
                '----------------------------------
                DrawGraph()
            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 records which are being stored in driver memory
        '----------------------------------
        Ret = DigGetDriverMemoryRecords(Id, Records)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetDriverMemoryRecords = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' If there are records in driver memory, retrieve the data
        '----------------------------------
        If Records > 0 Or Records = -1 Then
            Ret = DigReadLatestRecords(Id, DIG_RECORD_DATA, 1, Data, Nothing, Nothing)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigReadLatestRecords = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If
            '----------------------------------
            ' Draw graph
            '----------------------------------
            DrawGraph()
        End If

    End Sub

    '==================================
    ' Show information
    '==================================
    Private Sub CommandInfo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandInfo.Click
        Dim Status As Integer
        Dim Ret As Integer    ' Return Value
        Dim Ret2 As Integer   ' Return Value2
        Dim ErrorString As New StringBuilder("", 256)     ' Error string
        Dim SampleRate As Single    ' Sample rate
        Dim Duty As Single    ' Duty ratio
        Dim Level As Single   ' Trigger level
        Dim Slope As Short    ' Trigger direction
        Dim Count As Integer  ' Trigger count

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

        '-------------------------------------
        ' Get the information set by DigLoadConfig function automatically
        '-------------------------------------
        '-------------------------------
        ' Get the sample rate
        '-------------------------------
        Ret = DigGetSampleRate(Id, SampleRate)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetSampleRate = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
		TextDeviceInfo.Text = "Sample rate :" & SampleRate & "Hz" & vbCrLf
        '-------------------------------
        ' Get the record length
        '-------------------------------
        Ret = DigGetRecordLength(Id, RecordLength)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetSampleRate = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextDeviceInfo.Text = TextDeviceInfo.Text & "Record length :" & RecordLength & vbCrLf
        '-------------------------------
        ' Get duty ratio
        '-------------------------------
        Ret = DigGetDutyCycle(Id, Duty)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetDutyCycle = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextDeviceInfo.Text = TextDeviceInfo.Text & "Duty ratio :" & Duty & "%" & vbCrLf
        '-------------------------------
        ' Get the number of effective channels
        '-------------------------------
        Ret = DigGetChannels(Id, Channels)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetChannels = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextDeviceInfo.Text = TextDeviceInfo.Text & "Number of effective channels :" & Channels & vbCrLf
        '-------------------------------
        ' Get the trigger information
        '-------------------------------
        Ret = DigGetPostTriggerAiEdge(Id, TriggerChannel, Level, Slope, Count)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetPostTriggerAiEdge = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextDeviceInfo.Text = TextDeviceInfo.Text & "Channel :" & TriggerChannel & vbCrLf
        TextDeviceInfo.Text = TextDeviceInfo.Text & "Trigger level :" & Level & "V" & vbCrLf
        '-------------------------------
        ' Get the range information
        '-------------------------------
        Ret = DigGetRange(Id, TriggerChannel, Range)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetRange = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
		TextDeviceInfo.Text = TextDeviceInfo.Text & "Input range :" & Range & "VPP" & vbCrLf

        TextInfo.Text = "Show information : 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

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

        TextInfo.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

        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_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed
        Dim Ret As Integer    ' Return value
        Ret = DigExit(Id)
    End Sub

    '==================================
    ' Draw graph
    '==================================
    Private Sub DrawGraph()
        Dim XUnit As Single   ' Drawing unit of X axis
        Dim PosX As Single    ' Drawing coordinate of X axis
        Dim PosY As Single    ' Drawing coordinate of Y axis
        Dim OldX As Single    ' For saving the drawing coordinate of X axis
        Dim OldY As Single    ' For saving the drawing coordinate of Y axis
        Dim pen As Pen        ' Pen for draw graph
        Dim i As Integer
        Dim j As Integer

        ' Draw the current voltage in graph
        Graph.Image = bmp
        Dim grp As Graphics = Graphics.FromImage(Graph.Image)  ' Create a new graphics object from the specified image object
        grp.Clear(Graph.BackColor)                             ' Clear the graph

        '----------------------------------------------------
        ' Repeat the drawing handling only for the times which is equal to the number of channels
        '----------------------------------------------------
        XUnit = Graph.Size.Width() / CSng(RecordLength)  ' Calculate the moving pixel number of X axis for data 1
        For i = 0 To Channels - 1
            If i = 0 Then
                pen = New Pen(System.Drawing.Color.Red, 1)     ' Create red pen
            Else
                pen = New Pen(System.Drawing.Color.Blue, 1)    ' Create blue pen
            End If

            OldY = Graph.Height * ((Range / 2) - Data(i)) / Range     ' Calculate the initial value of Y axis
            If OldY >= Graph.Height Then
                OldY = Graph.Height - 1      ' If the initial value of Y axis exceeds the drawing rect of graph, modify the position
            End If
            If OldY < 0 Then
                OldY = 0     ' If the initial value of Y axis is lower than the drawing rect of graph, modify the position
            End If
            OldX = 0
            For j = i To RecordLength * Channels Step Channels
                PosX = OldX + XUnit      ' Calculate the coordinate of X axis
                PosY = Graph.Height() * ((Range / 2) - Data(j)) / Range       ' Calculate the coordinate of Y axis
                If PosY >= Graph.Height() Then
                    ' If the coordinate of Y axis exceeds the drawing rect
                    PosY = Graph.Height() - 1       ' Modify the drawing position

                ElseIf PosY < 0 Then
                    ' If the coordinate of Y axis is lower than the drawing rect
                    PosY = 0       ' Modify the drawing position
                End If
                grp.DrawLine(pen, OldX, OldY, PosX, PosY)     ' Draw

                OldX = PosX      ' For saving the current coordinate of X axis
                OldY = PosY      ' For saving the current coordinate of Y axis
            Next j
            pen.Dispose()   ' Reales all resoures used by this pen object
        Next i
        grp.Dispose()       ' Reales all resoures used by this graphics object
    End Sub
End Class
