﻿Imports System.Text

Public Class CapacitanceSingleMeasure
    '----------------------------------------
    ' Declaration of global variables
    '----------------------------------------
    Public Shared g_id As Short    ' ID for device access

    Private Sub CapacitanceSingleMeasure_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '----------------------------------------
        ' Set the default button
        '----------------------------------------
        AcceptButton = Button_Close
        '----------------------------------------
        ' Update the initial display of device name
        '----------------------------------------
        TextBox_DeviceName.Text = "ZM000"
        '----------------------------------------
        ' Create the combobox for channel No.
        '----------------------------------------
        ComboBox_ChannelNumber.Items.Add("CH1")
        ComboBox_ChannelNumber.Items.Add("CH2")
        '----------------------------------------
        ' Change the selection of channel No. combobox to 0
        '----------------------------------------
        ComboBox_ChannelNumber.SelectedIndex = 0
        '----------------------------------------
        ' Create the combobox for frequency
        '----------------------------------------
        ComboBox_Frequency.Items.Add("120 Hz")
        ComboBox_Frequency.Items.Add("1 kHz")
        '----------------------------------------
        ' Change the selection of frequency combobox to 0
        '----------------------------------------
        ComboBox_Frequency.SelectedIndex = 0
        '----------------------------------------
        ' Create the combobox for signal level
        '----------------------------------------
        ComboBox_Level.Items.Add("0.5 V")
        ComboBox_Level.Items.Add("1 V")
        '----------------------------------------
        ' Change the selection of signal level combobox to 0
        '----------------------------------------
        ComboBox_Level.SelectedIndex = 0
        '----------------------------------------
        ' Create the combobox for measurement speed
        '----------------------------------------
        ComboBox_Speed.Items.Add("FAST")
        ComboBox_Speed.Items.Add("NORMAL")
        ComboBox_Speed.Items.Add("SLOW")
        '----------------------------------------
        ' Change the selection of measurement speed combobox to 0
        '----------------------------------------
        ComboBox_Speed.SelectedIndex = 0
        '----------------------------------------
        ' Update the initial display of average count
        '----------------------------------------
        TextBox_AverageCount.Text = "1"
        '----------------------------------------
        ' Create the combobox for measurement range
        '----------------------------------------
        ComboBox_Range.Items.Add("4 nF")
        ComboBox_Range.Items.Add("40 nF")
        ComboBox_Range.Items.Add("400 nF")
        ComboBox_Range.Items.Add("4 uF")
        ComboBox_Range.Items.Add("40 uF")
        ComboBox_Range.Items.Add("400 uF")
        ComboBox_Range.Items.Add("1.4 mF")
        '----------------------------------------
        ' Change the selection of measurement range combobox to 0
        '----------------------------------------
        ComboBox_Range.SelectedIndex = 0
        '----------------------------------------
        ' Change the selection of equivalent circuit mode combobox to 0
        '----------------------------------------
        ComboBox_Circuit.Items.Add("AUTO")
        ComboBox_Circuit.Items.Add("SERIAL")
        ComboBox_Circuit.Items.Add("PARALLEL")
        '----------------------------------------
        ' Change the selection of equivalent circuit mode combobox to 0
        '----------------------------------------
        ComboBox_Circuit.SelectedIndex = 0
        '----------------------------------------
        ' Update the initial display of measurement start delay
        '----------------------------------------
        TextBox_StartDelay.Text = "2"
        '----------------------------------------
        ' Create the combobox for trigger synchronous output
        '----------------------------------------
        ComboBox_TriggerSyncEnable.Items.Add("DISABLE")
        ComboBox_TriggerSyncEnable.Items.Add("ENABLE")
        '----------------------------------------
        ' Change the selection of trigger synchronous output combobox to 0
        '----------------------------------------
        ComboBox_TriggerSyncEnable.SelectedIndex = 0
        '----------------------------------------
        ' Update the initial display of measurement signal output delay
        '----------------------------------------
        TextBox_SignalOutputDelay.Text = "0"
        '----------------------------------------
        ' Create the combobox for contact check
        '----------------------------------------
        ComboBox_ContactCheck.Items.Add("DISABLE")
        ComboBox_ContactCheck.Items.Add("BEFORE")
        ComboBox_ContactCheck.Items.Add("AFTER")
        ComboBox_ContactCheck.Items.Add("BOTH")
        '----------------------------------------
        ' Change the selection of contact check combobox to 0
        '----------------------------------------
        ComboBox_ContactCheck.SelectedIndex = 0
    End Sub

    '================================================================================
    ' It is the process when the ZmInit button is clicked
    ' Initialize the device
    '================================================================================
    Private Sub Button_Init_Click(sender As Object, e As EventArgs) Handles Button_Init.Click
        Dim device_name As String                         ' Device name
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Get device name from textbox
        '----------------------------------------
        device_name = TextBox_DeviceName.Text
        '----------------------------------------
        ' Device initialization
        '----------------------------------------
        ret = ZmInit(device_name, g_id)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        ZmGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnValue.Text = "ZmInit = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the ZmResetDevice button is clicked
    ' Reset the device
    '================================================================================
    Private Sub Button_ResetDevice_Click(sender As Object, e As EventArgs) Handles Button_ResetDevice.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Reset device
        '----------------------------------------
        ret = ZmResetDevice(g_id)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        ZmGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnValue.Text = "ZmResetDevice = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the ZmSetFrequency button is clicked
    ' Set the frequency
    '================================================================================
    Private Sub Button_SetFrequency_Click(sender As Object, e As EventArgs) Handles Button_SetFrequency.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim channel_number As UShort                      ' Channel No.
        Dim frequency As UShort                           ' Frequency

        '----------------------------------------
        ' Get the channel number to set from combobox
        '----------------------------------------
        channel_number = ComboBox_ChannelNumber.SelectedIndex + 1
        '----------------------------------------
        ' Get the frequency to set from combobox
        '----------------------------------------
        frequency = ComboBox_Frequency.SelectedIndex
        '----------------------------------------
        ' Set the frequency
        '----------------------------------------
        ret = ZmSetFrequency(g_id, channel_number, frequency)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        ZmGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnValue.Text = "ZmSetFrequency = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the ZmSetLevel button is clicked
    ' Set the signal level
    '================================================================================
    Private Sub Button_SetLevel_Click(sender As Object, e As EventArgs) Handles Button_SetLevel.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim channel_number As UShort                      ' Channel No.
        Dim level As UShort                               ' Signal level

        '----------------------------------------
        ' Get the channel number to set from combobox
        '----------------------------------------
        channel_number = ComboBox_ChannelNumber.SelectedIndex + 1
        '----------------------------------------
        ' Get the signal level to set from combobox
        '----------------------------------------
        level = ComboBox_Level.SelectedIndex
        '----------------------------------------
        ' Set the signal level
        '----------------------------------------
        ret = ZmSetLevel(g_id, channel_number, level)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        ZmGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnValue.Text = "ZmSetLevel = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the ZmSetSpeed button is clicked
    ' Set the measurement speed and the average count
    '================================================================================
    Private Sub Button_SetSpeed_Click(sender As Object, e As EventArgs) Handles Button_SetSpeed.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim channel_number As UShort                      ' Channel No.
        Dim speed As UShort                               ' Measurement speed
        Dim average_count As UInteger                     ' Average count

        '----------------------------------------
        ' Get the channel number to set from combobox
        '----------------------------------------
        channel_number = ComboBox_ChannelNumber.SelectedIndex + 1
        '----------------------------------------
        ' Get the measurement speed to set from combobox
        '----------------------------------------
        speed = ComboBox_Speed.SelectedIndex
        '----------------------------------------
        ' Get the average count to set from textbox
        '----------------------------------------
        If (UInteger.TryParse(TextBox_AverageCount.Text, average_count) = False) Then
            average_count = 1
        End If
        '----------------------------------------
        ' Set the measurement speed and the average count
        '----------------------------------------
        ret = ZmSetSpeed(g_id, channel_number, speed, average_count)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        ZmGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnValue.Text = "ZmSetSpeed = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the ZmSetRange button is clicked
    ' Set the measurement range
    '================================================================================
    Private Sub Button_SetRange_Click(sender As Object, e As EventArgs) Handles Button_SetRange.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim channel_number As UShort                      ' Channel No.
        Dim range As UShort                               ' Measurement range

        '----------------------------------------
        ' Get the channel number to set from combobox
        '----------------------------------------
        channel_number = ComboBox_ChannelNumber.SelectedIndex + 1
        '----------------------------------------
        ' Get the measurement range to set from combobox
        '----------------------------------------
        range = ComboBox_Range.SelectedIndex + 3
        '----------------------------------------
        ' Set the measurement range
        '----------------------------------------
        ret = ZmSetRange(g_id, channel_number, range)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        ZmGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnValue.Text = "ZmSetRange = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the ZmSetCircuit button is clicked
    ' Set the equivalent circuit mode
    '================================================================================
    Private Sub Button_SetCircuit_Click(sender As Object, e As EventArgs) Handles Button_SetCircuit.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim channel_number As UShort                      ' Channel No.
        Dim circuit As UShort                             ' Equivalent circuit mode

        '----------------------------------------
        ' Get the channel number to set from combobox
        '----------------------------------------
        channel_number = ComboBox_ChannelNumber.SelectedIndex + 1
        '----------------------------------------
        ' Get the equivalent circuit mode to set from combobox
        '----------------------------------------
        circuit = ComboBox_Circuit.SelectedIndex
        '----------------------------------------
        ' Set the equivalent circuit mode
        '----------------------------------------
        ret = ZmSetCircuit(g_id, channel_number, circuit)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        ZmGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnValue.Text = "ZmSetCircuit = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the ZmSetStartDelay button is clicked
    ' Set the measurement start delay
    '================================================================================
    Private Sub Button_SetStartDelay_Click(sender As Object, e As EventArgs) Handles Button_SetStartDelay.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim channel_number As UShort                      ' Channel No.
        Dim start_delay As Single                         ' Measurement start delay

        '----------------------------------------
        ' Get the channel number to set from combobox
        '----------------------------------------
        channel_number = ComboBox_ChannelNumber.SelectedIndex + 1
        '----------------------------------------
        ' Get the measurement start delay to set from textbox
        '----------------------------------------
        If (Single.TryParse(TextBox_StartDelay.Text, start_delay) = False) Then
            start_delay = 2
        End If
        '----------------------------------------
        ' Set the measurement start delay
        '----------------------------------------
        ret = ZmSetStartDelay(g_id, channel_number, start_delay)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        ZmGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnValue.Text = "ZmSetStartDelay = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the ZmSetTriggerSyncEnable button is clicked
    ' Set the trigger synchronous output
    '================================================================================
    Private Sub Button_SetTriggerSyncEnable_Click(sender As Object, e As EventArgs) Handles Button_SetTriggerSyncEnable.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim channel_number As UShort                      ' Channel No.
        Dim trigger_sync_enable As UShort                 ' Trigger synchronous output

        '----------------------------------------
        ' Get the channel number to set from combobox
        '----------------------------------------
        channel_number = ComboBox_ChannelNumber.SelectedIndex + 1
        '----------------------------------------
        ' Get the trigger synchronous output to set from combobox
        '----------------------------------------
        trigger_sync_enable = ComboBox_TriggerSyncEnable.SelectedIndex
        '----------------------------------------
        ' Set the trigger synchronous output
        '----------------------------------------
        ret = ZmSetTriggerSyncEnable(g_id, channel_number, trigger_sync_enable)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        ZmGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnValue.Text = "ZmSetTriggerSyncEnable = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the ZmSetSignalOutputDelay button is clicked
    ' Set the measurement signal output delay
    '================================================================================
    Private Sub Button_SetSignalOutputDelay_Click(sender As Object, e As EventArgs) Handles Button_SetSignalOutputDelay.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim channel_number As UShort                      ' Channel No.
        Dim signal_output_delay As Single                 ' Measurement signal output delay

        '----------------------------------------
        ' Get the channel number to set from combobox
        '----------------------------------------
        channel_number = ComboBox_ChannelNumber.SelectedIndex + 1
        '----------------------------------------
        ' Get the measurement signal output delay to set from textbox
        '----------------------------------------
        If (Single.TryParse(TextBox_SignalOutputDelay.Text, signal_output_delay) = False) Then
            signal_output_delay = 0
        End If
        '----------------------------------------
        ' Set the measurement signal output delay
        '----------------------------------------
        ret = ZmSetSignalOutputDelay(g_id, channel_number, signal_output_delay)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        ZmGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnValue.Text = "ZmSetSignalOutputDelay = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the ZmSetContactCheck button is clicked
    ' Set the contact check
    '================================================================================
    Private Sub Button_SetContactCheck_Click(sender As Object, e As EventArgs) Handles Button_SetContactCheck.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim channel_number As UShort                      ' Channel No.
        Dim contact_check As UShort                       ' Contack check

        '----------------------------------------
        ' Get the channel number to set from combobox
        '----------------------------------------
        channel_number = ComboBox_ChannelNumber.SelectedIndex + 1
        '----------------------------------------
        ' Get the contact check to set from combobox
        '----------------------------------------
        contact_check = ComboBox_ContactCheck.SelectedIndex
        '----------------------------------------
        ' Set the contact check
        '----------------------------------------
        ret = ZmSetContactCheck(g_id, channel_number, contact_check)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        ZmGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnValue.Text = "ZmSetContactCheck = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the ZmSingleCMeasure button is clicked
    ' Measure
    '================================================================================
    Private Sub Button_SingleCMeasure_Click(sender As Object, e As EventArgs) Handles Button_SingleCMeasure.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim channel_number As UShort                      ' Channel No.
        Dim capacitance As Single                         ' Capacitance
        Dim dissipation As Single                         ' Dissipation factor
        Dim status As UInteger                            ' Status
        Dim contact_check As UShort                       ' Contact check

        '----------------------------------------
        ' Get the channel number to measure from combobox
        '----------------------------------------
        channel_number = ComboBox_ChannelNumber.SelectedIndex + 1
        '----------------------------------------
        ' Measure
        '----------------------------------------
        ret = ZmSingleCMeasure(g_id, channel_number, capacitance, dissipation, status)
        '----------------------------------------
        ' Update the display when the ZmSingleCMeasure function ends normally
        '----------------------------------------
        If (ret = ZM_ERR_SUCCESS) Then
            '----------------------------------------
            ' Update the display of capacitance
            '----------------------------------------
            TextBox_Capacitance.Text = String.Format("{0:e}", capacitance)
            '----------------------------------------
            ' Update the display of dissipation factor
            '----------------------------------------
            TextBox_Dissipation.Text = String.Format("{0:f}", dissipation)
            '----------------------------------------
            ' Update the display of equivalent circuit mode
            '----------------------------------------
            If (((status >> 24) And &H3) = ZM_CIRCUIT_SERIAL) Then
                '----------------------------------------
                ' Update the display to serial equivalent circuit
                '----------------------------------------
                TextBox_Circuit.Text = "SERIAL"
            ElseIf (((status >> 24) And &H3) = ZM_CIRCUIT_PARALLEL) Then
                '----------------------------------------
                ' Update the display to parallel equivalent circuit
                '----------------------------------------
                TextBox_Circuit.Text = "PARALLEL"
            End If
            '----------------------------------------
            ' Update the display of contact check error
            ' Update the display to '-' when contact check is disabled
            '----------------------------------------
            ZmGetContactCheck(g_id, channel_number, contact_check)
            '----------------------------------------
            ' The contact check is disabled so update all display to '-'
            '----------------------------------------
            If (contact_check = ZM_CONTACTCHECK_DISABLE) Then
                TextBox_BeforeL.Text = "-"
                TextBox_BeforeH.Text = "-"
                TextBox_AfterL.Text = "-"
                TextBox_AfterH.Text = "-"
                '----------------------------------------
                ' The contact check is 'BEFORE' so update 'after measurement' to '-'
                '----------------------------------------
            ElseIf (contact_check = ZM_CONTACTCHECK_BEFORE) Then
                TextBox_BeforeL.Text = String.Format("{0:d}", (status >> 16) And &H1)
                TextBox_BeforeH.Text = String.Format("{0:d}", (status >> 17) And &H1)
                TextBox_AfterL.Text = "-"
                TextBox_AfterH.Text = "-"
                '----------------------------------------
                ' The contact check is 'AFTER' so update 'berore measurement' to '-'
                '----------------------------------------
            ElseIf (contact_check = ZM_CONTACTCHECK_AFTER) Then
                TextBox_BeforeL.Text = "-"
                TextBox_BeforeH.Text = "-"
                TextBox_AfterL.Text = String.Format("{0:d}", (status >> 18) And &H1)
                TextBox_AfterH.Text = String.Format("{0:d}", (status >> 19) And &H1)
                '----------------------------------------
                ' The contact check is 'BOTH' so update all display to status value
                '----------------------------------------
            ElseIf (contact_check = ZM_CONTACTCHECK_BOTH) Then
                TextBox_BeforeL.Text = String.Format("{0:d}", (status >> 16) And &H1)
                TextBox_BeforeH.Text = String.Format("{0:d}", (status >> 17) And &H1)
                TextBox_AfterL.Text = String.Format("{0:d}", (status >> 18) And &H1)
                TextBox_AfterH.Text = String.Format("{0:d}", (status >> 19) And &H1)
            End If
        End If
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        ZmGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnValue.Text = "ZmSingleCMeasure = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the ZmExit button is clicked
    ' Perform the exit process of device
    '================================================================================
    Private Sub Button_Exit_Click(sender As Object, e As EventArgs) Handles Button_Exit.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Exit process
        '----------------------------------------
        ret = ZmExit(g_id)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        ZmGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnValue.Text = "ZmExit = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the Close button is clicked
    ' Perform the exit process of device and terminate the application
    '================================================================================
    Private Sub Button_Close_Click(sender As Object, e As EventArgs) Handles Button_Close.Click
        '----------------------------------------
        ' Exit process
        '----------------------------------------
        ZmExit(g_id)
        '----------------------------------------
        ' Close dialog
        '----------------------------------------
        Me.Close()
    End Sub
End Class
