Arduino & Visual Studio - Serial Communication - 23 Steps - Instructables
Arduino & Visual Studio - Serial Communication - 23 Steps - Instructables
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?}
1 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
I was mad, but looked forward to solve the problem. And here, I wanna post the results.
2 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
Grab one corner and resize it, for example, like in the picture.
To make everything nice and good-looking, in the properties menu, change BackColor to Window.
3 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
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!
4 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
You will see, that a new tab will open. Here, you can type in your code.
5 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
Add the following code to your project like in the picture above.
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.
6 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
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.
7 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
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.
8 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
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.
9 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
Insied you Private Sub for the Connect - Button, add these lines of code:
Button_Connect.Visible = False
Button_Disconnect.Visible = True
10 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
Inside your Private Sub - Loop for the Connect Button again, add an if-Statement.
This statement openes the Serial Port. But only, if it's not connected already. This prevents us from and
Runtime-Exception.
11 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
12 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
Inside your Private Sub - Loop for the Disconnect-Button, add this code:
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.
13 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
Add a TextBox, and under the Properties Menu, change Multiline to True.
14 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
Under Properties >> Interval, change the Interval to 100, so it ticks every 100 Milliseconds.
15 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
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.
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
16 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
Double-Click the timer and add the following code inside your Private Sub - Loop for the Timer:
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.
17 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
On the top hand corner of VisualStudio, click on Start [or the green Play-Button]to start your program.
18 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
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.
19 of 20 28/04/2025, 16:08
Arduino & Visual Studio - Serial Communication : 23 Steps - Instructables https://www.instructables.com/Arduino-Visual-Studio-Serial-Communic...
Test it, and, if necassary, add some lines of code or remove some.
HAVE FUN!
20 of 20 28/04/2025, 16:08