Professional Documents
Culture Documents
Arduino & Visual Studio - Serial Communication - 23 Steps - Instructables
Arduino & Visual Studio - Serial Communication - 23 Steps - Instructables
instructables (/circuits/)
circuits
Projects (/circuits/projects/) Contests (/contest/) Let's Make...
Arduino & Visual Studio - Serial Communication
By Tedd'scherB (/member/Tedd%2527scherB/) in Circuits (/circuits/) > Arduino (/circuits/arduino/projects/) 45,890 44 8
Download Favorite
(/member/Tedd%2527scherB/)
By Tedd'scherB
(/member/Tedd%2527scherB/)
More by (/id/Arduino-
ICY-BEAR.COM (http://Icy-
the author: LiPo-
Bear.com) Battery-
Follow
About: After reading this, you won't be more clever then before! More About Tedd'scherB » (/member/Tedd%2527scherB/)
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
Arduino & Visual it is the
Studio - Serial same programming
Communication language,
by Tedd'scherB the code didn't
(/member/Tedd'scherB/) Follow work. There were no errors
found, but it cound't receive serial data.
Download
{"Sometimes, I hate you, Microsoft!" Why are you always changing the way VisualStudio works?
Favorite w I Made It
I was mad, but looked forward to solve the problem. And here, I wanna post the results.
/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow
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.
/
Arduino & Visual Studio - Serial CommunicationAdd Tip V
by Tedd'scherBAsk Question | Comment
(/member/Tedd'scherB/) Follow Download
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!
/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow
You will see, that a new tab will open. Here, you can type in your code.
/
Arduino & Visual Studio - Serial CommunicationAdd Tip V
by Tedd'scherBAsk Question | Comment
(/member/Tedd'scherB/) Follow Download
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.
/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow
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.
SerialPort1.PortName = ComboBox_AvailableSerialPorts.SelectedItem
And, like always, don't forget to choose the right names for your components.
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 nished.
/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow
Insied you Private Sub for the Connect - Button, add these lines of code:
Button_Connect.Visible = False
Button_Disconnect.Visible = True
End If
This statement openes the Serial Port. But only, if it's not connected already. This prevents us
from and Runtime-Exception.
/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow
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.
/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow
Add a TextBox, and under the Properties Menu, change Multiline to True.
/
Arduino & Visual Studio - Serial CommunicationAdd Tip V
by Tedd'scherBAsk Question | Comment
(/member/Tedd'scherB/) Follow Download
Under Properties >> Interval, change the Interval to 100, so it ticks every 100 Milliseconds.
/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow
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.
(https://cdn instructables
auto=webp&frame=1&width=1024&height=1024&fit=bounds)
com/FD9/NWAQ/ISCAUU0L/FD9NWAQISCAUU0L LARGE jpg? (https://cdn instructables
auto=webp&frame=1&width=1024&height=1024&fit=bounds)
com/FR7/OFFQ/ISCAUU21/FR7OFFQISCAUU21 LARGE jpg?
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:
/
Arduino & Visual Timer1.Enabled = False
Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow
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 rst, 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.
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/)
Follow
On the top hand corner of VisualStudio, click on Start [or the green Play-Button]to start your
program.
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 nd a solution for it.
In a few days, I will create another tutorial for an advanced Serial Communication between
VisualStudio and Arduino.
/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow
Test it, and, if necassary, add some lines of code or remove some.
HAVE FUN!
I Made It!
/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow
Recommendations
Download Favorite w I Made It
(/id/VK-01-Off-World-Bartender/) (/id/Neopixel-LED-Face-Mask/)
(/contest/handsfree/) (/contest/cnc2020/)
Add Tip
V Ask Question
| Post Comment
8 Discussions
Reply / Upvote
Cordial saludo... excelente tutorial... podrias explicar algo con varios sensores desde arduino a visual
estudio y ver los resultados en label separados? agradezco tu ayuda.
Answer / Upvote
With your project I want to communicate between pc and Arduino Uno.
I followed the Steps. Step 1....Step 6 no problem. But as soon as code is added then there are errors
(code BC30451) telling that Variables are not declared. Up to Step 20 25 errors.
What do you mean with: "And like always, do n't forget to choose the right name for your components"
? I did not change any name.
I am using Visual Studio 2017 ( v. 15.7.5)
I am a newbe. What am I doing wrong?
1 answer F
Reply / Upvote
Thnx
Post Comment
Outside
(/outside/) Download Favorite w I Made It
Find Us