How to Communicate to the Arduino in Visual Basic .NET

How to Communicate to the Arduino in Visual Basic .NET

Communicating to the Arduino in the .NET platform is pretty much straightforward: The Arduino uses a virtual serial port to allow programs to be written onto it, but we can also use this port to get and send data to and from the Arduino.
  • Arduino or Arduino Compatible Board (UNO, Mega, Etc.)
  • Compatible USB Cable to Connect the Arduino to the Host Computer
  • The Arduino Software (Available Here)
  • VB.NET 2010 or 2012 (Available Here)

I am assuming that you already have a simple knowledge of the .NET and Arduino (software & hardware) platforms. If not, there are many tutorials on the internet to help get you started. Let's begin by putting the program on the Arduino.

Step 1: Adding a Program to Send Data From the Arduino
Let's start by opening up the Arduino software, and copying/pasting in the following code:

void setup() {              
//Sends the Number 1234 Over the Serial Port Once Every Second
void loop() {

This program opens up a serial port, and sends the number string, 1234, over the port once every second in an infinite loop. The number '1234' has no special meaning, you could place any number there in place of it. Then plug in your Arduino board and upload the code to it. Leave it plugged in.

Step 2: Creating the .NET Program
Creating the .NET code in VB.NET is actually quite simple and easy to understand. Open a new VB.NET console program, clear all of the code, and paste in the following:

Module Module1
    Sub Main()
        Dim userRespond
        Dim fetchedData
        Dim writeString
        Console.WriteLine("Type R to Read Serial Port.")
        userRespond = Console.ReadLine
        If userRespond = "R" Or userRespond = "r" Then
            Dim port As New System.IO.Ports.SerialPort
            port.PortName = "COM4"
            port.BaudRate = 9600
            fetchedData = port.ReadByte
            Console.WriteLine("Not a Recognized Command.")
        End If
    End Sub
End Module

This code should be fairly simple. It opens a new console window, then it asks the user if they want to get the data from the port, next (if they said yes), it opens up a new COM port and gets the data, finally it writes it to the console. You may need to change COM4 to the correct port for your Arduino. It lists what port Arduino is on in the Arduino software bottom right corner. You can not write a new program to the Arduino when this software is running, for it gives an error that says that the COM port is busy.

Step 3: Testing
To use this program, be sure your Arduino is plugged in, and it is running the code from step 1. Now, run the program from above and type in R. It may take a second (the Arduino only sends it once per second), but the number 1234 will come up! If you change the number 1234 in the Arduino code to "hello" and upload it and run the .NET program, it will only give you the decimal value for a lower-case h. This is because this program can only read one byte at a time. See if you can change it up to read the whole word and convert it to unicode! Post your inventions!

Leave a Comment
  • Please add 7 and 2 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Wikis - Comment List
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
  • forgot semicolon on two lines in Arduino Program in Loop section:



    Also unused variable in.NET

    Dim writeString

    Also running the program VB.NET 2012 Professional, on Windows 8; when pressing either "R" or "r" the Arduino returns 210 in the console window.

    And changing it to be a string of Serial.write("1234") it returns 49 in the console window. not sure if this is HX or what's going on?

Page 1 of 1 (1 items)