
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 mpbof(i - 1) = False Then
            mpbo(i).Image = My.Resources.gray
        Else
            mpbo(i).Image = My.Resources.red
        End If



        Dim DoVal As UInt32
        Dim index As UInt16
        'Dim DoVal1 As ULong
        DoVal = 0


        For index = 0 To CUShort(sCardInfo(cbxSelectBoard.SelectedIndex).wDIOPortWidth - 1)
            If mpbof(index) Then
           
                DoVal = DoVal Or Math.Pow(2, index)
                'DoVal1 = &HFFFFFFFF
            End If
        Next

        If cbxOutput.SelectedIndex < 0 Then
            Exit Sub
        End If
        Ixud_WriteDO(CUShort(cbxSelectBoard.SelectedIndex), CUShort(cbxOutput.SelectedIndex), DoVal)
        'PIODA_DO(CShort(nudActive.Value), DoVal)    ' PCI-P16R16
        'tbxOutput.Text = Hex(DoVal)

    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"
            'btnOutput.Enabled = False
            'btnClear.Enabled = False
            'btnTest.Enabled = False
            'btnOpen.Enabled = False

            For index = 1 To sCardInfo(wBoardNo).wDIOPortWidth
                mpbo(index).Enabled = False
                mpbi(index).Enabled = False
            Next
            Exit Sub
        End If

        btnActive.Text = "&Stop"

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

        If sCardInfo(wBoardNo).wDIOPorts + sCardInfo(wBoardNo).wDIPorts > 0 Then
            Timer1.Enabled = True
        End If

        btnExit.Enabled = False

        '//Set DIO Mode
        If sCardInfo(wBoardNo).wDIOPorts > 1 Then
            wRtn = Ixud_SetDIOModes32(CUShort(wBoardNo), 1 << Me.cbxOutput.SelectedIndex)
        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
            wRtn = Ixud_ReadDI(CUShort(wBoardNo), CUShort(cbxInput.SelectedIndex), DIVal)


            For index = 1 To sCardInfo(wBoardNo).wDIOPortWidth
                'If CBool(DIVal And (1 << (index - 1))) Then
                If CBool(DIVal And (Math.Pow(2, (index - 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
