Interface VB with Arduino : Problem in serial communication

Hey everybody, hope u’re doing well!
I’ve got recently a weird problem in my interface using VB .NET when running my program!
“Invalid cross-thread operation: Control 'TextBox1 has been accessed from a thread other than the one it was created.”

My arduino program is a basic one of treating the keys pressed with the keypad and printing these keys with the serial communication, and when debugging, I found problem in the last instruction (Textbox1.Text = Chaine)!
Thank you very much !
This my VB code :

Imports System.IO
Imports System.IO.Ports

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitializeComponent()
        SerialPort1.Open()
        'SerialPort1.PortName = "COM21"
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default
    End Sub


    Private Sub DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim bytes As Integer = 6
        Dim Chaine As String = ""
        Dim comBuffer As Byte() = New Byte(bytes - 1) {}
        Chaine = SerialPort1.ReadChar()
        Dim Tableau(6) As Integer
        For i As Integer = 1 To (bytes - 1)
            Chaine = Chaine + comBuffer(i)
        Next
        TextBox1.Text = Chaine
    End Sub
End Class

HamzaLAMFAOUAR: I've got recently a weird problem in my interface using VB .NET when running my program! "Invalid cross-thread operation: Control 'TextBox1 has been accessed from a thread other than the one it was created."

That sounds like a VB problem and this is an Arduino Forum.

...R

        Dim comBuffer As Byte() = New Byte(bytes - 1) {}
        Chaine = SerialPort1.ReadChar()
        Dim Tableau(6) As Integer
        For i As Integer = 1 To (bytes - 1)
            Chaine = Chaine + comBuffer(i)
        Next

It occurs to me that you might actually want to put some data IN comBuffer BEFORE you append what is there so Chaine.

Hi all, this seems to me to indeed an important Arduino problem, streaming data into a GUI. I to have been struggling with capturing and parsing streaming data from Arduino into VB. Using Visual Studio 2013 - my Arduino is sending infrared temp, air temp, humidity, and ambient light level (photo-resistor). Arduino is sending data as follows with a 2000 us (2 sec) delay so there should be no issue with processing data before capturing next string...

IRtemp, AirTemp, Humidity, Light IRtemp, AirTemp, Humidity, Light IRtemp, AirTemp, Humidity, Light IRtemp, AirTemp, Humidity, Light

The following code was found on the web with slight modification by me. Data stream into the multi-line textbox as they should. The second textbox simply shows each segment of the data stream. I have, however, not found a good solution to parsing the data so it can be stored or graphed.

Public Class Form1 Dim WithEvents SerialPort As New IO.Ports.SerialPort

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try SerialPort.BaudRate = 9600 SerialPort.PortName = "COM5" '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

Delegate Sub myMethodDelegate(ByVal

Found the Perfect solution on YouTube for streaming Arduino data to VB. Have Fun!

Check out https://www.youtube.com/watch?v=ngnJ-81gVFo

It works GREAT! For reference here is my code too, which also works great for reading two temperatures, Humidity, and Light level from sensors on an Arduino.

Pertinent part of the Arduino script:

Serial.print("SkyT="); Serial.println(celcius); Serial.print("AirT="); Serial.println(temperature); Serial.print("Hum="); Serial.println(humidity); Serial.print("Lum="); Serial.println(analogRead(lightPin));

VB code to read and parse: Public Class Form1 Private WithEvents readSerial As New System.IO.Ports.SerialPort("COM5") Private SkyT As Single Private AirT As Single Private Hum As Single Private Lum As Integer

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load readSerial.BaudRate = 9600 readSerial.Open()

End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles tbSkyT.TextChanged

End Sub

Private Sub readSerial_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles readSerial.DataReceived Dim s As String = readSerial.ReadLine If s.Contains("SkyT") Then s = s.Replace("SkyT=", "") If IsNumeric(s) Then SkyT = CSng(s) tbSkyT.Invoke(New UpdateSkyTDelegate(AddressOf UpdateSkyT), SkyT.ToString + " C") End If ElseIf s.Contains("AirT") Then s = s.Replace("AirT=", "") If IsNumeric(s) Then AirT = CSng(s) tbAirT.Invoke(New UpdateAirTDelegate(AddressOf UpdateAirT), AirT.ToString + " C") End If ElseIf s.Contains("Hum") Then s = s.Replace("Hum=", "") If IsNumeric(s) Then Hum = CSng(s) tbHum.Invoke(New UpdateHumDelegate(AddressOf UpdateHum), Hum.ToString + " %") End If ElseIf s.Contains("Lum") Then s = s.Replace("Lum=", "") If IsNumeric(s) Then Lum = CInt(s) tbLum.Invoke(New UpdateLumDelegate(AddressOf UpdateLum), Lum.ToString) End If Else MsgBox("Read Error") End If End Sub Delegate Sub UpdateSkyTDelegate(ByVal t As String) Delegate Sub UpdateAirTDelegate(ByVal t As String) Delegate Sub UpdateHumDelegate(ByVal t As String) Delegate Sub UpdateLumDelegate(ByVal t As String)

Public Sub UpdateSkyT(ByVal t As String) tbSkyT.Text = t End Sub

Public Sub UpdateAirT(ByVal t As String) tbAirT.Text = t End Sub

Public Sub UpdateHum(ByVal t As String) tbHum.Text = t End Sub

Public Sub UpdateLum(ByVal t As String) tbLum.Text = t End Sub End Class