我正在用 Visual Basic 编写一个程序,它将从串行端口读取使用外部控制器(Arduino)发送的文本命令。但是,当我尝试测试代码时出现错误:
跨线程操作无效
代码如下所示:
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