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.
Materials:
  • 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() {              
    Serial.begin(9600); 
}
 
//Sends the Number 1234 Over the Serial Port Once Every Second
void loop() {
Serial.write(1234)
delay(1000)
}

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.Open()
            port.BaudRate = 9600
            fetchedData = port.ReadByte
            Console.WriteLine(fetchedData)
            Console.ReadLine()
  Else
            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!

Sort by: Published Date | Most Recent | Most Useful
Comments
  • forgot semicolon on two lines in Arduino Program in Loop section:

    Serial.write(1234);

    delay(1000);

    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?

  • @dbp427:  You are only reading a single byte.  The maximum a byte can hold is 255.  The system is returning the remainder of 210.  1234/256=4.820313;  .820313*256=210 or another way of saying this is it is reading the low byte.

Page 1 of 1 (2 items)