0

I am writing a program in Visual Basic that will read text commands from the serial port that are sent using an external controller (an Arduino). However, when I try to test the code I get an error:

Cross-thread Operation Not Valid

Here is what the code looks like:

 Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    Dim Data As String = SerialPort1.ReadExisting()
    If Data = "l" Then
        LeftRadio.Checked = True
    ElseIf Data = "r" Then
        RightRadio.Checked = True
    ElseIf Data = "c" Then
        CenterRadio.Checked = True
    End If
End Sub

Private Sub connect_Click(sender As Object, e As EventArgs) Handles connect.Click
    If Not SerialPort1.IsOpen Then
        SerialPort1.PortName = "COM3"
        SerialPort1.Open()
    End If
End Sub
abatishchev
  • 98,240
  • 88
  • 296
  • 433
user1683391
  • 79
  • 1
  • 1
  • 12
  • possible duplicate of [Cross-thread operation not valid](http://stackoverflow.com/questions/5037470/cross-thread-operation-not-valid) – abatishchev Dec 09 '12 at 20:55
  • The DataReceived event runs on a worker thread, you cannot update any controls directly. Use the *hundreds* of previous questions about this exception to find the solution. – Hans Passant Dec 09 '12 at 20:57

2 Answers2

0

Please see Cross-thread operation not valid and Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on

Short answer: you perform UI operation in not-UI (main) thread what is not permitted.

In VB.NET should be something like:

Dim check = Sub()
                LeftRadio.Checked = True
            End Sub
LeftRadio.Invoke(check)
Community
  • 1
  • 1
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • I read that but still do not fully understand how to implement into my code as that isnt visual basic. I am new to this , do you have insight on how I would go about doing it ? – user1683391 Dec 09 '12 at 21:07
  • @user1683391: See http://msdn.microsoft.com/en-us/library/bb531253.aspx and http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx – abatishchev Dec 09 '12 at 21:23
0

I have found that the DataReceived event doesn't usually work the way you think and can block the serial port while code runs in the event.

My preference is to add a Timer to the form that runs at a reasonable speed such as 5 times per second.

In the Timer OnTick event you can test SerialPort1.BytesAvailable to see if data has arrived. Then use ReadExisting() as you have above.

The timer code will be less prone to cross thread issues.

Visual Micro
  • 1,561
  • 13
  • 26