Go Down

Topic: Interface VB with Arduino : Problem in serial communication (Read 37 times) previous topic - next topic

HamzaLAMFAOUAR

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 :


Code: [Select]
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



Robin2

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

PaulS

Code: [Select]
        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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy