﻿Public Class SimpleInput_MultiChannel_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
    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
            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

    '================================================================================
    ' Processing when the AiInputMultiChannel button is pressed
    '================================================================================
    Private Sub Button_AiInputMultiChannel_Click(sender As Object, e As EventArgs) Handles Button_AiInputMultiChannel.Click
        Dim ret1 As Integer           ' Variable for getting return value
        Dim ret2 As Integer           ' Variable for getting return value 2
        Dim aidata() As Single        ' Array for storing analog data
        Dim device_no As Short        ' Variable for storing Device No.
        Dim channels As Short         ' Variable for storing channel data
        Dim error_string As String    ' Error string

        '------------------------------------
        ' Get the Device No. and the number of conversion channels from the text box
        '------------------------------------
        Try
            device_no = Short.Parse(TextBox_DeviceNo.Text)
            channels = Short.Parse(TextBox_ChannelNum.Text)
        Catch ex As Exception
            TextBox_ErrorDetail.Text = ex.Message
            Return
        End Try
        '------------------------------------
        ' Initialize the array for the number of channels for data storage
        '------------------------------------
        ReDim aidata(channels - 1)
        '------------------------------------
        ' Executing the AiInputMultiChannel method
        '------------------------------------
        ret1 = DncDaq1.AiInputMultiChannel(device_no, channels, 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 = "AiInputMultiChannel= " + ret1.ToString() + " " + error_string
            Return
        End If
        '------------------------------------
        ' Conversion data display processing
        '------------------------------------
        Dim i As Short
        TextBox_Data.Text = ""
        For i = 0 To channels - 1
            TextBox_Data.Text += i.ToString() + vbTab + aidata(i).ToString() + vbCrLf
        Next i
        TextBox_ErrorDetail.Text = "AiInputMultiChannel:" + ret1.ToString() + " Completed normally "
    End Sub
End Class
