.Net SerialPort Readline Vs DataReceived Event Handler

In VB.NET, what is the difference between using the SerialPort.ReadLine () method and using a DataReceived event handler? I am currently using a processed data handler and detect line endings. The problem is that the data comes in chunks, not in 1 line of sentences. If I use the SerialPort.ReadLine () method, the data comes in 1 line of sentences. However, using this method has a NewLine variable to set the line termination character for the port. Does the readline method just process the buffer for me? Are data stored in chunks regardless of the method used?

Method 1:

While _continue Try Dim message As String = _serialPort.ReadLine() Console.WriteLine(message) Catch generatedExceptionName As TimeoutException End Try End While 

Method 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 

I'm currently using method 2, but was thinking of switching to method 1 because it seems more secure and looks prettier than you know, but what's the point? Thanks

+1
source share
2 answers

When working with the .NET SerialPort implementation, you should NEVER attempt to read from the serial port using the DataReceived event or any other method. ReadExisting and ReadLine share the same basic MemoryStream module. You will encounter situations when you take data from a MemoryStream from ReadLine, when event interrupts force you to try to read data that has already been removed from the stream.

Use one or more methods. Do not use both.

Hans Passant is right about ReadLine blocking your UI thread. You have two methods: use the DataReceived event; or put your code that processes SerialPort in a separate thread.

Using the DataReceived event is usually preferable, since .NET will automatically run this on the workflow. However, you lose the ReadLine freebie. And you need to manually buffer input if you need to do a preview (to give you something like the ReadLine function).

+3
source

Your call to ReadLine () is synchronous and blocks your code, potentially hanging on your program. DataReceived is asynchronous and does not work. This makes it difficult to use; there is no free lunch. But it may be important, the serial ports can be slow enough so that your entire program does not respond to user input. This is usually not a problem in the console mode program or when using a separate thread.

You can also call ReadLine () on the DataReceived event handler, which is in the desert. This can lead to a deadlock, you must use the ReadTimeout property.

But since you are using the console, there is probably no reason to use DataReceived.

+2
source

Source: https://habr.com/ru/post/972274/


All Articles