Public Class Motor_Control 'Here we make declaration of the variables that we use 'Variable "Conectado" allows us to know the status of conection Dim Conectado As Boolean 'The variable "valor" will be the one that receives the value that we will send to Arduino Dim value As String 'At Close "Motor_Control" form also close the serial port "SPort" Private Sub Motor_Control_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed SPort.Close() End Sub 'When loading the form we disable the "ON" button and "OFF" Private Sub Motor_Control_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load CmdON.Enabled = False CmdOFF.Enabled = False End Sub 'The button "Connect" allows us to connect the PC to the Arduino after select serial port in the Combobox "cmbSerialPorts" CmdConectar_Click Private Sub (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConectar.Click 'If there is no conection If Conectado = False Then 'Load the combobox with the PC serial ports For Each PuertosDisponibles As String In My.Computer.Ports.SerialPortNames cmbSerialPorts.Items.Add (PuertosDisponibles) Next 'If anything in Combobox list If cmbSerialPorts.Items.Count> 0 Then 'Change the combobox text cmbSerialPorts.Text = cmbSerialPorts.Items (0) 'Change the value of the name of the serial port to selected value in combobox list SPort.PortName = cmbSerialPorts.Text 'Open the serial port SPort.Open () 'And activate the "ON" button "OFF" CmdON.Enabled = True CmdOFF.Enabled = True 'We disabled the button "Connectar" cmdConectar.Enabled = False MsgBox ("CONECTADO") 'The variable "Conectado" is true "Conectado" = True else 'If no ports show a message MsgBox ("NO PORT FOUND") End If Else 'At the end we close the port SPort.Close () MsgBox ("DISCONECTED") Conectado = False End If End Sub Private Sub CmdON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdON.Click 'If there conection If Conectado = True Then 'The variable "value" have the value "1" to activate the motor value = "1" 'Send the value to the port SPort.Write (value) 'Discards data from the serial port transmit buffer SPort.DiscardOutBuffer () End If End Sub CmdOFF_Click Private Sub (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdOFF.Click If Conectado = True Then 'The variable "value" have the value "0" to desactivate the motor value = "0" SPort.Write (value) SPort.DiscardOutBuffer () End If End Sub End Class