# Receive and Display Serial Data in Visual Basic?

**URL:** <https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919>\
**Category:** Interfacing w/ Software on the Computer\
**Created:** [June 19, 2013, 1:52pm UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919 "2013-06-19T13:52:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 19, 2013, 1:52pm UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/1 "2013-06-19T13:52:40Z")

</div>

hi all,

i got a problem to receive the data from sensor into my visual basic

this is my code for arduino

int trigPin = 3;  
int echoPin = 2;

void setup(){  
Serial.begin(19200);  
pinMode(trigPin, OUTPUT);  
pinMode(echoPin, INPUT);  
}

void loop(){  
int Duration, Distance;  
digitalWrite(trigPin, HIGH);  
delayMicroseconds (1000);  
digitalWrite(trigPin, LOW);  
Duration = pulseIn(echoPin, HIGH);  
Distance = (Duration/2) / 29.1;  
Serial.print(Distance);  
Serial.println(" cm");  
delay(500);  
}

this is my code for visual basic

Imports [System.IO](http://System.IO)  
Imports System.IO.Ports  
Imports System.Threading

Public Class Form1  
Dim myPort As Array  
Delegate Sub SetTextCallBack(ByVal

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 19, 2013, 1:57pm UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/2 "2013-06-19T13:57:29Z")

</div>

What happens if you just use the serial monitor instead of the VB?

(please use code tags when posting code)

---

<div class="post-metadata">

**Author:** ![guix](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/guix/32/215371_2.png) [@guix](https://forum.arduino.cc/u/guix)\
**Post date:** [June 19, 2013, 3:29pm UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/3 "2013-06-19T15:29:22Z")

</div>

Hello, I think your problem is that:

> The DataReceived event is raised on a secondary thread when data is received from the SerialPort object. Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception. If it is necessary to modify elements in the main Form or Control, post change requests back using Invoke, which will do the work on the proper thread.

I had a similar problem few days ago, the solution (in C++/CLI) was to use something like this:

```arduino
//in the Form constructor:
this->serialPort1->DataReceived += gcnew SerialDataReceivedEventHandler(this, &Form1::DataReceivedHandler);

...

private: System::Void DataReceivedHandler(Object^ sender, SerialDataReceivedEventArgs^ e)
{
	Monitor::Enter(this);
		
	try
	{
		while (this->serialPort1->BytesToRead > 0)
		{
			if ( Math::Min(this->serialPort1->BytesToRead, this->serialPort1->ReadBufferSize) > 0)
			{
				String^ input = this->serialPort1->ReadExisting();
				this->textBox2->AppendText( input );
			}
		}
	}
	
	catch ( Exception^ e )
	{
		this->label1->Text = e->Message;
	}
	
	finally
	{
		Monitor::Exit(this);
	}
}

```

> **[SerialPort.DataReceived Event (System.IO.Ports)](https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.datareceived?view=dotnet-plat-ext-8.0&redirectedfrom=MSDN)**
>
> Indicates that data has been received through a port represented by the SerialPort object.

Not sure if that will help you...

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 20, 2013, 11:00am UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/4 "2013-06-20T11:00:34Z")

</div>

Suppose that you put an object in front of the sensor at a distance of 5 cm. You are then sending "5 cm\r\n" to the serial port. You are asking VB to convert that to an int. Is there any real reason to send data that VB needs to discard?

How is VB supposed to know when to stop reading from the serial port? If the Arduino sends "5 cm\r\n 6 cm\r\n" before the VB app gets around to reading it, what do you expect the VB app to show in the text box?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 7, 2013, 3:20am UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/5 "2013-07-07T03:20:21Z")

</div>

hi all,

there is no problem if using the serial monitor. i want the vb to display the distance same like appear in serial monitor. so what should i do to display that data? i have tried many code but it still not working

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 7, 2013, 1:44pm UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/6 "2013-07-07T13:44:13Z")

</div>

> i want the vb to display the distance same like appear in serial monitor.

Then, why are you forcing the text received from the serial port to be converted to an integer?  
Why are you expecting the SensorTextBox object's Text property setter to deal with an integer?

```arduino
        Dim Distance As Integer = SerialPort1.ReadExisting
        SensorTextBox.Text = Distance

```

The Serial Monitor application makes no such demands on the data. It treats all the input as text.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 8, 2013, 5:34am UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/7 "2013-07-08T05:34:20Z")

</div>

i am beginner in this thing.so i need to change to another data type.is it?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 8, 2013, 6:10am UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/8 "2013-07-08T06:10:20Z")

</div>

hi all,

