How to read serial data from Arduino into Visual Basic 2010 textbox

Hello,

Thanks in advance to anyone who chimes in. I'm sure this is a simple fix, but I'm still fairly new at this and, so far, it's eluded me.

At this point I can display incoming data in a msgbox but I would like to be able to display it in a label or textbox that updates with new data as it is read. If someone could provide me with a clue or two I'd appreciate it.

Again, thanks to anyone willing to assist,
-Zixxer

Imports System.IO
Imports System.IO.Ports
Imports System.Threading


Public Class Form1


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Encoding = System.Text.Encoding.Default
    End Sub

    Dim runbtnClicked As Boolean = False
    Dim stopbtnClicked As Boolean = False


    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        Dim NewTemp As String = SerialPort1.ReadExisting()

        ''This does Not work and pulls a cross-threading exception 
        ''Cross-thread operation not valid: Control 'txtBx1' accessed from a thread other than the thread it was created on.
        txtBx1.Text = NewTemp

        ''This Works and I can see the incoming data''
        While runbtnClicked = True

            MsgBox(NewTemp)

        End While

    End Sub



    Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click

        MsgBox("Running")

        Try
            SerialPort1.Open()

        Catch ex As Exception

            MessageBox.Show(ex.ToString)

        End Try
        runbtnClicked = True

    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        runbtnClicked = False
        stopbtnClicked = True
        SerialPort1.Close()


    End Sub
End Class

Use and Arduino Leonardo, Arduino Micro, or Arduino Lillypad USB to emulate a USB keyboard. These board use the ATmega32u4 processor which has native USB support. Plug it into the PC and any keystrokes you send will go into the window control that has the current input focus.

I apologize for not including the following information:

Using an Arduino Uno Rev 3
Reading data from a LM75A Temp sensor via I2C

I am just asking how to gather data in VB in order to manipulate/store/display.

Thanks Again!

zixxer14:
I am just asking how to gather data in VB in order to manipulate/store/display.

To the PC the Arduino looks like a serial port. Read it like any serial port.

To the PC the Arduino looks like a serial port. Read it like any serial port.

But VB and other Visual Studio languages read the serial port on one thread, and manage the GUI on another.

OP: I could tell you how to resolve the issue in C#, but not in VB. I'm sure that there is some way to communicate between threads. You'll need to hit the documentation to find out how for VB.

In C#:

		private void SetText(string text)
		{
			// InvokeRequired required compares the thread ID of the
			// calling thread to the thread ID of the creating thread.
			// If these threads are different, it returns true.
			if (this.receiveText.InvokeRequired)
			{
				SetTextCallback d = new SetTextCallback(SetText);
				this.Invoke(d, new object[] { text });
			}
			else
			{
				this.receiveText.Text += "Text: ";
				this.receiveText.Text += text;
				this.receiveText.Text += Environment.NewLine;
			}
		}

This method is called by the thread that reads the serial data.