vb.net SerialPort控件接收数据问题,用串口调试助手发数据,只有第一次可以正常接收
PrivateSubSerialPort1_DataReceived(ByValsenderAsSystem.Object,ByValeAsSystem.IO.Ports...
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'触发-串口收到数据
' TextBox1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
End Sub
Public Delegate Sub myDelegate()
Public Sub updateTextBox()
'显示更新串口收到的数据
With TextBox1
.AppendText(SerialPort1.ReadExisting) '& vbCrLf)
.ScrollToCaret()
End With
End Sub 展开
'触发-串口收到数据
' TextBox1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
End Sub
Public Delegate Sub myDelegate()
Public Sub updateTextBox()
'显示更新串口收到的数据
With TextBox1
.AppendText(SerialPort1.ReadExisting) '& vbCrLf)
.ScrollToCaret()
End With
End Sub 展开
展开全部
你定义的委托需要带参数才行,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
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询