Software serial - making it wait

I am trying to make a Software serial "receiver" wait around until data is present if its not already there to be read.
Probably, in practice, use software serial , but anyhow, was thinking along the lines of:
I realise its blocking , but thats not an issue:

void setup()
{
  Serial.begin (19200);
  bool A = true;

  //  do other stuff other  setup type stuff
  //..........
  //...........
  // related to getting inital setup parameters from another processor :

  if (Serial.available > 0)
  {
    READIT();
    A = false;
  }

  while ((A == true) && (Serial.available == 0))
  {
    delay(50);
    if (Serial.available > 0)
    {
      READIT();
      A = false;
    }

  }
}// end setup

void READIT()
{
  //read data from serial port.
}

seems a bit "clunky", so wondered if there was a more elegant route ?

That's SoftwareSerial you want?

Edit:

#include <SoftwareSerial.h>

SoftwareSerial softSerial(2,3);

void setup() {
  softSerial.begin(19200);
}

void loop() {
  if(softSerial.available()) {
    // do your softSerial.read stuff
    
  }
}

Robin's updated serial input basics comes to mind. Just adjust it to use a SoftwareSerial instance.

If you need it to be blocking

while (newData == false)
{
  // one of Robin's the receive functions
}

Yep I’m happy with software serial , it’s reading the data , but making it hang around if the data is not there .
I’ve looked at serial basics , doesn’t cover this situation.

That is what the code I posted does. It waits around forever until something comes in on the SoftwareSerial port. I set my example for pins 2 and 3.

But if there no data when ‘serial.aveilable ‘ runs then it moves onto the next instruction.
If you notice I have it in void setup() so only one shot .

** aha !! **
So that will stay at that point and move on if >0 or if was > 0 when it executed . That’s much neater thx.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.