'//  V1.00 : 31/10/14 : DB-3CC : Creation 

Public Class FrmDemoTCSUB18A
    Dim D_output As Byte = 0
    Dim err18A As Integer = 0
    Dim ModuleOpened As Boolean = False

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Btn_S1_Click(sender As System.Object, e As System.EventArgs) Handles Btn_S1.Click
        D_output = System.Convert.ToByte(D_output Xor &H1)
        err18A = TCUSB18A.TCUSB18A_Output(1, D_output)
        TCUSB18A.TCUSB18A_Refresh(1)
        If (Btn_S1.BackColor = Color.Lime) Then
            Btn_S1.BackColor = Color.Green
        Else
            Btn_S1.BackColor = Color.Lime
        End If
    End Sub

    Private Sub Btn_S2_Click(sender As System.Object, e As System.EventArgs) Handles Btn_S2.Click
        D_output = System.Convert.ToByte(D_output Xor &H2)
        err18A = TCUSB18A.TCUSB18A_Output(1, D_output)
        TCUSB18A.TCUSB18A_Refresh(1)
        If (Btn_S2.BackColor = Color.Lime) Then
            Btn_S2.BackColor = Color.Green
        Else
            Btn_S2.BackColor = Color.Lime
        End If
    End Sub

    Private Sub Btn_S3_Click(sender As System.Object, e As System.EventArgs) Handles Btn_S3.Click
        D_output = System.Convert.ToByte(D_output Xor &H4)
        err18A = TCUSB18A.TCUSB18A_Output(1, D_output)
        TCUSB18A.TCUSB18A_Refresh(1)
        If (Btn_S3.BackColor = Color.Lime) Then
            Btn_S3.BackColor = Color.Green
        Else
            Btn_S3.BackColor = Color.Lime
        End If
    End Sub

    Private Sub Btn_S4_Click(sender As System.Object, e As System.EventArgs) Handles Btn_S4.Click
        D_output = System.Convert.ToByte(D_output Xor &H8)
        err18A = TCUSB18A.TCUSB18A_Output(1, D_output)
        TCUSB18A.TCUSB18A_Refresh(1)
        If (Btn_S4.BackColor = Color.Lime) Then
            Btn_S4.BackColor = Color.Green
        Else
            Btn_S4.BackColor = Color.Lime
        End If
    End Sub


    Private Sub ButtonOpen_Click(sender As System.Object, e As System.EventArgs) Handles ButtonOpen.Click

        Dim NumeroSeries As UInteger = 0
        Dim Num As Integer = 1
        Dim DllVersion As Byte = 0
        Dim FwVersion As Byte = 0
        NumeroSeries = CType(Txt_serie.Text, UInteger)

        err18A = TCUSB18A.TCUSB18A_OpenN(Num, NumeroSeries)

        If (err18A = 0) Then

            ModuleOpened = True
            labelSN.Text = "SN: " + NumeroSeries.ToString("00000000")
            TCUSB18A.TCUSB18A_Refresh(1)
            err18A = TCUSB18A.TCUSB18A_Version(Num, FwVersion, DllVersion)
            labelVersion.Text = "Version DLL: " + DllVersion.ToString() + " Version Firmware: " + FwVersion.ToString()
            timer1.Enabled = True
            ButtonOpen.Enabled = False

        Else
            ModuleOpened = False
            If (err18A = 1) Then MessageBox.Show("Numero de serie incorrect")
            If (err18A = 2) Then MessageBox.Show("N° de module incorrect")
            If (err18A = 3) Then MessageBox.Show("Pas de module TC/USB-18A détecté")
            If (err18A = 4) Then MessageBox.Show("Module non initialisé")
            Close()
        End If
    End Sub

    Private Sub FrmDemoTCSUB18A_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If (ModuleOpened) Then TCUSB18A.TCUSB18A_Close(1)
    End Sub

    Private Sub timer1_Tick(sender As System.Object, e As System.EventArgs) Handles timer1.Tick
        Dim D_input As UInteger = 0
        Dim D_Temp As Byte = 0
        Dim D_Ana As Double = 0

        TCUSB18A.TCUSB18A_Refresh(1)
        TCUSB18A.TCUSB18A_Input(1, D_input)

        TCUSB18A.TCUSB18A_Counter(1, 1, D_Temp)
        Txt_E1.Text = System.Convert.ToString(D_Temp)
        TCUSB18A.TCUSB18A_Counter(1, 2, D_Temp)
        Txt_E2.Text = System.Convert.ToString(D_Temp)
        TCUSB18A.TCUSB18A_Counter(1, 3, D_Temp)
        Txt_E3.Text = System.Convert.ToString(D_Temp)
        TCUSB18A.TCUSB18A_Counter(1, 4, D_Temp)
        Txt_E4.Text = System.Convert.ToString(D_Temp)
        TCUSB18A.TCUSB18A_Counter(1, 5, D_Temp)
        Txt_E5.Text = System.Convert.ToString(D_Temp)
        TCUSB18A.TCUSB18A_Counter(1, 6, D_Temp)
        Txt_E6.Text = System.Convert.ToString(D_Temp)
        TCUSB18A.TCUSB18A_Counter(1, 7, D_Temp)
        Txt_E7.Text = System.Convert.ToString(D_Temp)
        TCUSB18A.TCUSB18A_Counter(1, 8, D_Temp)
        Txt_E8.Text = System.Convert.ToString(D_Temp)

        TCUSB18A.TCUSB18A_AnaInVolts(1, 1, D_Ana)
        LabelTxt_Ana1.Text = D_Ana.ToString("0.00")
        TCUSB18A.TCUSB18A_AnaInVolts(1, 2, D_Ana)
        LabelTxt_Ana2.Text = D_Ana.ToString("0.00")
        TCUSB18A.TCUSB18A_AnaInVolts(1, 3, D_Ana)
        LabelTxt_Ana3.Text = D_Ana.ToString("0.00")
        TCUSB18A.TCUSB18A_AnaInVolts(1, 4, D_Ana)
        LabelTxt_Ana4.Text = D_Ana.ToString("0.00")

        If ((D_input And &H1) > 0) Then
            Btn_E1.BackColor = Color.Lime
        Else
            Btn_E1.BackColor = Color.Green
        End If
        If ((D_input And &H2) > 0) Then
            Btn_E2.BackColor = Color.Lime
        Else
            Btn_E2.BackColor = Color.Green
        End If
        If ((D_input And &H4) > 0) Then
            Btn_E3.BackColor = Color.Lime
        Else
            Btn_E3.BackColor = Color.Green
        End If
        If ((D_input And &H8) > 0) Then
            Btn_E4.BackColor = Color.Lime
        Else
            Btn_E4.BackColor = Color.Green
        End If
        If ((D_input And &H10) > 0) Then
            Btn_E5.BackColor = Color.Lime
        Else
            Btn_E5.BackColor = Color.Green
        End If
        If ((D_input And &H20) > 0) Then
            Btn_E6.BackColor = Color.Lime
        Else
            Btn_E6.BackColor = Color.Green
        End If
        If ((D_input And &H40) > 0) Then
            Btn_E7.BackColor = Color.Lime
        Else
            Btn_E7.BackColor = Color.Green
        End If
        If ((D_input And &H80) > 0) Then
            Btn_E8.BackColor = Color.Lime
        Else
            Btn_E8.BackColor = Color.Green
        End If

    End Sub

    Private Sub Btn_Ana1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles Btn_Ana1.ValueChanged
        Dim D_ana As Double = 0
        D_ana = System.Convert.ToDouble(Btn_Ana1.Value)
        TCUSB18A.TCUSB18A_AnaOutVolts(1, 1, D_ana)
        TCUSB18A.TCUSB18A_Refresh(1)
    End Sub

    Private Sub Btn_Ana2_ValueChanged(sender As System.Object, e As System.EventArgs) Handles Btn_Ana2.ValueChanged
        Dim D_ana As Double = 0
        D_ana = System.Convert.ToDouble(Btn_Ana2.Value)
        TCUSB18A.TCUSB18A_AnaOutVolts(1, 2, D_ana)
        TCUSB18A.TCUSB18A_Refresh(1)
    End Sub
End Class
