本文介绍了SerialDataReceivedEventArgs无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里有一个来自google的代码,它将显示文本消息到Richtextbox.但是,没有收到消息.
I have here a code from google that will show a text message to a richtextbox. But, there is no message received.
这是代码:
Public Class Form1
Dim spDrLine As String = ""
Dim spBuffer As String = ""
Dim receivedData As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call spOpen()
End Sub
Private Sub spOpen()
Try
spClose()
spObj.PortName = "COM6"
spObj.BaudRate = 9600
spObj.Parity = IO.Ports.Parity.None
spObj.DataBits = 8
spObj.StopBits = IO.Ports.StopBits.One
spObj.Handshake = IO.Ports.Handshake.None
spObj.DtrEnable = True 'imp
spObj.RtsEnable = True 'imp
spObj.NewLine = vbCr
spObj.ReadTimeout = 250
spObj.WriteTimeout = 250
spObj.Open()
Catch ex As Exception
'handle the way you want
End Try
End Sub
Private Sub spClose()
Try
If spObj.IsOpen Then
spObj.Close()
spObj.Dispose()
End If
Catch ex As Exception
End Try
End Sub
Private Sub spObj_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles spObj.DataReceived
Try
If spObj.IsOpen Then
spDrLine = spDrLine & spObj.ReadExisting() 'imp
If InStr(1, spDrLine, vbCr) > 0 Or InStr(1, spDrLine, vbLf) > 0 Then
spBuffer = spDrLine
spDrLine = ""
Me.Invoke(New EventHandler(AddressOf doProcess)) 'imp
Else
Exit Sub
End If
End If
Catch ex As Exception
End Try
End Sub
Function ReceiveSerialData() As String
Try
spDrLine = spDrLine & spObj.ReadExisting()
If InStr(1, spDrLine, vbCr) > 0 Or InStr(1, spDrLine, vbLf) > 0 Then
spBuffer = spDrLine
spDrLine = ""
Me.Invoke(New EventHandler(AddressOf doProcess)) 'imp
Else
Return spDrLine
End If
If spBuffer Is Nothing Then
Return "nothing" & vbCrLf
Else
Return spBuffer
End If
Catch ex As TimeoutException
Return "Error: Serial Port read timed out."
End Try
End Function
Private Sub doProcess()
'process spBuffer
RichTextBox1.Text &= spBuffer
End Sub
End Class
推荐答案
此论坛讨论的是Visual Studio WPF/SL设计器,Visual Studio指导自动化工具包,开发人员文档和帮助系统以及Visual Studio编辑器.
This forum is discussing Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, and Visual Studio Editor.
您的问题与VB开发有关,我将此线程移至相应的论坛以寻求专业解答.
Your issue is related to VB development I will move this thread to corresponding forum for a professional answer.
此致,
奥斯卡
这篇关于SerialDataReceivedEventArgs无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!