﻿Public Class ContinuousInput_Graph_WriteFile
    '================================================================================
    ' 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

    '================================================================================
    ' DncGraph Setting button click Event
    '================================================================================
    Private Sub Button_GraphProperty_Click(sender As Object, e As EventArgs) Handles Button_GraphProperty.Click
        '-----------------------------
        ' Displays DncGraph settings screen
        '-----------------------------
        DncGraph1.ShowProperty()
    End Sub

    '================================================================================
    ' DncWriteFile Setting button click event
    '================================================================================
    Private Sub Button_WriteFileProperty_Click(sender As Object, e As EventArgs) Handles Button_WriteFileProperty.Click
        '-----------------------------
        ' Displays DncWriteFile settings screen
        '-----------------------------
        DncWriteFile1.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
        Dim start_trg As String    ' Variable for confirming DncDaq start trigger
        Dim stop_trg As String     ' Variable for confirming DncDaq stop trigger

        '----------------------------------------
        ' Clear Error Details
        '----------------------------------------
        ListBox_ErrorDetail.Items.Clear()
        '----------------------------------------
        ' Check if device settings exist in DncDaq
        '----------------------------------------
        If (DncDaq1.DaqProperty.Device.Count <= 0) Then
            ListBox_ErrorDetail.Items.Add("There is no device setting.")
            Return
        End If
        '----------------------------------------
        ' Check if sampling settings exist in DncDaq
        '----------------------------------------
        If (DncDaq1.DaqProperty.Device(0).Sampling.Count <= 0) Then
            ListBox_ErrorDetail.Items.Add("There is no sampling setting.")
            Return
        End If
        '----------------------------------------
        ' Check if input task settings exist in DncDaq
        '----------------------------------------
        If (DncDaq1.DaqProperty.InputTask.Count <= 0) Then
            ListBox_ErrorDetail.Items.Add("There is no input task setting.")
            Return
        End If
        '----------------------------------------
        ' Check the start and stop trigger in the DncDaq sampling settings
        ' The default is the following settings
        '   Start Trigger : DncButton_StartTrigger_click
        '   Stop Trigger : DncButton_StopTrigger_click
        ' If the above button is not used in the start trigger / stop trigger, it is disabled
        ' If you change the button name or use the stop trigger button for the start trigger,
        ' the code part that enables / disables the button also needs to be modified to match the button name. 
        '----------------------------------------
        start_trg = DncDaq1.DaqProperty.Device(0).Sampling(0).StartTrigger
        stop_trg = DncDaq1.DaqProperty.Device(0).Sampling(0).StopTrigger
        '----------------------------------------
        ' Check if the start trigger is [DncButton_StartTrigger] and enable or disable the button
        ' When using a button, the ButtonName_click / ButtonName_valueChange / ButtonName_OnPressed
        ' can be selected, so you should compare the first part of string(ButtonName).
        '----------------------------------------
        If (start_trg.StartsWith("DncButton_StartTrigger") = True) Then
            '----------------------------------------
            ' Enable the button if DncButton_StartTrigger is used
            '----------------------------------------
            DncButton_StartTrigger.Enabled = True
        Else
            '----------------------------------------
            ' Disable the button if DncButton_StartTrigger is not used
            '----------------------------------------
            DncButton_StartTrigger.Enabled = False
        End If
        '----------------------------------------
        ' Check if the stop trigger is [DncButton_StopTrigger] and enable or disable the button
        ' When using a button, the ButtonName_click / ButtonName_valueChange / ButtonName_OnPressed
        ' can be selected, so you should compare the first part of string(ButtonName).
        '----------------------------------------
        If (stop_trg.StartsWith("DncButton_StopTrigger") = True) Then
            '----------------------------------------
            ' Enable the button if DncButton_StopTrigger is used
            '----------------------------------------
            DncButton_StopTrigger.Enabled = True
        Else
            '----------------------------------------
            ' Disable the button if DncButton_StopTrigger is not used
            '----------------------------------------
            DncButton_StopTrigger.Enabled = False
        End If
        '----------------------------------------
        ' Reflects the DncDaq sampling status on the label
        '----------------------------------------
        TextBox_SamplingStatus.Text = DncDaq1.DaqProperty.Device(0).AiContinuousModeStateText
        TextBox_SamplingError.Text = DncDaq1.DaqProperty.Device(0).AiContinuousModeErrorText
    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
            '----------------------------------------
            ' Add the error parameter to the list box
            ' The method called by Invoke is described by the lambda expression
            '----------------------------------------
            Invoke(New Action(Sub() ListBox_ErrorDetail.Items.Add("Error component: DncDaq" + ", " +
                                                                  "Device No.: " + DeviceNo.ToString() + ", " +
                                                                  "Error type: " + e.ErrorType.ToString() + ", " +
                                                                  "Error code(value): " + e.Error.ToString() + ", " +
                                                                  "Error code(character string): " + error_string + ", " +
                                                                  "Error parameter: " + e.Param.ToString()
                                                                  )))
        Else
            '----------------------------------------
            ' Add the error parameter to the list box
            '----------------------------------------
            ListBox_ErrorDetail.Items.Add("Error component: DncDaq" + ", " +
                                          "Device No.: " + DeviceNo.ToString() + ", " +
                                          "Error type: " + e.ErrorType.ToString() + ", " +
                                          "Error code(value): " + e.Error.ToString() + ", " +
                                          "Error code(character string): " + error_string + ", " +
                                          "Error parameter: " + e.Param.ToString()
                                          )
        End If
    End Sub

    '================================================================================
    ' DncGraph error event
    '================================================================================
    Private Sub DncGraph1_OnError(sender As Object, e As dnc.common.DncErrorEventArgs) Handles DncGraph1.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 = DncGraph1.ErrorString(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
            '----------------------------------------
            ' Add the error parameter to the list box
            ' The method called by Invoke is described by the lambda expression
            '----------------------------------------
            Invoke(New Action(Sub() ListBox_ErrorDetail.Items.Add("Error component: DncGraph" + ", " +
                                                                  "Error type: " + e.ErrorType.ToString() + ", " +
                                                                  "Error code(value): " + e.Error.ToString() + ", " +
                                                                  "Error code(character string): " + error_string + ", " +
                                                                  "Error parameter: " + e.Param.ToString()
                                                                  )))
        Else
            '----------------------------------------
            ' Add the error parameter to the list box
            '----------------------------------------
            ListBox_ErrorDetail.Items.Add("Error component: DncGraph" + ", " +
                                          "Error type: " + e.ErrorType.ToString() + ", " +
                                          "Error code(value): " + e.Error.ToString() + ", " +
                                          "Error code(character string): " + error_string + ", " +
                                          "Error parameter: " + e.Param.ToString()
                                          )
        End If
    End Sub

    '================================================================================
    ' DncWriteFile error event
    '================================================================================
    Private Sub DncWriteFile1_OnError(sender As Object, e As dnc.common.DncErrorEventArgs) Handles DncWriteFile1.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 = DncWriteFile1.ErrorString(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
            '----------------------------------------
            ' Add the error parameter to the list box
            ' The method called by Invoke is described by the lambda expression
            '----------------------------------------
            Invoke(New Action(Sub() ListBox_ErrorDetail.Items.Add("Error component: DncWriteFile" + ", " +
                                                                  "Error type: " + e.ErrorType.ToString() + ", " +
                                                                  "Error code(value): " + e.Error.ToString() + ", " +
                                                                  "Error code(character string): " + error_string + ", " +
                                                                  "Error parameter: " + e.Param.ToString()
                                                                  )))
        Else
            '----------------------------------------
            ' Add the error parameter to the list box
            '----------------------------------------
            ListBox_ErrorDetail.Items.Add("Error component: DncWriteFile" + ", " +
                                          "Error type: " + e.ErrorType.ToString() + ", " +
                                          "Error code(value): " + e.Error.ToString() + ", " +
                                          "Error code(character string): " + error_string + ", " +
                                          "Error parameter: " + e.Param.ToString()
                                          )
        End If
    End Sub

    '================================================================================
    ' DncDaq sampling start event
    '================================================================================
    Private Sub DncDaq1_OnAioContinuousModeStart(sender As Object, e As dnc.daq.AioContinuousModeStartEventArgs) Handles DncDaq1.OnAioContinuousModeStart
        '----------------------------------------
        ' Displays status information 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 the sampling status in the text box
            ' The method called by Invoke is described by the lambda expression
            '----------------------------------------
            Invoke(New Action(Sub()
                                  TextBox_SamplingStatus.Text = DncDaq1.DaqProperty.Device(0).AiContinuousModeStateText
                                  TextBox_SamplingError.Text = DncDaq1.DaqProperty.Device(0).AiContinuousModeErrorText
                              End Sub))
        Else
            '----------------------------------------
            ' Displays the sampling status in the text box
            '----------------------------------------
            TextBox_SamplingStatus.Text = DncDaq1.DaqProperty.Device(0).AiContinuousModeStateText
            TextBox_SamplingError.Text = DncDaq1.DaqProperty.Device(0).AiContinuousModeErrorText
        End If
    End Sub

    '================================================================================
    ' DncDaq sampling stop event
    '================================================================================
    Private Sub DncDaq1_OnAioContinuousModeStop(sender As Object, e As dnc.daq.AioContinuousModeStopEventArgs) Handles DncDaq1.OnAioContinuousModeStop
        '----------------------------------------
        ' Displays status information 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 sampling status / buffer status in the text box
            ' The method called by Invoke is described by the lambda expression
            '----------------------------------------
            Invoke(New Action(Sub()
                                  TextBox_SamplingStatus.Text = DncDaq1.DaqProperty.Device(0).AiContinuousModeStateText
                                  TextBox_SamplingError.Text = DncDaq1.DaqProperty.Device(0).AiContinuousModeErrorText
                              End Sub))
        Else
            '----------------------------------------
            ' Displays sampling status / buffer status in the text box
            '----------------------------------------
            TextBox_SamplingStatus.Text = DncDaq1.DaqProperty.Device(0).AiContinuousModeStateText
            TextBox_SamplingError.Text = DncDaq1.DaqProperty.Device(0).AiContinuousModeErrorText
        End If
    End Sub
End Class
