﻿Public Class ContinuousInput_DataSet
    '-----------------------------
    ' Total number of samplings
    '-----------------------------
    Public total_count As Int32

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

    '================================================================================
    ' DncDataSet Setting button click event
    '================================================================================
    Private Sub Button_DataSetProperty_Click(sender As Object, e As EventArgs) Handles Button_DataSetProperty.Click
        '----------------------------------------
        ' Displays DncDateSet settings screen
        '----------------------------------------
        DncDataSet1.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 ret1 As Integer           ' Variable for getting return value
        Dim ret2 As Integer           ' Variable for getting return value 2
        Dim error_string As String    ' Error string
        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()
        '----------------------------------------
        ' Reset the number of samplings
        '----------------------------------------
        total_count = 0
        '----------------------------------------
        ' Clear the data display text box
        '----------------------------------------
        TextBox_data.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
        '----------------------------------------
        ' Resets the contents of the DncDataSet buffer
        '----------------------------------------
        ret1 = DncDataSet1.BufferReset()
        '------------------------------------
        ' Error process
        '------------------------------------
        If (ret1 <> 0) Then
            '------------------------------------
            ' If the ErrorString method fails, the error content is cleared
            '------------------------------------
            error_string = ""
            ret2 = DncDataSet1.ErrorString(ret1, error_string)
            If (ret2 <> 0) Then
                error_string = ""
            End If
            ListBox_ErrorDetail.Text += "DncDataSet:BufferReset exception:" + error_string
            Return
        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
        '----------------------------------------
        ' Reflects the DncDataSet status on the label
        '----------------------------------------
        TextBox_DataSetStatus.Text = DncDataSet1.DataSetProperty.BufferStatusText
        TextBox_DataSetError.Text = DncDataSet1.DataSetProperty.BufferErrorText
    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()
                                                                )
                                  TextBox_SamplingStatus.Text = DncDaq1.DaqProperty.Device(0).AiContinuousModeStateText
                                  TextBox_SamplingError.Text = DncDaq1.DaqProperty.Device(0).AiContinuousModeErrorText
                              End Sub))
        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()
                                          )
            TextBox_SamplingStatus.Text = DncDaq1.DaqProperty.Device(0).AiContinuousModeStateText
            TextBox_SamplingError.Text = DncDaq1.DaqProperty.Device(0).AiContinuousModeErrorText
        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 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
                                  TextBox_DataSetStatus.Text = DncDataSet1.DataSetProperty.BufferStatusText
                                  TextBox_DataSetError.Text = DncDataSet1.DataSetProperty.BufferErrorText
                              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
            TextBox_DataSetStatus.Text = DncDataSet1.DataSetProperty.BufferStatusText
            TextBox_DataSetError.Text = DncDataSet1.DataSetProperty.BufferErrorText
        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
                                  TextBox_DataSetStatus.Text = DncDataSet1.DataSetProperty.BufferStatusText
                                  TextBox_DataSetError.Text = DncDataSet1.DataSetProperty.BufferErrorText
                              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
            TextBox_DataSetStatus.Text = DncDataSet1.DataSetProperty.BufferStatusText
            TextBox_DataSetError.Text = DncDataSet1.DataSetProperty.BufferErrorText
        End If
    End Sub

    '================================================================================
    ' DncDataSet error event
    '================================================================================
    Private Sub DncDataSet1_OnError(sender As Object, e As dnc.common.DncErrorEventArgs) Handles DncDataSet1.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 = DncDataSet1.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: DncDataSet" + ", " +
                                                                "Error type: " + e.ErrorType.ToString() + ", " +
                                                                "Error code(value): " + e.Error.ToString() + ", " +
                                                                "Error code(character string): " + error_string + ", " +
                                                                "Error parameter: " + e.Param.ToString()
                                                                )
                                  TextBox_DataSetStatus.Text = DncDataSet1.DataSetProperty.BufferStatusText
                                  TextBox_DataSetError.Text = DncDataSet1.DataSetProperty.BufferErrorText
                              End Sub))
        Else
            '------------------------------------
            ' Add the error parameter to the list box
            '------------------------------------
            ListBox_ErrorDetail.Items.Add("Error component: DncDataSet" + ", " +
                                          "Error type: " + e.ErrorType.ToString() + ", " +
                                          "Error code(value): " + e.Error.ToString() + ", " +
                                          "Error code(character string): " + error_string + ", " +
                                          "Error parameter: " + e.Param.ToString()
                                          )
            TextBox_DataSetStatus.Text = DncDataSet1.DataSetProperty.BufferStatusText
            TextBox_DataSetError.Text = DncDataSet1.DataSetProperty.BufferErrorText
        End If
    End Sub

    '================================================================================
    ' DncDaq timing event
    '================================================================================
    Private Sub DncDaq1_OnTiming(sender As Object, e As dnc.daq.TimingEventArgs) Handles DncDaq1.OnTiming
        '------------------------------------
        ' Gets the number of data in the DataSet and displays it on the label
        '------------------------------------
        Invoke(New Action(Sub() TextBox_DataSetCount.Text = DncDataSet1.DataSetProperty.DataCount.ToString()))
    End Sub

    '================================================================================
    ' DncDataSet event that the specified number of data are stored
    '================================================================================
    Private Sub DncDataSet1_OnNumOfDataStore_1(sender As Object, e As dnc.dataset.OnNumOfDataStoreEventArgs) Handles DncDataSet1.OnNumOfDataStore
        Dim ret1 As Integer              ' Variable for getting return value
        Dim ret2 As Integer              ' Variable for getting return value 2 
        Dim data_set_unit As Integer     ' Variable for storing data set unit
        Dim data_set_count As Integer    ' Variable for storing data count (data set unit) in buffer
        Dim i As Integer
        Dim j As Integer
        Dim sampling_data() As Single    ' Array for storing sampling data
        Dim Data As String               ' Character string for displaying sampling data
        Dim error_string As String       ' Error string

        '------------------------------------
        ' Get the dataset unit from the DncDataSet property
        '------------------------------------
        data_set_unit = DncDataSet1.DataSetProperty.DataSetUnit
        '------------------------------------
        ' Get the number of data counts in the buffer (Data set unit) from the DncDataSet property
        '------------------------------------
        data_set_count = DncDataSet1.DataSetProperty.DataSetCount
        '------------------------------------
        ' Initialize the array that stores the data
        '------------------------------------
        ReDim sampling_data(data_set_count * data_set_unit - 1)
        '------------------------------------
        ' Get sampling data
        '------------------------------------
        ret1 = DncDataSet1.GetBufferData(data_set_count, sampling_data)
        '------------------------------------
        ' Error process
        '------------------------------------
        If (ret1 <> 0) Then
            '------------------------------------
            ' If the ErrorString method fails, the error content is cleared
            '------------------------------------
            error_string = ""
            ret2 = DncDataSet1.ErrorString(ret1, error_string)
            If (ret2 <> 0) Then
                error_string = ""
            End If
            Invoke(New Action(Sub() ListBox_ErrorDetail.Items.Add("DncDataSet:GetBufferData exception:" + error_string)))
            Return
        End If
        '------------------------------------
        ' Initialize the variables for the acquired data
        '------------------------------------
        Data = ""
        '------------------------------------
        ' The first line shows the data item name
        ' This sample assumes that the ch. Used is a serial number starting from 0
        '------------------------------------
        Data += "Count" + vbTab
        For i = 0 To data_set_unit - 1
            Data += "ch." + i.ToString() + "[V]" + vbTab
        Next i
        Data += vbCrLf
        '------------------------------------
        ' Data will be added to the second and subsequent lines
        '------------------------------------
        For i = 0 To sampling_data.Length - 1 Step +data_set_unit
            total_count = total_count + 1
            Data += total_count.ToString() + vbTab
            For j = 0 To data_set_unit - 1
                Data += String.Format("{0:F3}", sampling_data(i + j)) + vbTab
            Next j
            Data += vbCrLf
        Next i
        '------------------------------------
        ' Add the acquired data
        '------------------------------------
        Invoke(New Action(Sub()
                              TextBox_data.Text = Data
                              TextBox_DataSetCount.Text = DncDataSet1.DataSetProperty.DataCount.ToString()
                              TextBox_DataSetStatus.Text = DncDataSet1.DataSetProperty.BufferStatusText
                              TextBox_DataSetError.Text = DncDataSet1.DataSetProperty.BufferErrorText
                          End Sub))
    End Sub
End Class
