VB.NET to Arduino Comms




About: I have been working in IT since the mid 1980's. Most of that has been database and application development. I've been working on Internet application development since the late 1980's. I've just moved back ...

I wanted to be able to send messages (instructions) from my desktop PC to my Arduino ... I'd like to have my Arduino send instructions as a consequence to other connected devices via I2C, but that's another issue for later.

For now, let's just get our PC and Arduino have a meaningful dialog. Who knows what may blossom from there?

The goal is fairly simple, I will use System.IO.Ports.SerialPort from the .NET side to communicate with the Arduino with Serial on the other side.

This is based on some other resources on the interweb that I found (and since lost) where this is done.

In this instructable, I've changed the code slightly to better suit my requirements on both the .NET and Arduino side of the fence.

Here we go.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: VB.NET Side

We start with a Windows Forms application and dive right into Form1.vb code (all of this is in the attached Form1.vb)

As I want the application to use the IO subsystem, we want to reference System.IO

Imports System.IO

This will be events driven, so we instantiate that

Dim WithEvents SerialPort As New IO.Ports.SerialPort

The interface is designed with a combo box (txtPort) which is populated by the RefreshPorts method. This method simply writes the serial port names into the combo box from the list of available ports in Windows. If there are more than 0 ports, select the first one. Whenever you change the selection, the selected COM port is shown in the application title.

The refresh button is intended to refresh the combo box (in case you want to plug another device in, or disconnect one).

When the application attempts to send a serial message, it makes sure that the COM port is open and configured correctly. If the port is already open, the application closes it and opens it with the currently selected configuration (so that you don't have the problem of trying to open a port that is already open or try to open a different port when one is already open ...

When you click on the send button, the application sends the string to the Serial Port.

When data is returned (by the Arduino) the received data is sent to the txtSerialText field.

The Clear button, simply clears the txtSerialText field.

The Exit button closes the application.

Step 2: The Arduino Side

The attached sketch is the one that I've used for this instructable.

First, I've set up a char variable to receive the incoming data

The setup() function simply sets up the Arduino Serial connection (9600 baud)

The loop() function "listens" for an available Serial communication

When it executes, the loop sets the incomingByte value to the Serial.read() ... that is, whatever the incoming byte of data is.

The loop() then writes the incomingByte value back to the Serial connection. This is then picked up by the VB.NET application.

Step 3: Interface Stuff

The first image is of the populated COM combo box where I have two boards attached to the computer. COM8 is an Arduino UNO that is running the Arduino As ISP sketch, while COM7 is a Freetronics Eleven running the ArduinoSerial sketch (our responder).

The second image shows the response from the Arduino.

The original version of this application that I derived this application from had the incomingByte variable (in the Arduino sketch) as an integer and the Serial.println method in the loop function sending back incomingByte as a DEC value.

I modified the sketch so that it receives a char and sends one back too.

Where to from here?

Next, I'll extend the Arduino sketch so that it performs various tasks based on the input. For instance a switch function that sends data/instruction to an attached ATTiny via I2C.

I'll need to change the sketch so that it receives whole instruction strings rather than just a letter, as I'll want to be able to direct the instruction to one of multiple connected devices with parameters.

Anyway ... that's it for now. Have Fun!

Step 4: Moving on ... a Char Array in Arduino

As I mentioned previously, I want to be able to send a line of text, an instruction and parameters from the PC to the Arduino.

I added the integer variable lf with a value of 10 and then changed the variable incomingBytes from a char to a char array and then changed from incomingBytes = Serial.read() to Serial.readBytesUntil(lf, incomingBytes, 80);, this means that the sketch will read the inbound serial data into an array of 80 elements (my buffer) and then write the result out through the Serial.println(incomingBytes) method.

Interestingly, I noticed that when the input value is 100 characters in length, the sketch will output the initial array of 80 elements, then read the next serial block, and then print that ... that could be a useful piece of functionality.

Finally, I need to clear the array using:

memset(incomingBytes, 0, sizeof(incomingBytes));

This clears the incomingBytes array. Without this function the array didn't seem to clear when new data was received ... anyway, that seems to have fixed the problem that I was seeing ... maybe I have mistaken the result, but the new result is what I want.

Again, the entire sketch is included for your entertainment.

Nothing needed to be changed in the VB.NET application, so no updates there.

What I'm thinking of here is to receive a line of text such as:


and then for the Arduino to split it into three instructions (X, Y and Z) sending these instructions to three ATTiny84 chips to move a step motor to those coordinates. There's a LOT missing in that simple statement, such as timing etc., but I'm considering implementing this in the VB.NET code ... maybe chasing fairies ... maybe not. I won't know until I've gone further with this design.

Coded Creations

Participated in the
Coded Creations

Be the First to Share


    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest

    12 Discussions


    3 years ago

    I think this is a good article. I have been a VB programmer and use VB 6.0 which is becoming a bit out dated. I did a project similar to this but it used ZigBee radios and an UNO board to control servos with a joystick (basically a radio control).

    I would love to have posted it here but my computer crashed and I lost everything.

    Keep up the great work!

    1 reply

    Reply 3 years ago

    Hey AndrewW180 ... bummer about the crash :( that can be a pain. I remember VB6, I made a reasonable living as a VB6 programmer ... writing COM+ applications for web servers ... ahh those were the days ... infinitely better than VB1.0. Nostalgia ....


    3 years ago

    Hey Its Nice Article But Needa Help , i was trying to run the Form1.VB fille through Visual studio, but its not Working Some how um new on VB.NET.Help Me

    1 reply

    Reply 3 years ago

    Hi LẫsìthãS, by "not working" what do you mean? What version of visual studio are you using? I wrote and compiled under 2010 without any problems. Without knowing what error you're getting, anything would be guess-work.


    4 years ago

    Why waste your time with VB.Net? Why not learn C/C++? You can do so much more with them...

    3 replies

    Reply 4 years ago

    Just use what you like and enjoy the results. Thanks for sharing!


    Reply 4 years ago on Introduction

    Hi psyberbob, thanks for the advice ;) Why bother? I've been programming with basic, visual basic, gwbasic for more than 30 years. Also, the application does what it needs to do with little effort, which is in the spirit of an instructable.

    I completely take your point, you can do more with C/C++ (and I do).