﻿Public Class SimpleInput_SingleChannel_Method
    '================================================================================
    ' DncDaq Setting button click event
    '================================================================================
    Private Sub Button_DaqProperty_Click(sender As Object, e As EventArgs) Handles Button_DaqProperty.Click
        '----------------------------------------
        ' Displays DncDaq settings screen
        '----------------------------------------
        DncDaq1.ShowProperty()
    End Sub

    '================================================================================
    ' Exit button click event
    '================================================================================
    Private Sub Button_Exit_Click(sender As Object, e As EventArgs) Handles Button_Exit.Click
        '----------------------------------------
        ' Exit the dialog
        '----------------------------------------
        End
    End Sub

    '================================================================================
    ' DncDaqInit event
    '================================================================================
    Private Sub DncDaq1_OnInit(sender As Object, e As EventArgs) Handles DncDaq1.OnInit
        '----------------------------------------
        ' Clear Error Details
        '----------------------------------------
        TextBox_ErrorDetail.Text = ""
        '----------------------------------------
        ' Check if device settings exist in DncDaq
        '----------------------------------------
        If (DncDaq1.DaqProperty.Device.Count <= 0) Then
            TextBox_ErrorDetail.Text = "There is no device setting."
            Return
        End If
        '----------------------------------------
        ' Check if input task settings exist in DncDaq
        '----------------------------------------
        If (DncDaq1.DaqProperty.InputTask.Count <= 0) Then
            TextBox_ErrorDetail.Text = "There is no input task setting."
            Return
        End If
    End Sub

    '================================================================================
    ' DncDaq error event
    '================================================================================
    Private Sub DncDaq1_OnError(sender As Object, DeviceNo As Short, e As dnc.common.DncErrorEventArgs) Handles DncDaq1.OnError
        Dim ret As Integer            ' Variable for getting return value
        Dim error_string As String    ' Error string

        '----------------------------------------
        ' Get the error string from the error No. in the event argument
        '----------------------------------------
        error_string = ""
        ret = DncDaq1.DaqErrorString(DeviceNo, e.Error, error_string)
        '----------------------------------------
        ' When failed to get the error string, initialize the error string
        '----------------------------------------
        If (ret <> 0) Then
            error_string = ""
        End If
        '----------------------------------------
        ' Displays the obtained error string and the status information in the event argument on the label
        ' Check if Invoke is required because the error event may be executed in another thread,
        ' and if necessary, access the form control with Invoke.
        '----------------------------------------
        If (InvokeRequired = True) Then
            '----------------------------------------
            ' Displays error parameters in the text box
            ' The method called by Invoke is described by the lambda expression
            '----------------------------------------
            Invoke(New Action(Sub() TextBox_ErrorDetail.Text = "Device No.: " + DeviceNo.ToString() + vbCrLf +
                                                               "Error type: " + e.ErrorType.ToString() + vbCrLf +
                                                               "Error code(value): " + e.Error.ToString() + vbCrLf +
                                                               "Error code(character string): " + error_string + vbCrLf +
                                                               "Error parameter: " + e.Param.ToString()
                                                               ))
        Else
            '----------------------------------------
            ' Displays error parameters in the text box
            '----------------------------------------
            TextBox_ErrorDetail.Text = "Device No.: " + DeviceNo.ToString() + vbCrLf +
                                       "Error type: " + e.ErrorType.ToString() + vbCrLf +
                                       "Error code(value): " + e.Error.ToString() + vbCrLf +
                                       "Error code(character string): " + error_string + vbCrLf +
                                       "Error parameter: " + e.Param.ToString()
        End If
    End Sub

    '================================================================================
    ' AiInputChannel button click event
    '================================================================================
    Private Sub Button_InputChannel_Click(sender As Object, e As EventArgs) Handles Button_InputChannel.Click
        Dim ret1 As Integer           ' Variable for getting return value
        Dim ret2 As Integer           ' Variable for getting return value 2
        Dim aidata As Single          ' Variables for storing analog data
        Dim device_no As Short        ' Device No. storage variable
        Dim channel As Short          ' Variable for storing channel data
        Dim error_string As String    ' Error string

        '------------------------------------
        ' Get the device number and conversion channel from the text box
        '------------------------------------
        Try
            device_no = Short.Parse(TextBox_DeviceNo.Text)
            channel = Short.Parse(TextBox_ChannelNo.Text)
        Catch ex As Exception
            TextBox_ErrorDetail.Text = ex.Message
            Return
        End Try
        '------------------------------------
        ' Executing the AiInputChannel method
        '------------------------------------
        ret1 = DncDaq1.AiInputChannel(device_no, channel, aidata)
        '------------------------------------
        ' Error process
        '------------------------------------
        If (ret1 <> 0) Then
            '------------------------------------
            ' If the ErrorString method fails, the error content is cleared
            '------------------------------------
            error_string = ""
            ret2 = DncDaq1.DaqErrorString(device_no, ret1, error_string)
            If (ret2 <> 0) Then
                error_string = ""
            End If
            TextBox_ErrorDetail.Text = "AiInputChannel= " + ret1.ToString() + " " + error_string
            Return
        End If
        '------------------------------------
        ' Displays the acquired data on the label
        '------------------------------------
        TextBox_Data.Text = channel.ToString() + vbTab + aidata.ToString() + vbCrLf
        TextBox_ErrorDetail.Text = "AiInputChannel:" + ret1.ToString() + " Completed normally"
    End Sub
End Class
