One of the main features you can add to your Arduino, is communicating with it via an application. You can easily and quickly use VB.NET to send and receive information from the serial COM port that your Arduino uses.
We are using Visual Studio 2015 for this tutorial, load it up and create a new project. We have named ours COMTest and saved it to the Desktop folder. Now that you have a basic application to work with, take a look at the following piece of code:
For Each p As String In My.Computer.Ports.SerialPortNames ListBox1.Items.Add(p) Next
As you can see, you can get a list of available COM ports on the system, this is useful as the user does not have to select the correct COM port.
You can either place this list of COM ports in a ListBox, or even better, configure your project to ‘auto’ connect to a COM port. (as seen below) This involves sending a byte to each of the COM ports and checking the response, in your Arduino sketch you can set it to respond to a certain string – If it matches this in VB.NET when you read the COM port then you have the right connection.
You can see this method with the following piece of code (VB.NET):
For Each p As String In My.Computer.Ports.SerialPortNames Try Using com As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort(p) com.ReadTimeout = 1000 com.WriteLine("t") Dim data As String = com.ReadLine() If InStr(data, "OK") Then comPort = p connected = True End If com.Close() End Using Catch ex As Exception End Try Next
As you can see above, we loop through the list of available COM ports on the system and then try and send a string “t” to the Arduino and check for a response. If the response equals “OK” we know this COM port has our Arduino connected. (Note this would only work with 1 connected Arduino, extra programming would be required for multiple devices) If it’s “OK” then we set a global variable comPort and connected, so that we can use them elsewhere in the program.
The code above also shows that we are wrapping this in a Try/Catch statement, this is because we’re trying to send data to all COM ports and we may receive an error message otherwise.
We have written a basic function to open a COM port, send a string and close it again. The comPort variable is filled in by using the above auto connect code, you can run this on the Form_Load event:
Sub sendCommand(data As String) If data <> "" Then Try Using com As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort(comPort) com.WriteLine(data) com.Close() End Using Catch ex As Exception MessageBox.Show(ex.Message) End Try End If End Sub
And in the Arduino sketch, respond to a certain string. We do this by reading the Serial data in the Loop, whilst the Serial is available. If this equals ‘t’ then we respond with an OK string. The .NET application now knows that it’s talking to our Arduino.
byte byteRead; void setup() { Serial.begin(9600); } void loop() { if (Serial.available()) { byteRead = Serial.read(); switch (byteRead) { case 't': Serial.println("OK"); return; break; default: break; } } }
That should be all you need to get started with the basics of connecting and sending/receiving data to/from the Arduino on the serial ports. This should work with any Arduino.