Does the SerialPort DataReceived event repeat itself?
Suppose I am reading data from SerialPort whenever there is 100 bytes available or nothing has been done. This means that the rest of the data will still be available in the SerialPort buffer. This read is done inside the DataReceived event handler.
Now suppose a situation arises where there is, say, 50 bytes in the SerilaPort buffer and no more data arrives. From what I understand, the DataReceived event will fire whenever there is a certain number of bytes available to read from the buffer.
Now, in this scenario, if I never read those 50 bytes, would the event fire continuously due to these unread bytes?
a source to share
I posted an answer (see comments above). It's in the documentation. "... when data is received from the SerialPort object". The OP said, "If I never read these 50 bytes, will the event fire continuously due to the presence of these unread bytes?" and you answered, "Yes, it will keep firing until you call" Reading "()".
The event is fired only when new data is received. If you do not process this piece of data, this piece of data will NOT raise a new event. However, if new data comes in, a new event will happen and you can handle the whole thing.
a source to share
Yes, it will keep firing as additional bytes come in until you call Read (). You can use the ReceivedBytesThreshold property to delay this. This is generally a bad idea, the loss of bytes due to an overflow error can result in the connection being completely hijacked. The buffer that you read () in the event handler yourself.
Also be aware that this is the only known behavior of the Microsoft serial port driver. The SerialPort class uses the WaitCommEvent API, and may require a driver to implement this. Especially many USB drivers that emulate a serial port to simplify the interface to a configurable device were not created equal.
a source to share
I think this proves my point, sorry for the VB code.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
'set up the com port for a test
SerialPort1.PortName = "COM5" 'attached to breakout box with loopback
SerialPort1.BaudRate = 115200 'some speed
SerialPort1.Encoding = System.Text.Encoding.GetEncoding("windows-1252")
Dim b() As Byte = New Byte() {42, 16, 20, 254, 255, 128} 'test data
ctrcv = 0 'counter
SerialPort1.Open() 'open the port
Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.ffff")) 'show time
SerialPort1.Write(b, 0, b.Length) 'write the test data
'give the DataReceived event handler chances to fire
Threading.Thread.Sleep(30000)
'show the last time it fired and how many times
Debug.WriteLine(lastRCV.ToString("HH:mm:ss.ffff") & " " & ctrcv)
'show how many are available to read
Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.ffff") & " " & SerialPort1.BytesToRead)
Array.Clear(b, 0, b.Length)
SerialPort1.Read(b, 0, SerialPort1.BytesToRead) 'read them
SerialPort1.Close() 'close the port
Stop
End Sub
Dim ctrcv As Integer = 0, lastRCV As DateTime
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles SerialPort1.DataReceived
ctrcv += 1
lastRCV = DateTime.Now
End Sub
Debug output
09:34:11.3241 <- when the test started
09:34:11.3642 3 <- the last data received event!, and how many events
09:34:41.3718 6 <- when the test ended
a source to share