Flush-serial1()

I'm trying to communicate with a NO2 SPEC sensor via the Serial1 port on the DUE. The program I'm using is contained on this address:

In the program is a flush.serial1() section:

void flush_serial1(){
// Do we have data in the serial buffer?
// If so, flush it
if (Serial1.available() > 0){
Serial.println ("Flushing serial buffer...");
while(1){
inChar = (char)Serial1.read();
delay(10);
Serial.print(inChar);
if (inChar == '\n') break;
}
Serial.println (" ");
Serial.println ("Buffer flushed!");
}
}

which just produces little squares printed across the screen without stopping. The buffer never gets flushed.
What should I change to flush serial1 so that I can start communicating with the sensor?
Thanks for your time.

if (inChar == '\n') break;

What happens if the buffer does not contain a newline character ?
Why not simply read from Serial1 until there is nothing available ?

Do not print any bytes that are read as they may well be unprintable characters

  • did you open your Serial monitor at 9600 bauds?
  • is your sensor configured at 9600 bauds?
void flush_serial1()
{
  // Do we have data in the serial buffer?
  // If so, flush it
  Serial.println ("Flushing serial buffer...");
  while (Serial1.available() > 0)
  {
    Serial.print(Serial1.read());
    delay(10);
  }
  Serial.println ();
  Serial.println ("Buffer flushed!");
}

I think the goal was to empty the incoming buffer up to the next 'end marker' - not get rid of everything

J-M-L:
I think the goal was to empty the incoming buffer up to the next 'end marker' - not get rid of everything

The original code also appears to assume that the incoming data on the serial port will be printable characters, so there is a possibility the OP has a hardware problem.

assume that the incoming data on the serial port will be printable characters

Indeed - hence my question on his Serial Monitor settings. Would not be the first time it's set to something different than what the code has declared (9600 here).

If that's correct then the next questions could be on 3.3V versus 5V devices, crossing Rx and Tx, connecting GND etc

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