Introduction: Arduino & Visual Studio - Serial Communication

About: After reading this, you won't be more clever then before!

The reason for this project is to show you, how to manage Serial connections in Visual Studio 2015 with VisualBasic as the main programming language.

A few days ago, I copyed a project from VisualBasic 2010 to Visual Studio inside a VisualBasic project.

Altough it is the same programming language, the code didn't work. There were no errors found, but it cound't receive serial data.
{"Sometimes, I hate you, Microsoft!" Why are you always changing the way VisualStudio works? Why do you make things so complicated?}

I was mad, but looked forward to solve the problem. And here, I wanna post the results.

Step 1: Create a New VisualStudio Project.

Choose New Project...>>Visual Basic>>Windows Forms Application.

Type in some name and click OK.


Step 2: Resize You Form.

Grab one corner and resize it, for example, like in the picture.

Step 3: Change the Background Color.

To make everything nice and good-looking, in the properties menu, change BackColor to Window.

Step 4: Add 4 Components.

Add:

1x GroupBox

1x ComboBox

2x Buttons

You can type any name and text inside these components you wanto to, but please, be careful when programming!

Step 5: Add a SerialPort-Module

From the Tools window, add a component called SerialPort.

Step 6: Double-Click on Your Form.

You will see, that a new tab will open. Here, you can type in your code.

Step 7: Add Some Code.

Add the following code to your project like in the picture above.

For Each AvailableSerialPorts As String In SerialPort1.GetPortNames()

ComboBox_AvailableSerialPorts.Items.Add(AvailableSerialPorts)
SerialPort1.ReadTimeout = 2000

Button_Connect.Visible = True
Button_Disconnect.Visible = False

Next

Be sure to put it into the Private Sub - Loop that we created by double-clicking onto the Form.

Also, be sure to choose the right names for the components.

Step 8: Double-click on the Connect-Button

Double click on the connect button. You will see, that you will add a new Private Sub - Loop to your code.

In the pictures above, every new code that we add will be marked blue.

Step 9: Add Two Lines of Code.

Add the following 2 lines of code inside your Private Sub - Loop of your Connect-Button.

SerialPort1.BaudRate = "9600"

SerialPort1.PortName = ComboBox_AvailableSerialPorts.SelectedItem

And, like always, don't forget to choose the right names for your components.

Step 10: Add Another Line of Code.

Inside your Private Sub - Loop for your Form, add the following code:

ComboBox_AvailableSerialPorts.Text = AvailableSerialPorts

This prevents the user from connecting to the serial port without having chosen one. It always chooses the last available Serial Port, but you will see that later when we've finished.

Step 11: Add 2 More Lines.

Insied you Private Sub for the Connect - Button, add these lines of code:

Button_Connect.Visible = False
Button_Disconnect.Visible = True

Step 12: Add an IF Statement.

Inside your Private Sub - Loop for the Connect Button again, add an if-Statement.

If SerialPort1.IsOpen = False Then
	SerialPort1.Open()
End If

This statement openes the Serial Port. But only, if it's not connected already. This prevents us from and Runtime-Exception.

Step 13: Double-click Your Disconnect Button.

VisualBasic will automatically add these new code-lines.

Step 14: Programm the Disconnect Button.

Inside your Private Sub - Loop for the Disconnect-Button, add this code:

If SerialPort1.IsOpen = True Then
	SerialPort1.Close()
End If


Button_Connect.Visible = True
Button_Disconnect.Visible = False

This only closes the SerialPort, if it's already opened. This prevents us from an Runtime Exception.

Also, it enables the Connect-Button and Disables the Disconnect-Button again, so that you can connect to another or the same Serial Port again.

Step 15: Add a TextBox.

Add a TextBox, and under the Properties Menu, change Multiline to True.

Step 16: Add a Timer

From the Tools - Menu, add a Module called Timer.

Step 17: Change the Timer-Interval.

Under Properties >> Interval, change the Interval to 100, so it ticks every 100 Milliseconds.

Step 18: Add Another Line of Code.

Let's go inside the Private Sub - Loop for your Form, and add one line of code:

TextBox_ReceivedMessage.ScrollBars = ScrollBars.Vertical

With that line, we add a scroll bar to your TextBox in order for you to see all receved data.

Step 19: Enabling and Disabling the Timer

Under the Private Sub - Loop for the Connect-Button, Enable the Timer:

Timer1.Enabled = True

And under the Private Sub - Loop for the Disconnect-Button, Disable the Timer:

Timer1.Enabled = False

Step 20: Double-click the Timer and Add Some Code.

Double-Click the timer and add the following code inside your Private Sub - Loop for the Timer:

If SerialPort1.IsOpen = True Then
	
	Dim ReceivedMessage As String
	ReceivedMessage = SerialPort1.ReadLine

	TextBox_ReceivedMessage.Text = TextBox_ReceivedMessage.Text + ReceivedMessage + Environment.NewLine
	
	TextBox_ReceivedMessage.SelectionStart = TextBox_ReceivedMessage.Length
	TextBox_ReceivedMessage.ScrollToCaret()

End If

At first, we check if the SerialPort is opened. If it is, we create a new String called ReceivedMessage, that contains the Messages from the Serial Port.

Then, we add text to the TextBox. The old text from the TextBox should stay there, the content from the String is added, and a new line will be created.

Last, the TextBox will automatically scroll the the latest input, so we'll always see the latest messages and don't have to do so by hand.

Step 21: Open Your Masterpiece.

On the top hand corner of VisualStudio, click on Start [or the green Play-Button]to start your program.

But please, don't do anything for now inside it.

Step 22: Give the Serial Device Something to Do.

I use an Arduino UNO for this project.

Program it like in the picture above.

I did that in codebender.cc, but you can to that in your Arduino IDE, or any other IDE, too.

!!Be careful! Don't change the delay to any higher number than 100 Milliseconds. If you do so, you have to change the Interval for the Timer in VisualStudio, too!!

But, of course, inside your Arduino, you can type in any lower number than 100, without changing the Interval in VisualStudio.

That means: The Interval for the Timer in Visual Studio is not allowed to be lower than the delay for the Arduino. But it is allowed to be higher.


And: do never connect to any other Serial Port than to your Arduino, or completely remove or outcomment the Private Sub for the Timer. But, if you do that, you can't receive any SerialData anymore.

Because if the SerialPort-Module can't read any data from the SerialPort, the program will go into a Runtime Exception.

If it does so, just close the program and open it again, and then connect to the proper Serial Port.

Of course, if you want to, you can prevent this bug (Is it?) if you find a solution for it.

In a few days, I will create another tutorial for an advanced Serial Communication between VisualStudio and Arduino.

Step 23: Go and Test Your Program.

Test it, and, if necassary, add some lines of code or remove some.



HAVE FUN!