﻿Public Class EdgeCount
    '================================================================================
    ' DncDaq Setting button click event
    '================================================================================
    Private Sub Button_DaqProperty_Click(sender As Object, e As EventArgs) Handles Button_DaqProperty.Click
        '----------------------------------------
        ' Displays the 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."
            Exit Sub
        End If
        '----------------------------------------
        ' Check if edge settings exist in DncDaq
        '----------------------------------------
        If DncDaq1.DaqProperty.Device(0).Edge.Count <= 3 Then
            TextBox_ErrorDetail.Text = "There is no edge setting."
            Exit Sub
        End If
        '----------------------------------------
        ' Get the detection direction of the edge setting in the text box
        '----------------------------------------
        TextBox_Direction0.Text = DncDaq1.DaqProperty.Device(0).Edge(0).EdgeType.ToString()
        TextBox_Direction1.Text = DncDaq1.DaqProperty.Device(0).Edge(1).EdgeType.ToString()
        TextBox_Direction2.Text = DncDaq1.DaqProperty.Device(0).Edge(2).EdgeType.ToString()
        TextBox_Direction3.Text = DncDaq1.DaqProperty.Device(0).Edge(3).EdgeType.ToString()
    End Sub

    '================================================================================
    ' DNC-DAQ 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() 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
            '----------------------------------------
            ' Add the error parameter to the list 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

    '================================================================================
    ' Action when a DNC-DAQ edge event occurs
    '================================================================================
    Private Sub DncDaq1_OnEdge(sender As Object, e As dnc.daq.EdgeEventArgs) Handles DncDaq1.OnEdge
        '----------------------------------------
        ' Branches the process by the bit number in the event argument
        ' This sample uses bit0-3
        ' If anything other than bit0-3 is detected, it will be displayed in the error text box.
        '----------------------------------------
        Select Case e.BitNumber
            Case 0
                TextBox_Count0.Text = e.Count.ToString()
            Case 1
                TextBox_Count1.Text = e.Count.ToString()
            Case 2
                TextBox_Count2.Text = e.Count.ToString()
            Case 3
                TextBox_Count3.Text = e.Count.ToString()
            Case Else
                TextBox_ErrorDetail.Text = "BitNo.: " + e.BitNumber.ToString() + vbCrLf +
                                           "Count value: " + e.Count.ToString() + vbCrLf +
                                           "Edge direction: " + e.EdgeType.ToString()
        End Select
    End Sub
End Class
