'//  V1.00 : 04/11/14 : DB-3CC : Creation 

Public Class FrmTCUSB16IO
    Dim D_output As Byte = 0
    Dim err As Integer = 0
    Dim ModuleOpened As Boolean = False
    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(TextBoxSN.Text, UInteger)

        'ouverture du module
        err = TCUSB16IO.TCUSB16IO_OpenN(Num, NumeroSeries)

        If (err = 0) Then

            ModuleOpened = True
            labelSN.Text = "SN: " + NumeroSeries.ToString("00000000")
            'Actualisation des données du module
            TCUSB16IO.TCUSB16IO_Refresh(1)
            'Lecture du firmware et la version de la dll
            err = TCUSB16IO.TCUSB16IO_Version(Num, FwVersion, DllVersion)
            labelDLL.Text = "Version DLL: " + DllVersion.ToString()
            labelFW.Text = "Version Firmware: " + FwVersion.ToString()
            timer1.Enabled = True
            ButtonOpen.Enabled = False

        Else
            ModuleOpened = False
            If (err = 1) Then MessageBox.Show("Numero de serie incorrect")
            If (err = 2) Then MessageBox.Show("N° de module incorrect")
            If (err = 3) Then MessageBox.Show("Pas de module TC/USB-16IO détecté")
            If (err = 4) Then MessageBox.Show("Module non initialisé")
            Close()
        End If
    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
        Dim etat As Integer = 0
        Dim DataPos As Integer = 0

        'Rafraichissement données du module
        TCUSB16IO.TCUSB16IO_Refresh(1)

        'Lecture entrées TOR 
        TCUSB16IO.TCUSB16IO_Input(1, D_input)

        If ((D_input And &H1) > 0) Then
            panelI1.BackColor = Color.Lime
        Else
            panelI1.BackColor = Color.Green
        End If
        If ((D_input And &H2) > 0) Then
            panelI2.BackColor = Color.Lime
        Else
            panelI2.BackColor = Color.Green
        End If
        If ((D_input And &H4) > 0) Then
            panelI3.BackColor = Color.Lime
        Else
            panelI3.BackColor = Color.Green
        End If
        If ((D_input And &H8) > 0) Then
            panelI4.BackColor = Color.Lime
        Else
            panelI4.BackColor = Color.Green
        End If
        If ((D_input And &H10) > 0) Then
            panelI5.BackColor = Color.Lime
        Else
            panelI5.BackColor = Color.Green
        End If
        If ((D_input And &H20) > 0) Then
            panelI6.BackColor = Color.Lime
        Else
            panelI6.BackColor = Color.Green
        End If
        If ((D_input And &H40) > 0) Then
            panelI7.BackColor = Color.Lime
        Else
            panelI7.BackColor = Color.Green
        End If
        If ((D_input And &H80) > 0) Then
            panelI8.BackColor = Color.Lime
        Else
            panelI8.BackColor = Color.Green
        End If

        'Lecteur des compteurs d'évenement sur les entrées TOR
        TCUSB16IO.TCUSB16IO_Counter(1, 1, D_Temp)
        textBoxCTR1.Text = System.Convert.ToString(D_Temp)
        TCUSB16IO.TCUSB16IO_Counter(1, 2, D_Temp)
        textBoxCTR2.Text = System.Convert.ToString(D_Temp)
        TCUSB16IO.TCUSB16IO_Counter(1, 3, D_Temp)
        textBoxCTR3.Text = System.Convert.ToString(D_Temp)
        TCUSB16IO.TCUSB16IO_Counter(1, 4, D_Temp)
        textBoxCTR4.Text = System.Convert.ToString(D_Temp)
        TCUSB16IO.TCUSB16IO_Counter(1, 5, D_Temp)
        textBoxCTR5.Text = System.Convert.ToString(D_Temp)
        TCUSB16IO.TCUSB16IO_Counter(1, 6, D_Temp)
        textBoxCTR6.Text = System.Convert.ToString(D_Temp)
        TCUSB16IO.TCUSB16IO_Counter(1, 7, D_Temp)
        textBoxCTR7.Text = System.Convert.ToString(D_Temp)
        TCUSB16IO.TCUSB16IO_Counter(1, 8, D_Temp)
        textBoxCTR8.Text = System.Convert.ToString(D_Temp)

        'Lecture entrées ABZ
        etat = TCUSB16IO.TCUSB16IO_BitInput(1, 11)
        If (etat = 1) Then
            panelZ.BackColor = Color.Lime
        Else
            panelZ.BackColor = Color.Green
        End If

        etat = TCUSB16IO.TCUSB16IO_BitInput(1, 9)
        If (etat = 1) Then
            panelA.BackColor = Color.Lime
        Else
            panelA.BackColor = Color.Green
        End If

        etat = TCUSB16IO.TCUSB16IO_BitInput(1, 10)
        If (etat = 1) Then
            panelB.BackColor = Color.Lime
        Else
            panelB.BackColor = Color.Green
        End If

        'Lecture compteur de position codeur
        err = TCUSB16IO_Pos(1, DataPos)
        textBoxDataPos.Text = DataPos.ToString()
    End Sub

    Private Sub FrmTCUSB16IO_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If ModuleOpened Then
            'Fermeture module
            err = TCUSB16IO.TCUSB16IO_Close(1)
        End If
    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)
        err = TCUSB16IO.TCUSB16IO_Output(1, D_output)
        TCUSB16IO.TCUSB16IO_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)
        err = TCUSB16IO.TCUSB16IO_Output(1, D_output)
        TCUSB16IO.TCUSB16IO_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)
        err = TCUSB16IO.TCUSB16IO_Output(1, D_output)
        TCUSB16IO.TCUSB16IO_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)
        err = TCUSB16IO.TCUSB16IO_Output(1, D_output)
        TCUSB16IO.TCUSB16IO_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 Btn_S5_Click(sender As System.Object, e As System.EventArgs) Handles Btn_S5.Click
        D_output = System.Convert.ToByte(D_output Xor &H10)
        err = TCUSB16IO.TCUSB16IO_Output(1, D_output)
        TCUSB16IO.TCUSB16IO_Refresh(1)
        If (Btn_S5.BackColor = Color.Lime) Then
            Btn_S5.BackColor = Color.Green
        Else
            Btn_S5.BackColor = Color.Lime
        End If
    End Sub

    Private Sub Btn_S6_Click(sender As System.Object, e As System.EventArgs) Handles Btn_S6.Click
        D_output = System.Convert.ToByte(D_output Xor &H20)
        err = TCUSB16IO.TCUSB16IO_Output(1, D_output)
        TCUSB16IO.TCUSB16IO_Refresh(1)
        If (Btn_S6.BackColor = Color.Lime) Then
            Btn_S6.BackColor = Color.Green
        Else
            Btn_S6.BackColor = Color.Lime
        End If
    End Sub

    Private Sub buttonRaz_Click(sender As System.Object, e As System.EventArgs)
        'RAZ position codeur
        TCUSB16IO.TCUSB16IO_ResetPos(1)
        TCUSB16IO.TCUSB16IO_Refresh(1)
    End Sub

    Private Sub button10_Click(sender As System.Object, e As System.EventArgs) Handles button10.Click
        'RAZ position codeur
        TCUSB16IO.TCUSB16IO_ResetPos(1)
        TCUSB16IO.TCUSB16IO_Refresh(1)
    End Sub

    Private Sub checkBoxZ_CheckStateChanged(sender As System.Object, e As System.EventArgs) Handles checkBoxZ.CheckStateChanged
        If checkBoxZ.Checked = True Then
            TCUSB16IO.TCUSB16IO_Config(1, &H8)
        Else
            TCUSB16IO.TCUSB16IO_Config(1, &H0)
        End If
        TCUSB16IO.TCUSB16IO_Refresh(1)
    End Sub
End Class
