16,555
社区成员
发帖
与我相关
我的任务
分享
Public strData as String
'
Public Sub OnDataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
Try
Invoke(New EventHandler(AddressOf GetData))
Catch ex As Exception
End Try
End Sub
'
Public Sub GetData(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim nums As Integer = Port1.BytesToRead
If nums > 0 Then
For i = 1 To nums
strData &= Chr(Port1.ReadChar)
Next
'处理数据...
End If
End Sub
Public Sub GetData(ByVal sender As System.Object, ByVal e As System.EventArgs)
If Port1.BytesToRead > 0 Then
For i As Integer = 1 To 5 '如果数据较多,多循环几次(延时长点时间)
System.Threading.Thread.Sleep(10)
Application.DoEvents()
Next
Dim strData As String = Port1.ReadExisting '一个一个字符读取在,再拼起来,就容易出现反的
'处理数据
End If
End Sub
Public Sub GetData(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim nums As Integer = Port1.BytesToRead
If nums > 0 Then
For i As Integer = 1 To 5 '如果数据较多,多循环几次(延时长点时间)
System.Threading.Thread.Sleep(10)
Application.DoEvents()
Next
Dim strData As String = Port1.ReadExisting
'nums = Port1.BytesToRead
'For i = 1 To nums
' strData &= Chr(Port1.ReadChar)
' Next '用你这个方法读取也可以
'处理数据...
End If
End Sub