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

    Dim mpbo() As PictureBox    'Output PictureBox Control's Array
    Dim mpbof(31) As Boolean
    Dim mpbi() As PictureBox    'Input PictureBox Control's Array

    Dim mrbSetDio() As RadioButton

    Dim wSetDioMode As UInt16
    Dim wDirectorDioMode As UInt32


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        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, sModelName)
            If sModelName <> "0" Then
                Me.cbxSelectBoard.Items.Add(Str(index) + ": " + sModelName)
            Else
                Me.cbxSelectBoard.Items.Add(Str(index) + ": Err Device")
            End If

            sDevInfo(index) = sGetDevInfo
            sCardInfo(index) = sGetCardInfo
        Next

        mpbo = CType(ControlArrayUtils.getControlArray(Me.gbxOutput, "pbOut"), PictureBox())
        mpbi = CType(ControlArrayUtils.getControlArray(Me.gbxInput, "pbIn"), PictureBox())
        'mrbSetDio = CType(ControlArrayUtils.getControlArray(Me.gbDIOSetting, "rbDIOM"), RadioButton())

        Dim i As Short
        For i = 1 To 32
            mpbo(i).Image = My.Resources.gray
            mpbi(i).Image = My.Resources.gray

            mpbo(i).Enabled = False
            mpbof(i - 1) = False
            mpbi(i).Enabled = False
            mpbo(i).SizeMode = PictureBoxSizeMode.StretchImage
            mpbi(i).SizeMode = PictureBoxSizeMode.StretchImage
            AddHandler mpbo(i).Click, AddressOf SignalOutput

        Next

    End Sub

    '//For picturebox control array Handle, and Digital Output control
    Private Sub SignalOutput(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim i As Short

        i = CShort(DirectCast(sender, PictureBox).Name.Remove(0, 5))

        mpbof(i - 1) = Not mpbof(i - 1)


        If cbxOutput.SelectedIndex < 0 Then
            Exit Sub
        End If

        If mpbof(i - 1) = False Then '//Set DO Bit X Value is Low
            mpbo(i).Image = My.Resources.gray
            wRtn = Ixud_WriteDOBit(CUShort(cbxSelectBoard.SelectedIndex), CUShort(cbxOutput.SelectedIndex), i - 1, 0)
        Else '//Set DO Bit X Value is High
            mpbo(i).Image = My.Resources.red
            wRtn = Ixud_WriteDOBit(CUShort(cbxSelectBoard.SelectedIndex), CUShort(cbxOutput.SelectedIndex), i - 1, 1)
        End If

    End Sub

    Private Sub cbxSelectBoard_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxSelectBoard.SelectedIndexChanged
        Dim index As UInt16
        Dim wIportNum As UInt16

        cbxOutput.Items.Clear()
        cbxInput.Items.Clear()


        wBoardNo = CUShort(cbxSelectBoard.SelectedIndex)
        If sCardInfo(wBoardNo).wDIPorts > sCardInfo(wBoardNo).wDOPorts Then
            wIportNum = sCardInfo(wBoardNo).wDIPorts
        Else
            wIportNum = sCardInfo(wBoardNo).wDOPorts
        End If

        If sCardInfo(wBoardNo).wDIOPorts + sCardInfo(wBoardNo).wDOPorts > 0 Then


            For index = 0 To CUShort(sCardInfo(wBoardNo).wDIOPorts + sCardInfo(wBoardNo).wDOPorts - 1)
                cbxOutput.Items.Add("Port " + Str(index))
            Next
            cbxOutput.SelectedIndex = 0
        End If

        If (sCardInfo(wBoardNo).wDIOPorts + sCardInfo(wBoardNo).wDIPorts) > 0 Then
            For index = 0 To CUShort(sCardInfo(wBoardNo).wDIOPorts + sCardInfo(wBoardNo).wDIPorts - 1)
                cbxInput.Items.Add("Port " + Str(index))
            Next
        End If


        cbxOutput.SelectedIndex = 0
        cbxInput.SelectedIndex = 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 btnActive.Click
        Dim index As UInt16

        If (cbxSelectBoard.SelectedIndex = -1) Then
            MsgBox("Please Selected a Board!!!")
            Exit Sub
        End If

        If btnActive.Text = "&Stop" Then
            Timer1.Enabled = False

            btnExit.Enabled = True
            btnActive.Text = "&Active"

            For index = 1 To sCardInfo(wBoardNo).wDIOPortWidth
                mpbo(index).Enabled = False
                mpbi(index).Enabled = False
            Next
            Exit Sub
        Else
            btnActive.Text = "&Stop"
            If sCardInfo(wBoardNo).wDIOPorts + sCardInfo(wBoardNo).wDIPorts > 0 Then
                Timer1.Enabled = True
            End If

            btnExit.Enabled = False

            For index = 1 To sCardInfo(wBoardNo).wDIOPortWidth
                mpbo(index).Enabled = True
                mpbi(index).Enabled = True
            Next

            'Config the Bi-direction DIO Port 
            If sCardInfo(wBoardNo).wDIOPorts > 1 Then
                wRtn = Ixud_SetDIOModes32(CUShort(wBoardNo), 1 << Me.cbxOutput.SelectedIndex)
            End If
        End If



    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim DIVal As UInt32
        Dim index As UInt16

      
        If cbxInput.SelectedIndex >= 0 Then
            '//Read DI Bit x Value
            For index = 1 To sCardInfo(wBoardNo).wDIOPortWidth
                wRtn = Ixud_ReadDIBit(CUShort(wBoardNo), CUShort(cbxInput.SelectedIndex), index - 1, DIVal)
                If DIVal = 1 Then
                    mpbi(CInt(index)).Image = My.Resources.red
                Else
                    mpbi(CInt(index)).Image = My.Resources.gray
                End If
            Next

            Application.DoEvents()
        End If
    End Sub




End Class
