Onamission71
Member
- Joined
- Jul 8, 2009
- Messages
- 14
- Programming Experience
- Beginner
Hi I'm trying to do a basic function, I'm using Visual Basic 2008 express, I have a windows form with 1 textbox, 1 button and a serial port. When the button is pressed serialport1 is opened then the data received is trimmed to remove unwanted char from start and end and then is displayed in the textbox, when the button is pressed again the port is closed, this works fine but in debug mode I get a cross thread error, telling me I can't use textbox1.text=result
Here is the code:-
Public Class Form1
Dim Buff As String
Dim returnvalue As String
Dim value2 As String
Dim result As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text Is "Read" Then
SerialPort1.Open()
Button1.Text = "Stop"
ElseIf Button1.Text = "Stop" Then
SerialPort1.Close()
Button1.Text = "Read"
End If
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Buff = (SerialPort1.ReadExisting())
returnvalue = Buff.TrimStart("?")
result = returnvalue.TrimEnd("")
TextBox1.Text = (result)
End Sub
End Class
How can I mod this code to get rid of the error?? It's been suggested to me to use BeginInvoke or Invoke but i'm not sure how to use this can someone help please...
Here is the code:-
Public Class Form1
Dim Buff As String
Dim returnvalue As String
Dim value2 As String
Dim result As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text Is "Read" Then
SerialPort1.Open()
Button1.Text = "Stop"
ElseIf Button1.Text = "Stop" Then
SerialPort1.Close()
Button1.Text = "Read"
End If
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Buff = (SerialPort1.ReadExisting())
returnvalue = Buff.TrimStart("?")
result = returnvalue.TrimEnd("")
TextBox1.Text = (result)
End Sub
End Class
How can I mod this code to get rid of the error?? It's been suggested to me to use BeginInvoke or Invoke but i'm not sure how to use this can someone help please...