我需要读取串口来解析传入的字符串。如果我使用PuTTy
我可以在串口中读取这些字符串,但如果我将VB应用程序与此代码一起使用,则它不起作用:
Private Sub comPortList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comPortList.SelectedIndexChanged
Dim com1 As IO.Ports.SerialPort = Nothing
Try
com1 = My.Computer.Ports.OpenSerialPort(comPortList.SelectedItem, 9600)
com1.ReadTimeout = 10000
Do
Dim Incoming As String = com1.ReadLine()
If Incoming IsNot Nothing Then
MsgBox(Incoming & vbCrLf)
End If
Loop
Catch ex As TimeoutException
MsgBox("Error: 'Serial Port read timed out.'") ' always getting this error
Finally
If com1 IsNot Nothing Then com1.Close()
End Try
End Sub
我总是得到读取超时错误。此代码基于MSDN文档。
提前致谢。