
Public Class Form1
    Dim wRtn As UInt16
    Dim wRtnClk As UInt16
    Dim DOvalue As UInt16
    Public wTotalBoards As UInt16
    Dim sGetCardInfo As IXUD_CARD_INFO
    Dim sGetDevInfo As IXUD_DEVICE_INFO
    Dim FuncAddress As CallBackFun
    Friend sCardInfo() As IXUD_CARD_INFO
    Friend sDevInfo() As IXUD_DEVICE_INFO
    Dim wBoardNo As UInt16
    Public szModelName As New String(" "c, 20)

    Dim wSetDioMode As UInt16
    Dim wDirectorDioMode As UInt32

    Public wApcCount As Integer 'VB6 module2
    Public Sub MyApcFunction(ByVal parm As UInteger)
        'Sleep 1000
        ' --------- Do you action in this function ------------
        wApcCount = wApcCount + 1
        If wApcCount = 10000 Then
            wApcCount = 0
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FuncAddress = AddressOf MyApcFunction
        Dim index As UInt16
        wRtn = Ixud_DriverInit(wTotalBoards)

        ReDim sCardInfo(wTotalBoards - 1)
        ReDim sDevInfo(wTotalBoards - 1)

        For index = 0 To CUShort(wTotalBoards - 1)
            wRtn = Ixud_GetCardInfo(index, sGetDevInfo, sGetCardInfo, szModelName)
            If szModelName <> "0" Then
                Me.cbxBoardNo.Items.Add(Str(index) + ": " + szModelName)
            Else
                Me.cbxBoardNo.Items.Add(Str(index) + ": Err Device")
            End If

        Next
        Me.cbxBoardNo.SelectedIndex = 0
        Me.TriggerMode.SelectedIndex = 0
        DOvalue = 0
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        wRtn = UniDAQ.Ixud_DriverClose()
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdActive.Click
        cbxBoardNo.Enabled = False
        TriggerMode.Enabled = False
        Dim hEvent As Long
        Dim wTriggerMode As Integer
        Dim wTrgMode As Integer

        If cmdActive.Text = "&ACTIVE" Then

            wBoardNo = Val(Me.cbxBoardNo.SelectedIndex)
            wRtn = Ixud_SetDIOMode(wBoardNo, 0, 0) 'Set Port 0(A) to DI Mode
            If wRtn <> 0 Then
                lstMsg.Items.Add("Set Port A to DI Mode Error!!")
            Else
                lstMsg.Items.Add(" Set Port A to DI Mode OK!!!")
            End If


            wRtn = Ixud_SetDIOMode(wBoardNo, 1, 1) 'Set Port 1(B) to DO Mode
            If wRtn <> 0 Then
                lstMsg.Items.Add("Set Port B to DO Mode Error!!")
            Else
                lstMsg.Items.Add(" Set Port B to DO Mode OK!!!")
            End If

            wTrgMode = TriggerMode.SelectedIndex

            wRtn = Ixud_SetDITriggerMode(wBoardNo, 1, wTrgMode, 5) '0:Compare 1:change

            If wRtn <> 0 Then
                lstMsg.Items.Add("SetDITriggerMode Error!!")
            Else
                lstMsg.Items.Add("SetDITriggerMode OK!!!")
            End If

            hEvent = 0 '//Initial hEvent


            wApcCount = 0 '//Callback function count for Int Source 0

            cmdActive.Text = "&STOP"


            wTriggerMode = IXUD_HARDWARE_INT Or IXUD_ACTIVE_HIGH

            '//Register Callback function, Source = 1(INT_0)
            wRtn = Ixud_SetEventCallback(wBoardNo, wTriggerMode, 1, 0, FuncAddress, 0) '//Register APC

            If wRtn <> 0 Then
                lstMsg.Items.Add("Register APC Error!!")
            Else
                lstMsg.Items.Add(" Register APC OK!!!")
            End If


            '//Install IRQ number
            wRtn = Ixud_InstallIrq(wBoardNo, &H1)
            If wRtn Then
                lstMsg.Items.Add("Install IRQ Error")
            Else
                lstMsg.Items.Add("Install IRQ Success!!!")
            End If

            Timer1.Enabled = True
        Else
            cmdActive.Text = "&ACTIVE"
            lstMsg.Items.Add(" Stop Counter Trigger")
            '// Revmove the Interrupt
            wRtn = Ixud_RemoveIrq(wBoardNo)
            lstMsg.Items.Add("Stop Interrupt!!")

            wRtn = Ixud_RemoveEventCallback(wBoardNo, 0) '//Remove APC
            lstMsg.Items.Add("Remove APC!!")

            lstMsg.Items.Add("Event Count" + Str(wApcCount))
            Timer1.Enabled = False
            cbxBoardNo.Enabled = True
            TriggerMode.Enabled = True

        End If


        While Me.cmdActive.Text = "&STOP"


            Me.tbxAPCCount.Text = CStr(wApcCount)

            Application.DoEvents()

        End While



    End Sub

    


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        DOvalue += 1
        If DOvalue > 10 Then
            DOvalue = 0
        End If
        wRtnClk = Ixud_WriteDO(wBoardNo, 1, DOvalue)
        Me.DoVAL.Text = CStr(DOvalue)
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        wRtn = Ixud_RemoveEventCallback(wBoardNo, 0) '//Remove APC
        wRtn = Ixud_RemoveIrq(wBoardNo)
        Timer1.Enabled = False
    End Sub

    
End Class
