Thank you for visiting → Compete For Nothing.com ← If you find anything useful leave me a comment and share. Interested in Arduino, C#, Unity3D, Game Creation, VB.NET, learning and most important of all Sharing!.
Close Notification
Open Notification

Arduino Serial Communication using VB NET SIMPLE VERSION – Source Code

Arduino Serial Communication using VB NET SIMPLE VERSION – Source Code
GARD Pro Not Registered

Example of Serial Communication between any Arduino and VB.NET.

The last tutorial that I made for serial communication between arduino and vb.net was
a basic but after going over it again I realized that it was full of other stuff, code that
would change buttons and check textboxes for text then disable ect….which in a tutorial really
pisses me off because it clutters up the code and I get lost. So today I thought I would
re-do it and make it as simple as it should be :)

GARD Pro Not Registered

First declare our serial port

    Dim WithEvents SerialPort As New IO.Ports.SerialPort

Create a button called btnSend and a textbox called txtSend

GARD Pro Not Registered
    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        If SerialPort.IsOpen Then
            SerialPort.Write(txtSend.Text)
        Else
            ConnectSerial()
            SerialPort.Write(txtSend.Text)
        End If
    End Sub

Try to connect to Arduino on startup

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ConnectSerial()
    End Sub

Ahh now for the Connection Sub

    Private Sub ConnectSerial()
        Try
            SerialPort.BaudRate = 9600
            SerialPort.PortName = "COM4" 'notice how the ports are named? they HAVE to have COM in front of the number
            SerialPort.Open()
        Catch
            SerialPort.Close()
        End Try
    End Sub

Now to Receive our data from serial and display it in a textbox called SerialText

GARD Pro Not Registered
    Delegate Sub myMethodDelegate(ByVal [text] As String)
    Dim myD1 As New myMethodDelegate(AddressOf myShowStringMethod)

    Sub myShowStringMethod(ByVal myString As String)
        'display text to our textbox called SerialText
        SerialText.AppendText(myString)
    End Sub

    Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
        Dim str As String = SerialPort.ReadExisting()
        Invoke(myD1, str)
    End Sub

That’s it congratulation you can now send and receive from VB.NET and your Arduino projects!!!

But wait how do we test it?

We need to load a basic sketch onto an Arduino to let us
know if it really receives any data

This code came from arduino.cc

	int incomingByte = 0;
	void setup(){
	   Serial.begin(9600); 
	}

	void loop(){
			if (Serial.available() > 0) {
					// read the incoming byte:
					incomingByte = Serial.read();

					// say what you received in ASCII
					Serial.print("I received: ");
					Serial.println(incomingByte, DEC);
			}
	}

Dogecoin
Donate Dogecoins: DGkJRWQCyBUeXbenYz18LRN9X67aora9nz Whats This?

5 Comments

  1. Arduino Serial Communication using VB NET SIMPLE VERSION ... | Arduino, vb.net, Unity3D, FPV | Scoop.it · January 12, 2013 Reply

    […] The last tutorial that I made for serial communication between arduino and vb.net was a basic but after going over it again I realized that it was full of other stuff, code that would change buttons and check textboxes for text then …  […]

  2. Ady · July 11, 2013 Reply

    Warren, i want to display the data by using the button with click event, but the data isnt displayed. i sent the character c to arduino and the arduino give me the data. below is my code

    Private Sub btnSensor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSensor.Click
    Dim sensor As String = Nothing
    sensor = “c”
    SerialPort1.Write(sensor)
    Dim distance As String = SerialPort1.ReadExisting()
    ListBoxOutput.Items.Add(distance)

    have you used this kind of method?

    thanks

    • warren · July 11, 2013 Reply

      Hey Ady, you are trying to send data and receive data inside the Sub. try and edit myShowStringMethod to filter your results. This is UNTESTED i just wrote it and cant test it but your get the idea.

      Dim AddToListbox As Boolean = False

      Private Sub btnSensor_Click()
      ConnectSerial()
      AddToListbox = True
      Dim sensor As String = Nothing
      sensor = ā€œcā€
      SerialPort1.Write(sensor)
      End Sub

      Private ButtonClickReceivedSub()
      Dim distance As String = SerialPort1.ReadExisting()
      ListBoxOutput.Items.Add(distance)
      End Sub

      Sub myShowStringMethod(ByVal myString As String)
      If AddToListbox = True Then ‘display text to our Listbox
      ListBoxOutput.Items.Add(distance)
      AddToListbox = False
      Else
      SerialText.AppendText(myString) ‘display text to our textbox called SerialText
      End If
      End Sub

      let me know how you go.

  3. nana · August 25, 2015 Reply

    Hi warren,
    I tried the code above in VB2010. Sadly, transmitted data from VB to Arduino was not achieved. I did exactly from your tutorial and it didn’t work well (of course I changed what I supposed to change like COM port no). Received data from Arduino to VB was okay and I did receive at VB whatever I sent from Arduino.

    Regards,
    Nana

    • warren · August 25, 2015 Reply

      Hi Nana did you put the code in the Arduino?

      
      	int incomingByte = 0;
      	void setup(){
      	   Serial.begin(9600); 
      	}
      
      	void loop(){
      			if (Serial.available() > 0) {
      					// read the incoming byte:
      					incomingByte = Serial.read();
      
      					// say what you received in ASCII
      					Serial.print("I received: ");
      					Serial.println(incomingByte, DEC);
      			}
      	}
      

Leave a Reply to warren Cancel reply