VB。net 串口接收数据,用串口调试助手发数据,只有第一次可以正常接收

2024-12-18 20:14:59
推荐回答(1个)
回答1:

你定义的委托需要带参数才行,DataReceived事件中没有接收数据代码,下面的代码经测试可用
Delegate Sub SetTextCallback(ByVal InputString As String)
Private Sub ShowString(ByVal comData As String)
txt_Rect.Text += comData '将收到的数据入接收文字框中
txt_Rect.SelectionStart = txt_Rect.Text.Length
txt_Rect.ScrollToCaret()
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim inData As String = SerialPort1.ReadExisting
Dim d As New SetTextCallback(AddressOf ShowString)
BeginInvoke(d, inData)
End Sub