vb.net 使用多个serialport串口 控件,互相影响得问题
高手好:做一个小软件,使用电脑的3个USB分别联接3台串口设备。现在出现了一个很[color=#FF0000]诡异[/color]得现象!!!当我拔掉串口2的设备,串口1...
高手好:
做一个小软件,使用电脑的3个USB分别联接3台串口设备。现在出现了一个很[color=#FF0000]诡异[/color]得现象!!!
当我拔掉串口2的设备,串口1端口无法接收设备的返回数据,
也就是 SerialPort1.DataReceived 事件没有反应了。
但是发送数据,串口设备能接收到。
我重复关闭和打开串口尝试重新联接设备,接收数据事件也还是没响应。
唯一只能关闭软件重新运行软件 才能恢复接收数据。
串口设备1的接收事件
Private Sub SerialPort1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim ReciveLEN As Integer
Try
ReciveLEN = SerialPort1.BytesToRead
ReDim Preserve ReciveBYTE(ReceiveSUM + ReciveLEN)
SerialPort1.Read(ReciveBYTE, ReceiveSUM, ReciveLEN) '读取缓冲数据
ReceiveSUM += ReciveLEN
If UBound(ReciveBYTE) > 3 Then
If ReciveBYTE(ReceiveSUM - 1) = 10 And ReciveBYTE(ReceiveSUM - 2) = 13 Then
Me.BeginInvoke(New Meterdelegate(AddressOf MeterreceivedDelegate), New Object() {})
End If
End If
Catch ex As Exception
ThreadErr = ("仪表接收线程执行错误1" & ex.ToString)
End Try
End Sub
串口设备2的接收事件
'电源通信接收事件
Private Sub SerialPort2_DataReceived(sender As Object, e As Ports.SerialDataReceivedEventArgs) Handles SerialPort2.DataReceived
Dim ReciveLEN As Integer
Try
ReciveLEN = SerialPort2.BytesToRead
ReDim Preserve PowerReciveBYTE(PowerReceiveSUM + ReciveLEN)
SerialPort2.Read(PowerReciveBYTE, PowerReceiveSUM, ReciveLEN) '读取缓冲数据
PowerReceiveSUM += ReciveLEN
If PowerReciveBYTE(PowerReceiveSUM - 1) = 10 Then
Me.BeginInvoke(New Powerdelegate(AddressOf PowerreceivedDelegate), New Object() {})
End If
Catch ex As Exception
ThreadErr = ("电源接收线程执行错误1" & ex.ToString)
End Try
End Sub 展开
做一个小软件,使用电脑的3个USB分别联接3台串口设备。现在出现了一个很[color=#FF0000]诡异[/color]得现象!!!
当我拔掉串口2的设备,串口1端口无法接收设备的返回数据,
也就是 SerialPort1.DataReceived 事件没有反应了。
但是发送数据,串口设备能接收到。
我重复关闭和打开串口尝试重新联接设备,接收数据事件也还是没响应。
唯一只能关闭软件重新运行软件 才能恢复接收数据。
串口设备1的接收事件
Private Sub SerialPort1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim ReciveLEN As Integer
Try
ReciveLEN = SerialPort1.BytesToRead
ReDim Preserve ReciveBYTE(ReceiveSUM + ReciveLEN)
SerialPort1.Read(ReciveBYTE, ReceiveSUM, ReciveLEN) '读取缓冲数据
ReceiveSUM += ReciveLEN
If UBound(ReciveBYTE) > 3 Then
If ReciveBYTE(ReceiveSUM - 1) = 10 And ReciveBYTE(ReceiveSUM - 2) = 13 Then
Me.BeginInvoke(New Meterdelegate(AddressOf MeterreceivedDelegate), New Object() {})
End If
End If
Catch ex As Exception
ThreadErr = ("仪表接收线程执行错误1" & ex.ToString)
End Try
End Sub
串口设备2的接收事件
'电源通信接收事件
Private Sub SerialPort2_DataReceived(sender As Object, e As Ports.SerialDataReceivedEventArgs) Handles SerialPort2.DataReceived
Dim ReciveLEN As Integer
Try
ReciveLEN = SerialPort2.BytesToRead
ReDim Preserve PowerReciveBYTE(PowerReceiveSUM + ReciveLEN)
SerialPort2.Read(PowerReciveBYTE, PowerReceiveSUM, ReciveLEN) '读取缓冲数据
PowerReceiveSUM += ReciveLEN
If PowerReciveBYTE(PowerReceiveSUM - 1) = 10 Then
Me.BeginInvoke(New Powerdelegate(AddressOf PowerreceivedDelegate), New Object() {})
End If
Catch ex As Exception
ThreadErr = ("电源接收线程执行错误1" & ex.ToString)
End Try
End Sub 展开
2个回答
2018-08-26 · 知道合伙人互联网行家
关注
展开全部
你定义的委托需要带参数才行,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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询