while (Serial1.available ()> 0) Difference in position, operation difference?

Serial0 is used for monitoring
Serial1 is used for GPS monitoring
Serial2 is used for wireless communication

Serial0 will modify the variable when the request signal comes every second. (To be implemented)
Serial1 receives the GPS signal every second, and stores the latitude and longitude value in a variable.
Serial2 uses AT COMMAND every second to save the parameter value that comes in response.

Currently, Serial0, 1, and 2 are operating in loop ().

Question 1: Do I always have to wait in the loop() to wait for a response?

Question 2: Can not I always get the value every time I answer without having to wait?
Is it possible to use serialEvent()?

Question 3: If I put while (Serial.available ()> 0) {// action} in setup(), I can not escape.
The next function does not seem to be executed. Why?

Question 4: I called the Serial function once a second in the timer.
It's not working, is it a problem with my code?

void loop() {
    /*
     If the function is called in succession,
     Delay () between function calls does not work well
     When the function is terminated, the following function is executed.
    */

    if (!LoRaParameter_oneTime && !GPS_oneTime && !Setting_File_SDCARD_oneTime) {                      
    LoRa_SaveBuffer(); // for wireless communication
    }
    else if (LoRaParameter_oneTime && !GPS_oneTime && !Setting_File_SDCARD_oneTime) {
      GPS_SaveBuffer(); // get the GPS data
    }
    else if (!Setting_File_SDCARD_oneTime && LoRaParameter_oneTime && GPS_oneTime){   
      Check_Setting_File_SDCARD();  // for SPI communication
    } 
    else if (Setting_File_SDCARD_oneTime){
       // some sensor action;
    } 
} // loop


void LoRa_SaveBuffer() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = 'D';
  char endMarker = '\n';
  char rc;
  Serial2.println("$Rpar=");
  while (Serial2.available() > 0 && newData == false) {
    rc = Serial2.read();
    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }

  }

  if (newData == true ) {
   // action
  }
  LoRaParameter_oneTime = true;
  newData = false;
}



void GPS_SaveBuffer() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '

I have heard from someone that this source is very inefficient. Since Serial code is still waiting for communication in the loop () function, this serial code can expect all communication timing delays if other sensor extraction operations are in loop (). someone recommend using serial interrupt, then should I use serialEvent ()?
;
  char endMarker = '\n';
  char rc;

while (Serial1.available() > 0 && newData == false) {
    rc = Serial1.read();

if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }
    else if (rc == startMarker) {
      recvInProgress = true;
    }

}

if ((newData == true) ) {
  // action
  }

GPS_oneTime = true;
  newData = false;

}


I have heard from someone that this source is very inefficient. Since Serial code is still waiting for communication in the loop () function, this serial code can expect all communication timing delays if other sensor extraction operations are in loop (). someone recommend using serial interrupt, then should I use serialEvent ()?

1. You may go this way – the polling strategy:

byte n, n1, n2;

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial2.begin(9600);
}

void loop()
{
  if (Serial.available() > 0)
  {
    n = Serial.available();
    //porcess data
  }
  if (Serial1.available() > 0)
  {
    n1 = Serial1.available();
    //process data
  }
  if (Serial2.available() > 0)
  {
    n2 = Serial2.available();
    //process data
  }
}

OR

2. You may go this way – the interrupt strategy:

bool flag, flag1, flag2;
byte n, n1, n2;

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial2.begin(9600);
}

void loop()
{
  if (flag == true)
  {
    n = Serial.available();
    //porcess data
    flag = false;
  }
  if (flag1 == true)
  {
    n1 = Serial1.available();
    //process data
    flag1 = false;
  }
  if (flag2 == true)
  {
    n2 = Serial2.available();
    //process data
    flag2 = false;
  }
}

void serialEvent()
{
  flag = true;
}

void serialEvent1()
{
  flag1 = true;
}

void serialEvent2()
{
  flag2 = true;
}

narutorobot:
...someone recommend using serial interrupt...

