serialEvent() with the Esplora,Leo, Micro

Hello,

is this limitation just a question of patience and wait for implementation or is it a permanently hardware problem ?

NB : Currently, serialEvent() is not compatible with the Esplora, Leonardo, or Micro

wally

You don't need to use serialEvent anyway. I never do.

In your main loop check for Serial.available and act accordingly.

Nick,

yes, sure, this is the polling approach but is not very efficient.
The event driven approach has also advantages dependent on task.
An answer is still highly welcome.

wally

All that serialEvent does is poll the outstanding serial requests after calling loop.

This is main.cpp:

#include <Arduino.h>
int main(void)
{
	init();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();   
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}      
	return 0;
}

See? Polling.

Just move your test for serial data into loop and you have something just as efficient.

Hi to All

I’m a newbie of Arduino and C language

I have a problem on above code

That is Pure C , can somebody help me transform it in Arduino IDE language , this can help me also to understand how it work .

Best Regards

Francesco

That is Pure C , can somebady help me transform it in Arduino IDE language ,

Yes, it is C/C++
So it already is the "Arduino language"

AWOL:

That is Pure C , can somebady help me transform it in Arduino IDE language ,

Yes, it is C/C++
So it already is the "Arduino language"

But if i put on arduino Ide give me some compilation error

Sorry i'm really newbie

Thanks

The IDE provides that code for you invisibly, so all you need to do is provide the bodies of "setup" and "loop"

AWOL,

can you provide a complete sketch which demonstrates it, please ?
thx

void setup ()
{
}
void loop()
{
}

It's done for you, and it's invisible

What is it you want to do?

Thank you for your answer and your time

And also thanks to Wally123 for support me XD

AWOL,

for my understanding this skeleton does nothing except looping.

Let's say, i want to switch LED13 ON when something received on serial port.

Put your code to turn on the LED in a "void" function called "serialEvent".

If you get stuck, have a look here

what does this mean : ?
NB : Currently, serialEvent() is not compatible with the Esplora, Leonardo, or Micro

wally123:
Let's say, i want to switch LED13 ON when something received on serial port.

Write some code which tests whether there is input available on the serial port, reads it and buffers it until a complete message is available, and then processes the message in whatever way you want. Put this code within loop or some function called from loop(). This is a very common requirement and there are tons of examples demonstrating how to do it. Using serialEvent() is not necessary and is IMO not at all helpful in dealing with this problem - it just hides what's going on.

Ok, super glad to find that out! Thank you.

Do give the OP some credit though-- it is completely undocumented why serialEvent isn’t supported on the Arduino Micro, Leo, etc. It’s fine now that we know that serialEvent is just a call tacked on to the end of main, but really, someone should make that clear in the reference guide! (Also, someone needs to put a comment in the tutorials/example code, because the serialEvent example, for one, is broken by this on Micro, Leonardo, etc. because it relies on serialEvent. It didn’t even trip a compiler warning for me!)

Even more mysterious as to why those boards are so special as to have the call to serialEvent missing from their main loop. Weird. Aren’t those all the on-cip USB boards? Must be something like the on-chip USB makes the call slow things down on those boards. Still, why call serialEvent on any of the boards if it just amounts to polling anyway? Like why trick people into making it seem like it’s really an interrupt when it isn’t? Seems like that could get some sketches into trouble-- like if you had a really loose and lengthy void loop() and were relying on serialEvent to interrupt when needed, you’d be scratchin your head as to why it’s taking so long, or maybe even lose data? (Or is it buffered internally?)

I agree with you.

I don’t like the idea of “loop” having an extra “hidden” function call like that. It isn’t elegant.

(Or is it buffered internally?)

Incoming serial data is buffered up to the size of the serial buffer which on the larger processors is 64.