im have a problem to display my ultrasonic sensor data from Arduino into visual basic. it works if using the serial monitor and i got my data in. but when im trying to display these data on visual basic, im unable to display the sensor data.  
this is my code for arduino

```cpp
int trigPin = 8;
int echoPin = 9;

void setup(void)
{ 
  Serial.begin(19200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop(void)
{
  if(Serial.available()){
    //Create new variable val
    char val = Serial.read();
    
    if(val != -1)
    {
      switch(val)
      {
      case 'c':
        ultrasonicSensor(); //Print the Sensor data
        break;
      }
  }
}

void ultrasonicSensor()
{
  int Duration, Distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  Duration = pulseIn(echoPin, HIGH);
  Distance = (Duration/2) / 29.1;
  Serial.println(Distance);
  delay(500); 
}

```

and this is the code for Visual Basic

```cpp
Imports System
Imports System.IO.Ports
Imports System.ComponentModel
Imports System.Threading

Public Class Form1
    Dim myPort As Array
    Dim Distance As Integer
    Delegate Sub SetTextCallBack(ByVal [text] As String)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myPort = IO.Ports.SerialPort.GetPortNames()
        PortComboBox.Items.AddRange(myPort)
        BaudComboBox.Items.Add(9600)
        BaudComboBox.Items.Add(19200)
        BaudComboBox.Items.Add(38400)
        BaudComboBox.Items.Add(57600)
        BaudComboBox.Items.Add(115200)
        ConnectButton.Enabled = True
        DisconnectButton.Enabled = False

    End Sub

    Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click
        SerialPort1.PortName = PortComboBox.Text
        SerialPort1.BaudRate = BaudComboBox.Text
        SerialPort1.Open()
        Timer1.Start()

        lblMessage.Text = PortComboBox.Text & " Connected."
        ConnectButton.Enabled = False
        DisconnectButton.Enabled = True
    End Sub

    Private Sub DisconnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectButton.Click
        SerialPort1.Close()
        lblMessage.Text = SerialPort1.PortName & " Disconnected."
        DisconnectButton.Enabled = False
        ConnectButton.Enabled = True
    End Sub

 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            SerialPort1.Write("c")
            System.Threading.Thread.Sleep(250)
            Dim distance As string = SerialPort1.ReadExisting()
            ListBoxSensor.Text = distance
        Catch ex As Exception

        End Try

    End Sub
End Class

```

is there any error on my code because im unable to display the data into visual basic. please help me.  
thanks.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 8, 2013, 9:44am UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/9 "2013-07-08T09:44:36Z")

</div>

Here's the timing for the first time you send C to the Arduino

VB: sends 'c' to the serial port  
VB: sleep for 250ms  
Arduino: reads serial. if 'c' call ultrasonicSensor  
Arduino: writes HI to trigger pin  
Arduino: sleeps for 250ms (of 1000ms)  
VB: reads serial port and adds result to listbox  
Arduino: completes remaining 750ms of 1000ms sleep  
Arduino: writes LO to trigger pin  
etc

did you try sending the c twice in a row? You may read something in the second time.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 8, 2013, 9:46am UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/10 "2013-07-08T09:46:49Z")

</div>

Ah it's a timer. What is the interval of the timer?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 8, 2013, 9:49am UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/11 "2013-07-08T09:49:23Z")

</div>

And look into asynchronous serial port methods too - synchronous is ugly.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 8, 2013, 9:51am UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/12 "2013-07-08T09:51:00Z")

</div>

> i am beginner in this thing.so i need to change to another data type.is it?

Yes. The same one that the Serial Monitor uses and that the Arduino outputs - String.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 8, 2013, 4:25pm UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/13 "2013-07-08T16:25:26Z")

</div>

> did you try sending the c twice in a row? You may read something in the second time.

do you mean like this

```arduino
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            SerialPort1.Write("c")
            SerialPort1.Write("c")
            System.Threading.Thread.Sleep(250)
            Dim i As UShort = SerialPort1.ReadExisting()
            ListBoxSensor.Text = "" + i.ToString()
            Me.Refresh()
        Catch ex As Exception

        End Try

```

i tried it but still no data displayed. yup, im using timer and the interval is 1000.  
im a beginner in vb programming. can you help me fix my code. i have no idea what to do now.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 9, 2013, 1:32am UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/14 "2013-07-09T01:32:59Z")

</div>

> The same one that the Serial Monitor uses and that the Arduino outputs - String.

i've changed the data type into string but still no data displayed in textbox. i tried with another way to get the sensor value. i send the "c" character to arduino and arduino print the sensor value.below is the code

