Option Explicit On
Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim wRtn As UInt16
    Public wTotalBoards As UInt16
    
    Friend sCardInfo(MAX_BOARD_NUMBER) As IXUD_CARD_INFO
    Friend sDevInfo(MAX_BOARD_NUMBER) As IXUD_DEVICE_INFO
    Dim wBoardNo As UInt16
    Public sModelName As New String(" "c, 20)

    Dim MAX_DATA_NUM As UInt32 = 512
    Dim dwDIBuf(MAX_DATA_NUM - 1) As UInt32

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim wBoardIndex As UInt16
        '//Driver Initial
        wRtn = Ixud_DriverInit(wTotalBoards)
        If (wRtn) Then
            MsgBox("Driver Initial Error!!Error Code:" & Str(wRtn))
            End
        End If

        '//Get Card Information
        For wBoardIndex = 0 To CUShort(wTotalBoards - 1)

            wRtn = Ixud_GetCardInfo(wBoardIndex, sDevInfo(wBoardIndex), sCardInfo(wBoardIndex), sModelName)

            If (wRtn) Then
                MsgBox("Get Card Information Err")
            End If

            Me.cbxSelBoard.Items.Add(Str(wBoardIndex) & " " & sModelName)
        Next

        Me.cbxSelBoard.SelectedIndex = 0
    End Sub

    Private Sub cbxSelBoard_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxSelBoard.SelectedIndexChanged
        Dim wPortIndex As UInt16

        wBoardNo = CUShort(cbxSelBoard.SelectedIndex)
        cbxPort.Items.Clear()

        If (sCardInfo(wBoardNo).wDIOPorts + sCardInfo(wBoardNo).wDIPorts > 0) Then
            For wPortIndex = 0 To sCardInfo(wBoardNo).wDIOPorts + sCardInfo(wBoardNo).wDIPorts - 1
                cbxPort.Items.Add("Port " + wPortIndex.ToString())
            Next
            cbxPort.SelectedIndex = 0

        End If

        Me.Text = cbxSelBoard.Text + " Digital Input Pacer Program"

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Ixud_DriverClose()
        Close()
    End Sub

    Private Sub btnActive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnActive.Click

        Dim wIndex As UInt32
        Dim iDivIndex As UInt32

        iDivIndex = 8
        wBoardNo = CUShort(cbxSelBoard.SelectedIndex)

        If (btnActive.Text = "&ACTIVE") Then

            btnActive.Text = "&STOP"
            btnExit.Enabled = False
            '//bDisplay = true;
            While (btnActive.Text <> "&ACTIVE")

                lstDIData.Items.Clear()
                '//Start Digital Input Pacer
                wRtn = Ixud_StartDI(wBoardNo, CUShort(cbxPort.SelectedIndex), 0, CSng(tbxSRate.Text) * 1000.0, MAX_BOARD_NUMBER)
                If (wRtn <> 0) Then
                    MsgBox("Start AI Error!!Error Code:" + wRtn.ToString())
                    btnActive_Click(sender, e)
                End If
                '//Get DI Data
                wRtn = Ixud_GetDIBufferH(wBoardNo, CUShort(cbxPort.SelectedIndex), MAX_BOARD_NUMBER, dwDIBuf)

                If (wRtn <> 0) Then
                    MsgBox("Get DI Buffer Error!!Error Code:" + wRtn.ToString())
                    btnActive_Click(sender, e)
                End If

                '//Stop Digital Input Pacer
                wRtn = Ixud_StopDI(wBoardNo, CUShort(cbxPort.SelectedIndex))
                If (wRtn <> 0) Then
                    MsgBox("Stop DI Error!!Error Code:" + wRtn.ToString())
                    btnActive_Click(sender, e)
                End If
                For wIndex = 0 To CUShort(MAX_DATA_NUM / iDivIndex - 1)
                    lstDIData.Items.Add("[" & ((wIndex * iDivIndex + 0).ToString()).PadLeft(4, "0"c) & "]0x" & (Convert.ToString(dwDIBuf(wIndex * iDivIndex + 0), 16)).PadLeft(8, "0"c) & "  [" & ((wIndex * iDivIndex + 1).ToString()).PadLeft(4, "0"c) & "]0x" & (Convert.ToString(dwDIBuf(wIndex * iDivIndex + 1), 16)).PadLeft(8, "0"c) & "  [" & ((wIndex * iDivIndex + 2).ToString()).PadLeft(4, "0"c) & "]0x" & (Convert.ToString(dwDIBuf(wIndex * iDivIndex + 2), 16)).PadLeft(8, "0"c) & "  [" & ((wIndex * iDivIndex + 3).ToString()).PadLeft(4, "0"c) & "]0x" & (Convert.ToString(dwDIBuf(wIndex * iDivIndex + 3), 16)).PadLeft(8, "0"c) & "  [" & ((wIndex * iDivIndex + 4).ToString()).PadLeft(4, "0"c) & "]0x" & (Convert.ToString(dwDIBuf(wIndex * iDivIndex + 4), 16)).PadLeft(8, "0"c) & "  [" & ((wIndex * iDivIndex + 5).ToString()).PadLeft(4, "0"c) & "]0x" & (Convert.ToString(dwDIBuf(wIndex * iDivIndex + 5), 16)).PadLeft(8, "0"c) & "  [" & ((wIndex * iDivIndex + 6).ToString()).PadLeft(4, "0"c) & "]0x" & (Convert.ToString(dwDIBuf(wIndex * iDivIndex + 6), 16)).PadLeft(8, "0"c) & "  [" & ((wIndex * iDivIndex + 7).ToString()).PadLeft(4, "0"c) & "]0x" & (Convert.ToString(dwDIBuf(wIndex * iDivIndex + 7), 16)).PadLeft(8, "0"c))

                    'Me.lstDIData.Items.Add("[" + CStr(wIndex) + "]" + Hex(dwDIBuf(wIndex)))


                    Application.DoEvents()

                Next

            End While
        Else

            '//bDisplay = false;
            btnActive.Text = "&ACTIVE" '
            '//this.pbxDisplay_Paint(pbxDisplay, new PaintEventArgs(pbxDisplay.CreateGraphics(), Rectangle.Empty));                
            btnExit.Enabled = True

        End If


    End Sub


End Class
