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

    Dim dwDataNum As Long = 300
    Dim wChannels As UInt16 = 6

    Dim fBuf(9999) As Single
    Dim wChannelList(wChannels) As UInt16
    Dim wConfigList(wChannels) As UInt16

    Dim Clabel() As Label
    Dim cbxChannel() As ComboBox
    Dim before_select_board As UInt16

    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

        Clabel = CType(ControlArrayUtils.getControlArray(Me.groupBox1, "Clabel"), Label())
        cbxChannel = CType(ControlArrayUtils.getControlArray(Me.groupBox1, "cbxChannel"), ComboBox())

        Me.cbxSelBoard.SelectedIndex = 0
        
        
    End Sub

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

        For i = 1 To 6
            cbxChannel(i).Items.Clear()
        Next
        wBoardNo = CUShort(cbxSelBoard.SelectedIndex)
        If (sCardInfo(wBoardNo).wAIChannels > 0) Then
            For wChannelIndex = 0 To sCardInfo(wBoardNo).wAIChannels - 1
                For i = 1 To 6
                    cbxChannel(i).Items.Add("Channel " + wChannelIndex.ToString())
                Next
            Next
            For i = 1 To 6
                cbxChannel(i).SelectedIndex = i - 1
            Next
            cbxCardType.SelectedIndex = 0
            cbxConfig.SelectedIndex = 0
            tbxScanNum.SelectedIndex = 6
        End If
        Me.Text = cbxSelBoard.Text + " Analog Input Polling Scan Demo"
       
    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 i As Integer
        wBoardNo = CUShort(cbxSelBoard.SelectedIndex)

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

            '//Config the Analog Input Setting
            wRtn = UniDAQ.Ixud_ConfigAI(wBoardNo, 2, 2048, CUShort(cbxCardType.SelectedIndex), 0)
            btnActive.Text = "&STOP"
            btnExit.Enabled = False


            If (wRtn > 0) Then

                MsgBox("AI Config Error!!Error Code:" + wRtn.ToString())
                btnActive_Click(sender, e)
            End If

            Dim gr As Graphics = pbxDisplay.CreateGraphics()
            While (btnActive.Text <> "&ACTIVE")
                For i = 1 To 6
                    wChannelList(i - 1) = cbxChannel(i).SelectedIndex
                    wConfigList(i - 1) = cbxConfig.SelectedIndex
                    '//Set Scan Channels
                    '//Set Scan channel's configcode
                Next
                
               
                '//Work on Analog Input Polling
                wRtn = Ixud_PollingAIScan(wBoardNo, wChannels, wChannelList, wConfigList, dwDataNum, fBuf)
                If (wRtn <> 0) Then
                    MsgBox("Start AI Error!!Error Code:" + wRtn.ToString())
                    btnActive_Click(sender, e)
                End If
                
                lDataNo.Text = Str(dwDataNum)
                PlotData(gr)

                Application.DoEvents()
            End While

            gr.Dispose()


        Else


            btnActive.Text = "&ACTIVE" '                
            btnExit.Enabled = True

        End If


    End Sub

    '//Draw the Graphics for Analog Input Data
    Private Sub PlotData(ByVal g As Graphics)
        'float Yup = 20.0F, Ydown = -20.0F;
        Dim fScaleX As Single, fScaleY As Single
        Dim RedPen As New Pen(Color.Red)
        Dim BlackPen As New Pen(Color.Black)
        Dim BluePen As New Pen(Color.Blue)
        Dim OrangePen As New Pen(Color.Orange)
        Dim PinkPen As New Pen(Color.Fuchsia)
        Dim GreenPen As New Pen(Color.ForestGreen)

        Dim PointArray(dwDataNum * wChannels - 1) As PointF
        Dim LPointArray() As PointF
        Dim LBaseArray(1) As PointF
        Dim i As UInt16
        Dim j As UInt16

        For i = PointArray.GetLowerBound(0) To PointArray.GetUpperBound(0)

            PointArray(i).X = i
            PointArray(i).Y = fBuf(i)
        Next

        ReDim LPointArray(PointArray.GetUpperBound(0) / wChannels - 1)

        fScaleX = pbxDisplay.Width / PointArray.Length
        fScaleY = CSng((pbxDisplay.Height * 0.97)) / (CSng(tbxYup.Text) - CSng(tbxYdown.Text))
        pbxDisplay.Refresh()
        For j = 0 To 5

            If tbxScanNum.SelectedIndex > j Then
                For i = PointArray.GetLowerBound(0) To (PointArray.GetUpperBound(0) / wChannels) - 1

                    LPointArray(i).X = PointArray(i * wChannels + j).X * fScaleX

                    LPointArray(i).Y = ((Convert.ToSingle(tbxYup.Text) - PointArray(i * wChannels + j).Y) * fScaleY)
                Next
            Else
                Exit For
            End If

            If j = 0 Then
                g.DrawLines(RedPen, LPointArray)
            ElseIf j = 1 Then
                g.DrawLines(BlackPen, LPointArray)
            ElseIf j = 2 Then
                g.DrawLines(BluePen, LPointArray)
            ElseIf j = 3 Then
                g.DrawLines(OrangePen, LPointArray)
            ElseIf j = 4 Then
                g.DrawLines(PinkPen, LPointArray)
            ElseIf j = 5 Then
                g.DrawLines(GreenPen, LPointArray)
            End If
        Next

        'pbxDisplay.Refresh();

        RedPen.Dispose()
        BlackPen.Dispose()
        BluePen.Dispose()
        OrangePen.Dispose()
        PinkPen.Dispose()
        GreenPen.Dispose()
        'g.Dispose();
    End Sub

    Private Sub cbxConfig_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxConfig.SelectedIndexChanged

        Dim fRangeMax As Single = 0, fRangeMin As Single = 0
        Select Case cbxConfig.SelectedIndex
            Case UniDAQ.IXUD_BI_20V
                fRangeMax = 20.0F
                fRangeMin = -20.0F
                Exit Select
            Case UniDAQ.IXUD_BI_10V
                fRangeMax = 10.0F
                fRangeMin = -10.0F
                Exit Select
            Case UniDAQ.IXUD_BI_5V

                '1 //Bipolar +/- 5V 
                fRangeMax = 5.0F
                fRangeMin = -5.0F
                Exit Select
            Case UniDAQ.IXUD_BI_2V5

                '2 //Bipolar +/- 2.5V 
                fRangeMax = 2.5F
                fRangeMin = -2.5F
                Exit Select
            Case UniDAQ.IXUD_BI_1V25

                '3 //Bipolar +/- 1.25V 
                fRangeMax = 1.25F
                fRangeMin = -1.25F
                Exit Select
            Case UniDAQ.IXUD_BI_0V625

                '4 //Bipolar +/- 0.625V 
                fRangeMax = 0.625F
                fRangeMin = -0.625F
                Exit Select
            Case UniDAQ.IXUD_BI_0V3125

                '5 //Bipolar +/- 0.3125V 
                fRangeMax = 0.3125F
                fRangeMin = -0.3125F
                Exit Select
            Case UniDAQ.IXUD_BI_0V5

                '6 //Bipolar +/- 0.5V 
                fRangeMax = 0.5F
                fRangeMin = -0.5F
                Exit Select
            Case UniDAQ.IXUD_BI_0V05

                '7 //Bipolar +/- 0.05V 
                fRangeMax = 0.05F
                fRangeMin = -0.05F
                Exit Select
            Case UniDAQ.IXUD_BI_0V005

                '8 //Bipolar +/- 0.005V 
                fRangeMax = 0.005F
                fRangeMin = -0.005F
                Exit Select
            Case UniDAQ.IXUD_BI_1V

                '9 //Bipolar +/- 1V 
                fRangeMax = 1.0F
                fRangeMin = -1.0F
                Exit Select
            Case UniDAQ.IXUD_BI_0V1

                '10 //Bipolar +/- 0.1V 
                fRangeMax = 0.1F
                fRangeMin = -0.1F
                Exit Select
            Case UniDAQ.IXUD_BI_0V01

                '11 //Bipolar +/- 0.01V 
                fRangeMax = 0.01F
                fRangeMin = -0.01F
                Exit Select
            Case UniDAQ.IXUD_BI_0V001

                '12 //Bipolar +/- 0.001V 
                fRangeMax = 0.001F
                fRangeMin = -0.001F
                Exit Select
            Case UniDAQ.IXUD_UNI_20V

                '13 //Unipolar 0 ~ 20V 
                fRangeMax = 20.0F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_10V

                '14 //Unipolar 0 ~ 10V 
                fRangeMax = 10.0F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_5V

                '15 //Unipolar 0 ~ 5V 
                fRangeMax = 5.0F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_2V5

                '16 //Unipolar 0 ~ 2.5V 
                fRangeMax = 2.5F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_1V25

                '17 //Unipolar 0 ~ 1.25V 
                fRangeMax = 1.25F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_0V625

                '18 //Unipolar 0 ~ 0.625V 
                fRangeMax = 0.625F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_1V

                '19 //Unipolar 0 ~ 1V 
                fRangeMax = 1.0F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_0V1

                '20 //Unipolar 0 ~ 0.1V 
                fRangeMax = 0.1F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_0V01

                '21 //Unipolar 0 ~ 0.01V 
                fRangeMax = 0.01F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_0V001

                '22 //Unipolar 0 ~ 0.001V 
                fRangeMax = 0.001F
                fRangeMin = 0.0F
                Exit Select
        End Select

        tbxYup.Text = Convert.ToString(fRangeMax)
        tbxYdown.Text = Convert.ToString(fRangeMin)



    End Sub


    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbxDataNum.TextChanged
        dwDataNum = Val(Me.tbxDataNum.Text)
    End Sub


   

    Private Sub tbxScanNum_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbxScanNum.SelectedIndexChanged
        Dim i As Integer
        For i = 1 To 6
            If i <= tbxScanNum.SelectedIndex Then
                Clabel(i).Visible = True
                cbxChannel(i).Visible = True
            Else
                Clabel(i).Visible = False
                cbxChannel(i).Visible = False
            End If
        Next
    End Sub
End Class
