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
大雅新科技有限公司
2024-11-19 广告
2024-11-19 广告
这方面更多更全面的信息其实可以找下大雅新。深圳市大雅新科技有限公司从事KVM延长器,DVI延长器,USB延长器,键盘鼠标延长器,双绞线视频传输器,VGA视频双绞线传输器,VGA延长器,VGA视频延长器,DVI KVM 切换器等,优质供应商,...
点击进入详情页
本回答由大雅新科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询