Is a Serial.DataReceived event guaranteed to run to completion before it is raised again?

Jan 12, 2015 at 10:52 PM
I am working on a Gadgeteer module driver, which talks to the module hardware via Gadgeteer.SocketInterfaces.Serial. It is possible that the device could send more than one sequence of bytes without any intervening action from the Gadgeteer application.

Now to the question: In the Gadgeteer environment, if the Serial.DataReceived event is raised and my registered event handler is called, can I be sure that the event handler will run to completion before the same event handler is called with a second set of data, even if the additional data arrives from the device before I return from handling the first event? Or do I need to ensure that my event handler is reentrant, so that I properly handle the second event even if the first event is not completely handled?

I'm trying to keep the event handler small and simple, but want to make sure I don't oversimplify, if it's possible I'll get called again before I'm finished.

Jan 13, 2015 at 1:35 PM
Edited Jan 13, 2015 at 1:36 PM
Yes, that's guaranteed for .NET Gadgeteer code. (Custom virtual drivers could use multiple threads to circumvent this behaviour.)

In .NET Gadgeteer, the event handlers are called synchronously from one thread and the underlying .NET Micro Framework currently does not allow for re-entrancy of hardware event handlers. If you do not dispatch the incoming serial data quickly enough, you may end up with hardware buffer overflow. How the hardware deals with that is subject to individual manufacturers, there are devices with fatal consequences (i.e. dump/restart) and those that simply ignore it or output it to the display controller.
Jan 13, 2015 at 3:47 PM
Thanks for your explanation.