```arduino
int trigPin = 8;
int echoPin = 9;

void setup(void)
{ 
  Serial.begin(19200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop(void)
{
  if(Serial.available()){
    
    char val = Serial.read();
    
    if(val != -1)
    {
      switch(val)
      {
      case 'c':
        ultrasonicSensor(); //Print the Sensor value
        break;
      }
    } 
  }
}

void ultrasonicSensor()
{
  int Duration, Distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  Duration = pulseIn(echoPin, HIGH);
  Distance = (Duration/2) / 29.1;
  Serial.println(Distance);
  delay(500); 
}

```

in visual basic, im using the timer to send the "c" character.below is the code

```arduino
Imports System
Imports System.IO.Ports
Imports System.ComponentModel
Imports System.Threading

Public Class Form1
    Dim myPort As Array
    Dim Distance As Integer
    Delegate Sub SetTextCallBack(ByVal [text] As String)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myPort = IO.Ports.SerialPort.GetPortNames()
        PortComboBox.Items.AddRange(myPort)
        BaudComboBox.Items.Add(9600)
        BaudComboBox.Items.Add(19200)
        BaudComboBox.Items.Add(38400)
        BaudComboBox.Items.Add(57600)
        BaudComboBox.Items.Add(115200)
        ConnectButton.Enabled = True
        DisconnectButton.Enabled = False

    End Sub

    Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click
        SerialPort1.PortName = PortComboBox.Text
        SerialPort1.BaudRate = BaudComboBox.Text
        SerialPort1.Open()
        lblMessage.Text = PortComboBox.Text & " Connected."
        ConnectButton.Enabled = False
        DisconnectButton.Enabled = True
    End Sub

    Private Sub DisconnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectButton.Click
        SerialPort1.Close()
        lblMessage.Text = SerialPort1.PortName & " Disconnected."
        DisconnectButton.Enabled = False
        ConnectButton.Enabled = True
    End Sub

 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            SerialPort1.Write("c")
            System.Threading.Thread.Sleep(250)
            Dim i As String = SerialPort1.ReadExisting()
            ListBoxSensor.Text = i
            Me.Refresh()
        Catch ex As Exception

        End Try

    End Sub
End Class

```

on timer, i set the interval to 1000. but still not working. whats wrong actually?

thanks for your reply.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 9, 2013, 10:22am UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/15 "2013-07-09T10:22:41Z")

</div>

```cpp
            System.Threading.Thread.Sleep(250)

```

What it the unit for 250? Is this sleeping for 6+ minutes or 1/4 of a second?

Instead of a timer, use a button with a click event. Step through the code using the debugger.

Instead of something complicated for the Arduino to do (read a ping sensor), make it do something simple:

```cpp
  if(Serial.available())
  {
    char val = Serial.read();
    Serial.print("Hey, Joe!");
  }

```

Instead of a one letter message to the Arduino, send a bunch of stuff:

```cpp
            SerialPort1.Write("Hey, what a beautiful day. Isn't it great to be alive?")

```

Then, pay attention to the RX LED on the Arduino. Is it actually receiving anything when you click the button in the VB app?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 9, 2013, 3:06pm UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/16 "2013-07-09T15:06:34Z")

</div>

> im a beginner in vb programming.

But, apparently not to crossposting.

> i have no idea what to do now.

Well, I have an idea.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 9, 2013, 4:06pm UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/17 "2013-07-09T16:06:34Z")

</div>

> What it the unit for 250?

the unit is ms.the vb app send c character and sleep for 250ms.

i;ve tried with your suggestion but still not working. if im using the button with click event, i need to use the serial port data\_received event right?  
the RX led on arduino is blinking when i click any button on vb app but the RX led doesn't blink. thats mean no data is sent from arduino to vb app isn't it?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 9, 2013, 4:56pm UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/18 "2013-07-09T16:56:15Z")

</div>

> Well, I have an idea.

you already told me on the other post. i tried it but still not working. do you have another idea? let just discuss here.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 9, 2013, 5:18pm UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/19 "2013-07-09T17:18:21Z")

</div>

> the RX led on arduino is blinking when i click any button on vb app but the RX led doesn't blink.

Does she? Or doesn't she? Only her hairdresser knows for sure.

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [July 9, 2013, 9:17pm UTC](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919/20 "2013-07-09T21:17:49Z")

</div>

Please **do not cross-post**. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

- Moderator

[Next page](https://forum.arduino.cc/t/receive-and-display-serial-data-in-visual-basic/168919.md?page=2)