Interrupts are nearly always less efficient by every possible measure.

And, you are already using serial interrupts. Serial is interrupt driven.

narutorobot:
I have heard from someone that this source is very inefficient.

The next time that someone makes such a claim politely ask them to provide mathematical proof that the source is very inefficient then sit back and enjoy the show as they trip over their own tongue from their linguistic gymnastics.

GolamMostafa:
OR
2. You may go this way -- the interrupt strategy:

SerialEvent() may look like an interrupt but it is not. It is just polling. It just checks serial.available() and c=runs the code in the function if it is greater than 0.

...R

narutorobot:
Question 1: Do I always have to wait in the loop() to wait for a response?

I don’t really understand what was in your mind when you wrote that question.

If this was my project my code in loop() would be simpler - like this

void loop() {

    receiveLoraData();

    receiveGpsData();

    if (newLoraData == true) {
       // do something
       newLoraData = false;
    }

   if (newGpsData == true) {
      // do something
      newGpsData = false;
   }
}

and then there would be no waiting around for anything

…R
Serial Input Basics - simple reliable ways to receive data.

Robin2:
SerialEvent() may look like an interrupt but it is not. It is just polling. It just checks serial.available() and c=runs the code in the function if it is greater than 0.

By definition and operation, an ISR is automatically called upon by the processor without any intervention from the user. From this view point, can the serialEvent() be called an ISR?

Until now, I realized that I had misunderstood that I knew the serialEvent () function as an interrupt.
and I thought that 'while (Serial1.available ()> 0)' and 'if (Serial1.available ()> 0)' are the same functions.

narutorobot:
[…] and I thought that ‘while (Serial1.available ()> 0)’ and ‘if (Serial1.available ()> 0)’ are the same functions.

The following two blocks of codes may help you to get better idea on the difference between if((Serial1.available ()> 0) and while(Serial1.available ()> 0).

if (Serial1.available ()> 0)
{
   byte n = Serial1.available();  //checking if any character has been accumulated in the FIFO buffer
   for (int i=0; i<n; i++)           //there are n bytes data present in the FIFO buffer
   {
      myArray[i] = Serial1.read();  //bring out the data from unseen FIFO buffer and store into user array.

   }
}

//===================

int i=0;
while(Serial1.available ()> 0)     
{
 myArray[i] = Serial1.read(); //there are data in the FIFO; bring them out and save in user array
 i++;
}

GolamMostafa:
By definition and operation, an ISR is automatically called upon by the processor without any intervention from the user. From this view point, can the serialEvent() be called an ISR?

NO.

Study the source code.

I believe the serialEvent() function was introduced to be similar to a Processing function that has that name, It may well be interrupt driven in Processing but it is not on an Arduino.

...R

narutorobot:
Until now, I realized that I had misunderstood that I knew the serialEvent () function as an interrupt.
and I thought that ‘while (Serial1.available ()> 0)’ and ‘if (Serial1.available ()> 0)’ are the same functions.

I don’t understand.

In both of the pieces I have coloured the functions are the same as far as I can see.

…R

I don't understand.

I presumed that the OP was referring to the difference between while and if rather than Serial1.available()

UKHeliBob:
I presumed that the OP was referring to the difference between while and if rather than Serial1.available()

I was hoping to provoke the OP into pointing that out and, in the course of doing so he might explain more clearly what is really troubling him :slight_smile:

...R

Thank you for reply
sorry. I made a typo. it was right, I was talking difference about 'while loop'and 'if loop'.
And Also I wanted to ask question no.3
Is the meaning of using Serial.available () in the setup () function simply to initialize?

narutorobot:
Is the meaning of using Serial.available () in the setup () function simply to initialize?

No. The initialization is done with Serial.begin()

Serial.available() is just a test to discover how many bytes are waiting in the Serial Input Buffer

…R

I was talking difference about 'while loop’and ‘if loop’.

There is no such thing as an ‘if loop’ There is an if test which can conditionally execute a block of code but the program will then move on and execute the next instruction