.NET的SerialPort的Readline VS DataReceived事件检索处理程序 [英] .Net SerialPort Readline Vs DataReceived Event Handler
问题描述
在VB.NET,什么是使用SerialPort.ReadLine()方法与使用DataReceived事件检索处理程序之间的区别?目前,我使用的是接收数据的事件处理程序和检测线的结局。问题是数据即将在块而不是1行的句子。如果我使用SerialPort.ReadLine()方法,该数据来自于1号线的句子。使用这种方法,但是,有新行可变设定结束字符端口的线路。是ReadLine方法只是处理缓冲区给我吗?数据是否仍然会使用该方法的块,无论?
In VB.NET, what is the difference between using the SerialPort.ReadLine() method versus using the DataReceived event handler? Currently, I'm using the data received event handler and detecting the line endings. The problem is the data is coming in chunks and not 1 line sentences. If I use SerialPort.ReadLine() method, the data comes in 1 line sentences. Using this method, however, has the NewLine variable to set the line ending character for the port. Is the readline method just handling the buffer for me? Does the data still come in chunks regardless of the method used?
方法1:
While _continue
Try
Dim message As String = _serialPort.ReadLine()
Console.WriteLine(message)
Catch generatedExceptionName As TimeoutException
End Try
End While
方法2:
Public Sub StartListener()
Try
_serialport = New SerialPort()
With _serialport
.PortName = "COM3"
.BaudRate = 38400
.DataBits = 8
.Parity = Parity.None
.StopBits = StopBits.One
.Handshake = Handshake.None
AddHandler .DataReceived, AddressOf DataReceivedHandler
End With
_serialport.Open()
Catch ex As Exception
End Try
End Sub
Private Shared buffer As String = ""
Private Sub DataReceivedHandler(sender As Object, e As SerialDataReceivedEventArgs)
Try
Dim rcv As String = _serialport.ReadExisting()
buffer = String.Concat(buffer, rcv)
Dim x As Integer
Do
x = buffer.IndexOf(vbCrLf)
If x > -1 Then
Console.WriteLine(buffer.Substring(0, x).Trim())
buffer = buffer.Remove(0, x + 2)
End If
Loop Until x = -1
Catch ex as Exception
End Try
End Sub
我目前使用方法2,但考虑切换到方法1,因为它似乎更安全,看起来prettier你知道的,但有什么意义呢?谢谢
I am currently using Method 2, but was thinking about switching to Method 1 because it seems safer and looks prettier you know, but what's the point? Thanks
推荐答案
在使用.NET的SerialPort实施工作,你应该的的从不的尝试从串口读取使用DataReceived事件检索和任何其它方法。 ReadExisting
和的ReadLine
都使用相同的底层的MemoryStream。你会遇到你在哪里readline的数据拉断的MemoryStream当事件中断导致您尝试读取已被从数据流中删除数据的情况。
When working with the .NET SerialPort implementation, you should NEVER try to read from the serial port using the DataReceived event and any other method. ReadExisting
and ReadLine
both use the same underlying MemoryStream. You will run into situations where you are pulling data off the MemoryStream from the ReadLine when the event interrupts causing you to attempt to read data that has already been removed from the stream.
使用一种方法或其他。难道没有同时使用。
Use one method or the other. Do no use both.
汉斯帕桑特是正确的关于的ReadLine
阻塞你的UI线程。你身边有两个方法:使用 DataReceived
事件;或者将您的code处理该的SerialPort
在一个单独的线程。
Hans Passant is correct about the ReadLine
blocking your UI thread. You have two methods around that: use the DataReceived
event; or place your code that handles the SerialPort
on a separate thread.
使用 DataReceived
事件通常是preferable作为.NET会自动运行的工作线程。你失去的ReadLine
虽然的免费赠品。而且你必须手动缓冲输入,如果你需要执行预读(给你喜欢的东西readline的功能)。
Using the DataReceived
event is usually preferable as .NET will run that on a worker thread automatically. You lose the freebie of the ReadLine
though. And you have to manually buffer the input if you need to perform read aheads (to give you something like the ReadLine functionality).
这篇关于.NET的SerialPort的Readline VS DataReceived事件检索处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!