Multiple Simulataneous Serial DataAvailable instances?

Hello all.

Trying to track down a problem and found that there are 3 separate Serial.DataAvailable events runnnig with the cursor in different parts of the code. Why does this happen? I had no idea that >1 DataAvailable Events could be executing at the same time. I found it while stepping through some code and wondering why the IDE take the cursor to the middle of the code in the DataAvailable event.

Questions
What causes this?
How to prevent it. I want only 1 instance.
Can the DataAvailable event fire while executing code within the event?

Perplexed and confused…
Thanks,
Tim

You can move away from DataAvailable and use polling (via timer) as was suggested on another thread. I find that more robust and deterministic. The DataAvailable event is just that, and event. So every time the event happens (i.e. every time the serial device driver tells Xojo that new data was received) it will fire. You could perhaps create a flag to ignore the event when you don’t want to handle it, and handle it when you want to. But for those type of things (which in my mind are pretty deterministic) I prefer to poll it when I am ready, rather than it polling me every time the OS decides is time. Either way (DataAvailable or polling via Timer) you want to be in and out as quickly as possible and do the data processing outside of the event or the timer.

Hi Tim,

I really have no idea what you are explaining.
can you show some code, as asked previously, or some screen shots.
if you have one instance of the serial class then it will only have a single DataAvailble event.

to answer the questions,:-
what you describe as more than one instance is something I have never experienced in many years of using the serial class.
there should be nothing to prevent, there is only one DataAvailable event in the project when you have a single instance of the class.
I don’t know if the event can fire while in the event, but I suspect that the event occurs but you do not know about it until you exit the event.

without seeing your code its really hard to surmise what the issues you see are.

the serial class is so simple and reliable its odd you are still getting problems, please show some code as without it its impossible to know why you are seeing issues that do not appear to have been seen previously.

Mark

Are you seeing then when you’re stepping through code in debug mode? If so then it’s an illusion. The last couple of Xojo versions have had some very strange behavior for me when stepping through code. It will sometimes leap up to the parent function that is calling the thing you’re working in, or suddenly show an empty page, or start back at the top of the function only to then leap down to where it actually is after the next time you execute a single line.

The only way you can actually have the data available event fire while it’s already firing is if you start calling app.DoEvents() or app.yieldToNextThread or something else like that. As long as you’re not doing that in the DataAvailable event and you’re only seeing this while stepping through the code I think that is likely all it is.

Please look for a Feedback report to join as it’s quite frustrating.

Hi Guys

I found the cause, Me.Poll. The Poll method “Causes the control’s properties to update and causes the DataAvailable event to execute if any new data is available.” I was not expecting the latter part, “DataAvailable event to execute” only to update the properties. So performing the Poll method while inside of the DataAvailable event causes a NEW instance of the DataAvailable event to be created.

Screen shots attached. Note that each instance has different data in it!.



My solution was to exit out of the DataAvailable event and not perform the Poll method.

Tim

What version of Xojo are you working with? I’ve not seen that behavior and would consider that a bug.

Can you show the code that had the me.poll in it?

I know it is not the same. But what you describe reminded me of “polling” and interrupt inside the service routine. Thank God for watchdogs.

Hi Bob,

Version 2019R1.1 - I would consider it an inconvenience but sine the behavior corresponds to the documentation, not a bug. Ideally an update of the objects properties is what I was looking for.

The code was in the DataAvailable event - recall in a different thread I asked you where you had Me.Poll?

** In the DataAvailable event **
Do until ba >= MsgCount
  ba = Me.BytesAvailable
  buffer = me.LookAhead
  
  #If DebugBuild Then 
    Print "Ba = " + cstr(Ba) + " Buff Len = " + cstr(Len(buffer))
  #Endif
  Me.Poll  // This causes multiple instances of the DataAvailable event 
Loop
blen = Len(buffer)

There’s other code in there too, but this is the action that triggers a new event instance.

**What threw me off, is while stepping through the code, all of the data was received and processed (checksums lined up etc.) then the IDE would take be back to the same position each time inside of the DataAvailable event to the line just after the Me.Poll line. Was frustrating until I noticed the stack had multiple instance of the DataAvailable event. When I remarked the me.poll line, it all settled down.

So now instead of initiating what I thought was an update of the Controls properties, I exit from the event.

It would be very very nice if there was a way to only update the controls properties - but I do not see one in the docs.

Tim

Tim,
in the previous thread about the serial data there is a very precise explanation as to how a working example operates.

none of that involves calling Poll in the event (from my explanation) it uses a timer.
if data is available the DataAvailable event fires, and all data received is then in the buffer.

I do not understand why you would have any sort of loop in an event.
the event firing is effectively a loop, the loop is called by Poll so to speak.

Mark

Yes Mark that is how you did it, and I did consider that method and appreciate your feedback.

I did not want a timer to perform the action, I wanted the event to fire and my code to take it from there. Again, I was mistaken that .Poll not only updates the Properties of the object but also causes a new instance to fire. Had the .Poll method worked as I originally thought, updating the properties but NOT creating a new instance, performing a loop in that even would have been perfectly acceptable.

Thanks to everyone for their feedback in this!
Tim

Ah yes! that too :slight_smile: I forgot about that one :slight_smile: glad you’ve figured it out!

Back when the server socket had a bug where it wouldn’t send more than 8k to the DataAvailable event at a time I actually used this ability to more rapidly receive data. set a class variable that I was going to do it and if I hadn’t received all the data I needed I would call poll from within the data available event after setting that class boolean to true. Then when the data available event fired again I would just receive the data into a buffer and check to see if I had it all. If so I would set the variable to false and return which would release the polling loop in the original data available :wink: Was silly, but it worked as a work around for that problem until they fixed it.