Error Message - Visual Basic


I've made a program that controls my arduino with Visual Basic.

However currently when I start the program with the Arduino not connected and use a command I will get an error message that port com3 doesn't exist (serialport). Instead of crashing and showing that error I would like my program to create a popup and show a message a long the lines of "please connect your Arduino before continuing". I was wondering if any1 had any idea how to do this.

Thanks in advance :)


You can do a few things

First of all use Try/catch exception handling so that your program does not crash


 myport = new SerialPort() ... bla blah ...

Catch (ex as Exception)
  Windows.Forms.MessageBox.Show("Unable to connect to arduino" + vbcrlf + ex.Message)
End Try

If you want to check if the port is available before hitting the error then...

There is a list (array of string port names) of serial ports in the System.IO namespace and also this article might be useful although I use .IO instead of My....