Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
    Shared _continue As Boolean
    Shared _serialPort As SerialPort
    Dim myPort As String()



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox2.SelectedIndex = 4
        myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
        ComboBox1.Items.Clear()
        For lx = 0 To UBound(myPort)
            ComboBox1.Items.Add(myPort(lx))
            ComboBox1.Text = ComboBox1.Items.Item(0)
        Next
        If UBound(myPort) = -1 Then
            ComboBox1.Text = ""
            If ComboBox1.Text = "" Then

                ComboBox1.Text = "No Ports"
                MetroButton1.Enabled = False




            End If
        Else
            MetroButton1.Enabled = True


        End If



        Timer1.Start()

    End Sub

    Private Sub MetroContextMenuSimple1_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs)

    End Sub

    Private Sub MetroPanelCategoryV1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub MetroButton10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MetroButton10.Click
        TextBox4.Clear()
    End Sub

    Private Sub MetroButton9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MetroButton9.Click
        TextBox3.Clear()
    End Sub

    Private Sub MetroButton8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MetroButton8.Click
        TextBox2.Clear()
    End Sub

    Private Sub MetroButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MetroButton7.Click
        TextBox1.Clear()
    End Sub

    Private Sub MetroButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MetroButton3.Click
        SerialPort1.Write("1" + TextBox1.Text)
    End Sub

    Private Sub MetroButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MetroButton4.Click
        SerialPort1.Write("2" + TextBox2.Text)
    End Sub

    Private Sub MetroButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MetroButton5.Click
        SerialPort1.Write("3" + TextBox3.Text)
    End Sub

    Private Sub MetroButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MetroButton6.Click
        SerialPort1.Write("4" + TextBox4.Text)
    End Sub

    Private Sub MetroButton11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MetroButton11.Click
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
    End Sub

    Private Sub MetroButton12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MetroButton12.Click
        SerialPort1.Write("5")
    End Sub

    Private Sub TabPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage2.Click

    End Sub


    

    


    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MetroButton6.Enabled = False
        MetroButton5.Enabled = False
        MetroButton9.Enabled = False
        MetroButton10.Enabled = False
        TextBox4.Enabled = False
        TextBox3.Enabled = False
        TextBox2.Enabled = True
        TextBox1.Enabled = True
        TextBox1.MaxLength = 8
        TextBox2.MaxLength = 8
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MetroButton6.Enabled = False
        MetroButton5.Enabled = False
        MetroButton9.Enabled = False
        MetroButton10.Enabled = False
        TextBox4.Enabled = False
        TextBox3.Enabled = False
        TextBox2.Enabled = True
        TextBox1.Enabled = True
        TextBox1.MaxLength = 16
        TextBox2.MaxLength = 16
    End Sub

    Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MetroButton6.Enabled = True
        MetroButton5.Enabled = True
        MetroButton3.Enabled = True
        MetroButton4.Enabled = True
        MetroButton10.Enabled = True
        MetroButton9.Enabled = True
        MetroButton8.Enabled = True
        MetroButton7.Enabled = True
        TextBox4.Enabled = True
        TextBox3.Enabled = True
        TextBox2.Enabled = True
        TextBox1.Enabled = True
        TextBox1.MaxLength = 16
        TextBox2.MaxLength = 16
        TextBox3.MaxLength = 16
        TextBox4.MaxLength = 16
    End Sub

    Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MetroButton6.Enabled = True
        MetroButton5.Enabled = True
        MetroButton3.Enabled = True
        MetroButton4.Enabled = True
        MetroButton10.Enabled = True
        MetroButton9.Enabled = True
        MetroButton8.Enabled = True
        MetroButton7.Enabled = True
        TextBox4.Enabled = True
        TextBox3.Enabled = True
        TextBox2.Enabled = True
        TextBox1.Enabled = True
        TextBox1.MaxLength = 20
        TextBox2.MaxLength = 20
        TextBox3.MaxLength = 20
        TextBox4.MaxLength = 20
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If SerialPort1.IsOpen = False Then
            MetroButton3.Enabled = False
            MetroButton4.Enabled = False
            MetroButton5.Enabled = False
            MetroButton6.Enabled = False
            MetroButton7.Enabled = False
            MetroButton8.Enabled = False
            MetroButton9.Enabled = False
            MetroButton10.Enabled = False
            MetroButton11.Enabled = False
            MetroButton12.Enabled = False
            TextBox1.Enabled = False
            TextBox2.Enabled = False
            TextBox3.Enabled = False
            TextBox4.Enabled = False
        Else

            If RadioButton1.Checked = True Then
                MetroButton6.Enabled = False
                MetroButton5.Enabled = False
                MetroButton9.Enabled = False
                MetroButton10.Enabled = False
                MetroButton11.Enabled = True
                MetroButton12.Enabled = True
                MetroButton3.Enabled = True
                MetroButton4.Enabled = True
                MetroButton7.Enabled = True
                MetroButton8.Enabled = True
                TextBox4.Enabled = False
                TextBox3.Enabled = False
                TextBox2.Enabled = True
                TextBox1.Enabled = True
                TextBox1.MaxLength = 8
                TextBox2.MaxLength = 8
            End If
            If RadioButton2.Checked = True Then
                MetroButton6.Enabled = False
                MetroButton5.Enabled = False
                MetroButton9.Enabled = False
                MetroButton10.Enabled = False
                MetroButton11.Enabled = True
                MetroButton12.Enabled = True
                MetroButton3.Enabled = True
                MetroButton4.Enabled = True
                MetroButton7.Enabled = True
                MetroButton8.Enabled = True
                TextBox4.Enabled = False
                TextBox3.Enabled = False
                TextBox2.Enabled = True
                TextBox1.Enabled = True
                TextBox1.MaxLength = 16
                TextBox2.MaxLength = 16
            End If


            If RadioButton3.Checked = True Then
                MetroButton6.Enabled = True
                MetroButton5.Enabled = True
                MetroButton3.Enabled = True
                MetroButton4.Enabled = True
                MetroButton10.Enabled = True
                MetroButton9.Enabled = True
                MetroButton8.Enabled = True
                MetroButton7.Enabled = True
                MetroButton11.Enabled = True
                MetroButton12.Enabled = True

                TextBox4.Enabled = True
                TextBox3.Enabled = True
                TextBox2.Enabled = True
                TextBox1.Enabled = True
                TextBox1.MaxLength = 16
                TextBox2.MaxLength = 16
                TextBox3.MaxLength = 16
                TextBox4.MaxLength = 16
            End If
            If RadioButton4.Checked = True Then
                MetroButton6.Enabled = True
                MetroButton5.Enabled = True
                MetroButton3.Enabled = True
                MetroButton4.Enabled = True
                MetroButton10.Enabled = True
                MetroButton9.Enabled = True
                MetroButton8.Enabled = True
                MetroButton7.Enabled = True
                MetroButton11.Enabled = True
                MetroButton12.Enabled = True
                TextBox4.Enabled = True
                TextBox3.Enabled = True
                TextBox2.Enabled = True
                TextBox1.Enabled = True
                TextBox1.MaxLength = 20
                TextBox2.MaxLength = 20
                TextBox3.MaxLength = 20
                TextBox4.MaxLength = 20
            End If



        End If

    End Sub







    Private Sub RadioButton2_CheckedChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

  
    Private Sub MetroButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MetroButton1.Click
        If MetroButton1.Text = "Connect" Then

            SerialPort1.Close()
            SerialPort1.PortName = ComboBox1.Text
            SerialPort1.BaudRate = ComboBox2.Text
            SerialPort1.DataBits = 8
            SerialPort1.Parity = Parity.None
            SerialPort1.StopBits = StopBits.One
            SerialPort1.Handshake = Handshake.None
            SerialPort1.Encoding = System.Text.Encoding.Default







            SerialPort1.Open()








            MetroButton1.Text = "Disconnect"
            '
            RadioButton1.Enabled = False
            RadioButton2.Enabled = False
            RadioButton3.Enabled = False
            RadioButton4.Enabled = False
            ComboBox1.Enabled = False
            ComboBox2.Enabled = False
            MetroTabControl1.SelectTab(1)
        ElseIf MetroButton1.Text = "Disconnect" Then
            SerialPort1.Close()

            ComboBox1.Enabled = True
            ComboBox2.Enabled = True
            RadioButton1.Enabled = True
            RadioButton2.Enabled = True
            RadioButton3.Enabled = True
            RadioButton4.Enabled = True






            MetroButton1.Text = "Connect"
        End If
    End Sub

    



    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Dim url As String = "https://facebook.com/LiollRomania"
   
        Process.Start(url)
    End Sub

    Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
        Dim url2 As String = "http://lioll.org"

        Process.Start(url2)
    End Sub

  
    Private Sub MetroButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MetroButton2.Click

        myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
        ComboBox1.Items.Clear()
        For lx = 0 To UBound(myPort)
            ComboBox1.Items.Add(myPort(lx))
            ComboBox1.Text = ComboBox1.Items.Item(0)
        Next
        If UBound(myPort) = -1 Then
            ComboBox1.Text = ""
            If ComboBox1.Text = "" Then

                ComboBox1.Text = "No Ports"
                MetroButton1.Enabled = False




            End If
        Else
            MetroButton1.Enabled = True


        End If
    End Sub
End Class


