SerialConnection.DataReceived firing without data

Hi,
I upgraded recently updated my program to use “SerialConnection.DataReceived” to replace “Serial.DataAvailable” that was marked as deprecated.

Every thing seem ok (my app is still communicating) but i noticed that “SerialConnection.DataReceived” event is always triggering without data after receiving its first byte. This wasn’t the case with old “Serial.DataAvailable” class, I was assuming that behavior would be the same.

I put a breakpoint at first instruction (after receiving initial data)

Count = mySerialConn.BytesAvailable → Return 0
RawInData = mySerialConn.LookAhead → Return “” (0 bytes)

While MySerial.BytesAvailable > 0
…processing incoming data…
Wend

if someone has coded this (above) in the “SerialConnection.DataReceived” that check number of bytes before processing they may not notice that this event is firing all the time after receiving first byte, but it wouldn’t process data as there is no data ( so not seeing that this is happening ).

Have someone seen this issue/behavior before ?

ref: using Xojo 2021r3 on Windows

1 Like

This could happen of you use .poll in a loop or timer…

I don’t use the .poll method nor a timer in this case.

My understanding is that “SerialConnection.DataReceived” should only trigger when receiving data from serial port, but byte count is zero. Is there a other reason or configuration that could trigger that event with a zero byte count ?

I just experienced this issue, and have narrowed it down to this:
Draining the data from the SerialConnection using the equivalent of SerialConnection.Read(SerialConnection.BytesAvailable) exhibits the behavior you described. Draining the data from the SerialConnection using SerialConnection.ReadAll does not exhibit the behavior (i.e. works as expected).

For the time being I’m working around it with something similar to:

if Connection.BytesAvailable >= SerialReceive_ExpectedLength then
  var serialData as Variant
  var excessDataAvailable as Boolean = (Connection.BytesAvailable > SerialReceive_ExpectedLength)
  if excessDataAvailable then
    serialData = Connection.Read(SerialReceive_ExpectedLength)
  else
    serialData = Connection.ReadAll()
  end
  Timer.CallLater(1, SerialReceive_MessageParser, serialData) 
  
end

As you said, my program essentially worked without my noticing the issue… until I ran it in the profiler and saw the number of calls into the DataReceived event was off the charts.

Xojo 2021r3.1 on MacOS 12.2.1 w/ Prolific USB->Serial adapter.

1 Like

Has this been logged as a bug - It is causing me problems too

I don’t think I have reported this bug but should be reported.
For now i use this in DataReceived Handler:

Count = me.BytesAvailable
If Count = 0 Then
Return
End

’ Read Available Data and append the data to a Array
Do
RawInData = me.Read(Len(me.LookAhead))
’ To Store Incoming Data in Array
Loop Until Len(LookAhead) = 0