Have to call loop(); inside serialEvent1()

Hi everyone,

Just a little query here. I have 2 arduinos talking to each other over rs485, and the serialEvent1 on receiving mega is always being called. So often in fact that the program never returns to the main loop. I've ended up having to put a loop(); call inside on serialEvent1.

Here is my code. Please note I am still tinkering so this might be the cleanest code you've ever seen.

void serialEvent1() {
  //Serial.println("Serial Event Called");

  if (!stringComplete) {
    while (Serial1.available()) {
      byte inByte = Serial1.read(); //Read each character one by one
      Serial.print(inByte);
      Serial.print("  ");
      Serial.print((char)inByte);
      Serial.print("  ");
      Serial.println(inByte, BIN);
      
      if (inByte == 'X') {
        Serial.println("Termination Character Received");
        data[serialCounter] = inByte;
        if (data[serialCounter] == 'X' && data[serialCounter-2] == 'C') {
          Serial.println("------");
        }
        stringComplete = true;
        loop();                      <-----------------------------------------------------
        //break;                               <--- This also doesn't seem to work
      } 
      else if (serialCounter >= serialSize) {
        Serial.println("Input Array Full");
        for ( int i = 0; i < sizeof(data); i++ ) {
          data[i] = (byte)0;
        }
        serialCounter = 0;
        break;
      }
      else if (useful(inByte) && serialCounter < serialSize) {
        data[serialCounter] = inByte;
      }
      serialCounter ++;
    }
  }
}

At the moment I'm timing the serial sends to this arduino every 500ms. If anyone could shed some light on what I'm missing then I would really appreciate it.

Many thanks.

lacanau:
At the moment I'm timing the serial sends to this arduino every 500ms. If anyone could shed some light on what I'm missing then I would really appreciate it.

Many thanks.

I'd say just don't use serialEvent(). Rather, just test for new serial data in loop:

void loop()
{
  if (Serial.available())
  {
    //do something
  }
  // other stuff
}

and avoid using String class... stick to parsing C strings like demo's here in Serial Input Basics

NEVER call loop(). loop is called from main() as is serialEvent() your loop() is being called over and over and is getting all the time it needs

Post ALL your code.

Mark

If you look at main() you'll see that every time serialEvent is called, loop is called first. The problem you describe is impossible. The two functions just alternate, loop serialEvent, loop, serialEvent, loop.

BulldogLowell:
I'd say just don't use serialEvent(). Rather, just test for new serial data in loop:

void loop()

{
  if (Serial.available())
  {
    //do something
  }
  // other stuff
}




and avoid using String class... stick to parsing C strings like demo's here in [Serial Input Basics](https://forum.arduino.cc/index.php?topic=288234.0)

Thanks for your reply. What are the benefits to adding a serial event condition inside the main as an alternative to serial.Event() ?

lacanau:
Thanks for your reply. What are the benefits to adding a serial event condition inside the main as an alternative to serial.Event() ?

You don't add one - it's already there:-

int main(void)
{
	init();

	initVariant();

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

lacanau:
Thanks for your reply. What are the benefits to adding a serial event condition inside the main as an alternative to serial.Event() ?

Did you study the link you were given - Serial Input Basics?

...R

Robin2:
Did you study the link you were given - Serial Input Basics?

...R

I have indeed, and at no point does it make reference to the serial.Event() function explained in the arduino documentation?

Can you shed any light on the differences/benefits to using 'if (Serial.available())' inside of the main loop instead of 'void serialEvent()' ? The arduino documentation states that it is called every time that the program detects data in the buffer, but I'm not sure if this is interrupting the main program or it is called to check for data in the buffer at the end of every loop cycle. I'm uncertain as to what OldSteve is talking about.

Thanks again.

I'm not sure if this is interrupting the main program or it is called to check for data in the buffer at the end of every loop cycle.

It is called at the end of loop() if serial data is available.

As has been suggested, you might just as well ignore the serialEvent() mechanism and simply write your own routine.

lacanau:
I'm uncertain as to what OldSteve is talking about.

Simple. You said "What are the benefits to adding a serial event condition inside the main".
And I was just saying that nothing needs to be added inside 'main()'. It's already there. ie There are no benefits.

OldSteve:
Simple. You said "What are the benefits to adding a serial event condition inside the main".
And I was just saying that nothing needs to be added inside 'main()'. It's already there. ie There are no benefits.

Ah ok I'm following now.

I have simply changed my serial receive functions name and popped it inside my loop.

Thanks everyone for your help :slight_smile:

lacanau:
I have indeed, and at no point does it make reference to the serial.Event() function explained in the arduino documentation?

You are quite right, and I apologize - I must have written about serialEvent() elsewhere. I will add a little bit to the Tutorial.

serialEvent() behaves exactly as if you had the following code as the last thing in loop()

if (Serial.available > 0) {
 mySerialEvent();
}

...R

Robin2:
You are quite right, and I apologize - I must have written about serialEvent() elsewhere. I will add a little bit to the Tutorial.

serialEvent() behaves exactly as if you had the following code as the last thing in loop()

if (Serial.available > 0) {

mySerialEvent();
}




...R

Thanks for that. I admit putting the call inside the loop itself has made me more aware of the procedure which does come in handy to remember, especially when the program gets XL size :slight_smile: