Introduction: How to Auto-detect and Connect to Arduino in Visual Studio Using Visual Basic

Picture of How to Auto-detect and Connect to Arduino in Visual Studio Using Visual Basic

How to auto detect what port the arduino or geekduino is connected to using visual basic and automatically connect to it!! (later I will add how to auto detect when it is unplugged!).

Note: button is not necessary but there in case you plug your arduino in after opening the program.

Step 1: Visual Studio

1. Add the following to your Windows Form in design view:

  • Timer and call it TimerConnect.
  • Listbox and call it lstConsole.
  • Button and call it btnConnect.

2. Add Reference IO ports at top of code:

Imports System.IO.Ports

3. Set a global variable at the beginning of the Public Class:

Dim ArduinoConnected As Boolean

4. Add these to your Form Load sub:

TimerConnect.Enabled = False
ArduinoConnected = False
autoconnect()

3. Add these subs to the script:

Private Sub BtnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
If ArduinoConnected Then SerialPort.Close() btnConnect.Text = "Connect" Else autoconnect() End If End Sub
Private Sub autoconnect()
For Each sp As String In My.Computer.Ports.SerialPortNames Try SerialPort.PortName = sp SerialPort.BaudRate = 9600 SerialPort.DataBits = 8 SerialPort.Parity = Parity.None SerialPort.StopBits = StopBits.One SerialPort.Handshake = Handshake.None SerialPort.Encoding = System.Text.Encoding.Default SerialPort.Open() SerialPort.Write("3/") TimerConnect.Interval = 500 TimerConnect.Start() While TimerConnect.Enabled And ArduinoConnected = False Application.DoEvents() End While If ArduinoConnected Then btnConnect.Text = "Disconnect" Exit For End If SerialPort.Close() Catch ex As Exception MsgBox(ex.Message) End Try Next If ArduinoConnected = False Then MsgBox("Arduino failed to connect. Please check that it is plugged in.") End If
End Sub
Public Sub TimerConnect_Tick(sender As Object, e As EventArgs) Handles TimerConnect.Tick
TimerConnect.Stop()
End Sub
Public Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
Dim str As String = SerialPort.ReadExisting()
If str.Contains("Arduino") Then
ArduinoConnected = True
lstConsole.Items.Add("Arduino Connected")
End If
End Sub

Step 2: Arduino Code

void setup()
{
Serial.begin(9600);
}

void loop()
{

if (Serial.read() == "3/")

{
Serial.print("Arduino");
}

}

Comments

diystuffandfun (author)2017-09-09

Just for anyone have trouble to make this as for the Serial reference. No need to add any reference just import from toolbox the serial port tool. Just make sure you have same name in code and form.

Thanks for you helpfull tutorial.

seamster (author)2016-03-08

Thank you for sharing, welcome to instructables! :)

About This Instructable

1,875views

3favorites

License:

More by CyborgDroid:How to auto-detect and connect to Arduino in Visual Studio using Visual Basic
Add instructable to: