Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

 (/) Circuits &

Arduino (/circuits/) Workshop


Visual Studio (/workshop/)
- Serial Craft (/craft/)
Communication Cooking(/member/Tedd'scherB/)
by Tedd'scherB (/cooking/) Living (/living/)Follow
Outside (/outside/) Login (/account/login/) | Sign Up

Teachers (/teachers/) (/account/register/)

Download Favorite w I Made It

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

(https://cdn instructables com/FOT/2WSJ/ISCAVG8V/FOT2WSJISCAVG8V


auto=webp&frame=1&fit=bounds) LARGE jpg?

(/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

Why do you make things so complicated?}

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

 Add Tip V Ask Question | Comment Download

Step 1: Create a New VisualStudio Project.

(https://cdn instructables com/FFZ/8WQ5/ISCAUT5K/FFZ8WQ5ISCAUT5K LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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

Type in some name and click OK.

 Add Tip V Ask Question | Comment Download

Step 2: Resize You Form.

/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow

Download Favorite w I Made It

(https://cdn instructables com/FGD/KQ8Q/ISCAUT7E/FGDKQ8QISCAUT7E LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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

 Add Tip V Ask Question | Comment Download

Step 3: Change the Background Color.

(https://cdn instructables com/FWD/6ZB4/ISCAUT8O/FWD6ZB4ISCAUT8O LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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

Download Favorite w I Made It

Step 4: Add 4 Components.

(https://cdn instructables com/FH2/AIS3/ISCAUT9Z/FH2AIS3ISCAUT9Z LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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!

 Add Tip V Ask Question | Comment Download

Step 5: Add a SerialPort-Module

/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow

Download Favorite w I Made It

(https://cdn instructables com/F1B/6EZK/ISCAUTAK/F1B6EZKISCAUTAK LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

From the Tools window, add a component called SerialPort.

 Add Tip V Ask Question | Comment Download

Step 6: Double-Click on Your Form.

(https://cdn instructables com/FRT/341N/ISCAUTFL/FRT341NISCAUTFL LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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

Download Favorite w I Made It

Step 7: Add Some Code.

(https://cdn instructables com/F9J/QEDP/ISCAUTHI/F9JQEDPISCAUTHI LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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.

 Add Tip V Ask Question | Comment Download

Step 8: Double-click on the Connect-Button

/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow

Download Favorite w I Made It

(https://cdn instructables com/FD0/F9C0/ISCAUTIQ/FD0F9C0ISCAUTIQ LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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.

 Add Tip V Ask Question | Comment Download

Step 9: Add Two Lines of Code.

(https://cdn instructables com/FJB/UFIQ/ISCAUTL0/FJBUFIQISCAUTL0 LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)


/
Add
Arduino & Visual the- Serial
Studio following 2 lines ofbycode
Communication inside(/member/Tedd'scherB/)
Tedd'scherB your Private Sub - Follow
Loop of your Connect-Button.

SerialPort1.BaudRate = "9600" Download Favorite w I Made It

SerialPort1.PortName = ComboBox_AvailableSerialPorts.SelectedItem

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

 Add Tip V Ask Question | Comment Download

Step 10: Add Another Line of Code.

(https://cdn instructables com/FUM/EPO3/ISCAUTNC/FUMEPO3ISCAUTNC LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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.

 Add Tip V Ask Question | Comment Download

Step 11: Add 2 More Lines.

/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow

Download Favorite w I Made It

(https://cdn instructables com/FCE/5T9B/ISCAUTOH/FCE5T9BISCAUTOH LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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

Button_Connect.Visible = False
Button_Disconnect.Visible = True

 Add Tip V Ask Question | Comment Download

Step 12: Add an IF Statement.

(https://cdn instructables com/FBE/PRNI/ISCAUTQ9/FBEPRNIISCAUTQ9 LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)


/
Inside
Arduino & Visual Studioyour Private
- Serial Sub - Loop
Communication for the (/member/Tedd'scherB/)
by Tedd'scherB Connect Button again, Followadd an if-Statement.

If SerialPort1.IsOpen = False Then


SerialPort1.Open()
Download Favorite w I Made It

End If

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

 Add Tip V Ask Question | Comment Download

Step 13: Double-click Your Disconnect Button.

(https://cdn instructables com/FGW/ZWFB/ISCAUTSQ/FGWZWFBISCAUTSQ LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

VisualBasic will automatically add these new code-lines.

 Add Tip V Ask Question | Comment Download

Step 14: Programm the Disconnect Button.

/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow

Download Favorite w I Made It

(https://cdn instructables com/FBP/6RCK/ISCAUTTG/FBP6RCKISCAUTTG LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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.

 Add Tip V Ask Question | Comment Download

Step 15: Add a TextBox.

/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow

Download Favorite w I Made It

(https://cdn instructables com/FSE/OQEU/ISCAUTU1/FSEOQEUISCAUTU1 LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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

 Add Tip V Ask Question | Comment Download

Step 16: Add a Timer

(https://cdn instructables com/F7O/VANY/ISCAUTVA/F7OVANYISCAUTVA LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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

/

Arduino & Visual Studio - Serial CommunicationAdd Tip V
by Tedd'scherBAsk Question | Comment
(/member/Tedd'scherB/) Follow Download

Download Favorite w I Made It

Step 17: Change the Timer-Interval.

(https://cdn instructables com/FG2/QDHX/ISCAUTX7/FG2QDHXISCAUTX7 LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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

 Add Tip V Ask Question | Comment Download

Step 18: Add Another Line of Code.

/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow

Download Favorite w I Made It

(https://cdn instructables com/FKI/CHWM/ISCAUTZW/FKICHWMISCAUTZW LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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.

 Add Tip V Ask Question | Comment Download

Step 19: Enabling and Disabling the Timer

(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

 Add Tip V Ask Question | Comment Download


Download Favorite w I Made It

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

(https://cdn instructables com/FBR/5QD7/ISCAUU46/FBR5QD7ISCAUU46 LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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 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.

 Add Tip V Ask Question | Comment Download

/
Step 21: Open Your Masterpiece.
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/)
Follow

Download Favorite w I Made It

(https://cdn instructables com/FCO/JB7L/ISCAUU4V/FCOJB7LISCAUU4V LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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.

 Add Tip V Ask Question | Comment Download

Step 22: Give the Serial Device Something to Do.

(https://cdn instructables com/FKW/TJX0/ISCAUU5F/FKWTJX0ISCAUU5F LARGE jpg?auto=webp&frame=1&width=1024&fit=bounds) /


I use
Arduino & Visual an- Arduino
Studio UNO for this
Serial Communication project. (/member/Tedd'scherB/)
by Tedd'scherB Follow

Program it like in the picture above. Download Favorite w I Made It

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.

 Add Tip V Ask Question | Comment Download

Step 23: Go and Test Your Program.

/
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow

Download Favorite w I Made It

(https://cdn instructables com/FWU/V8CR/ISCAUU66/FWUV8CRISCAUU66 LARGE jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds)

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

HAVE FUN!

 Add Tip V Ask Question | Comment Download

2 People Made This Project!

Thanura Dinesh (/member/Thanura+Dinesh/)


made it! drak3 (/member/drak3/) made it!

Did you make this project? Share it with us!

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/)

VK-01 Off-World Bartender (/id/VK-01-Off- Neopixel LED Face Mask (/id/Neopixel-


World-Bartender/) by Donald Bell LED-Face-Mask/) by splat238
(/member/Donald+Bell/) in Raspberry Pi (/member/splat238/) in LEDs

 23 8 1.3K  139 8 10K

(/contest/handsfree/) (/contest/cnc2020/)

 Add Tip

V Ask Question

| Post Comment

We have a be nice policy.


Please be positive and constructive.

Add Images Post

8 Discussions

(/member/cesarvelez2018/) cesarvelez2018 (/member/cesarvelez2018/) 10 months ago

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.

(/member/MarMaxMar/) MarMaxMar (/member/MarMaxMar/) Tip 1 year ago


Reply / Upvote
1. Change the names of all controls to make them worjk to "Control" + a number like:
Button1 instead of Button_Connect
Button2 instead of Button_Disconnect
Textbox1 instead .....
aso.
/
Then your errors (red lines) will disappear
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow
2. Groupbox is not being used, you can delete it
3. If the program does not find any serial ports at all, change
For Each AvailableSerialPorts As String In SerialPort1.GetPortNames()
Download Favorite w I Made It
to
For Each AvailableSerialPorts As String In SerialPort1.PortName()

/1 (/member/crs.iulia96/) crs.iulia96 (/member/crs.iulia96/) 1 year ago


Reply / Upvote
I used your code, But the data i need to "plot" are some RFID code tags. i followed all the step, but still
shows nothing in the text box. Can u help me ?

(/member/bostulpje/) bostulpje (/member/bostulpje/) Question 2 years ago on Step 22

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

(/member/Aim%25C3%25A9D1/) AiméD1 (/member/Aim%25C3%25A9D1/) 2 years ago

Reply / Upvote
Thnx

(/member/AnnaMai/) AnnaMai (/member/AnnaMai/) 2 years ago


Reply / Upvote
I have same article about this, you and other reader can view here
http://engineer2you.blogspot.com/2016/12/arduino-serial-communication-visual.html

(/member/drak3/) drak3 (/member/drak3/) 2 years ago


Reply / Upvote
thx for this instruction...it was very useful.

Post Comment

Categories About Us Resources

 Circuits  Craft Who We Are Sitemap (/sitemap/)


(/circuits/) (/craft/) (/about/) Help (/id/how-to-
 Workshop  Cooking Why Publish? write-a-great-
(/workshop/) (/cooking/) (/create/) instructable/)
/
 Living  Teachers Jobs (/jobs/) Contact (/contact/)
Arduino & Visual Studio - Serial Communication by Tedd'scherB (/member/Tedd'scherB/) Follow
(/living/) (/teachers/)

 Outside
(/outside/) Download Favorite w I Made It

Find Us

(https://www.instagram.com/instructables/) (https://www.pinterest.com/instructables) (https://www.facebook.com/instructables) (https://www.twitter.com/instructables)

© 2020 Autodesk, Inc.

Terms of Service Privacy Statement Privacy Legal Notices & Trademarks


(http://usa.autodesk.com/adsk/servlet/item? (http://usa.autodesk.com/adsk/servlet/item? settings (http://usa.autodesk.com/legal-notices-
siteID=123112&id=21959721) siteID=123112&id=21292079) | trademarks/)
| |
(http://www.autodesk.com)

You might also like