16,555
社区成员
发帖
与我相关
我的任务
分享
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitSerialPort()
End Sub
Dim strIncoming As String
Dim RS232 As SerialPort
Private Sub InitSerialPort()
RS232 = New SerialPort("COM10", 9600, Parity.None, 8, 1)
If (Not RS232.IsOpen) Then
RS232.Open()
End If
Dim td As Thread = New Thread(AddressOf serialPort1_DataReceived)
td.Start()
End Sub
Private Sub serialPort1_DataReceived()
While True
If RS232.BytesToRead > 0 Then
strIncoming = RS232.ReadExisting.ToString
RS232.DiscardInBuffer()
Invoke(New EventHandler(AddressOf ForDisplay)) '调用接收数据函数
End If
End While
End Sub
Private Sub ForDisplay()
ListBox1.Items.Add(strIncoming) '取到回车符位置数-1
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
If ListBox1.Items.Count > 1000 Then
Me.ListBox1.Items.Clear()
End If
End Sub