SerialEvent()... or what?

Hi guys!

I have to implement communication via serial port. I have a GPS module which sends to Arduino (via Serial port, i made an interface to normalize tension with a simple MAX232 since the module works at +-12 or so).
Ok, communication between Arduino and GPS is fine, 'cause i can read the GPS sentences. I'm using the SerialEvent() example but i have a "little" problem: it just doesn't clear the inputString. Via serial monitor i see the same sentences repeated over and over very fast but it doesn't clear the string: i just see the sentences repeated very fast and it stops only when i detach the RX pin or when i power down the GPS. Nor i can't compare the string! If i make

if(inputString == "Ready")

where Ready is actually the string received by Arduino it seems that he doesn't enter in the 'if' condition.

Here's the code, which is nearly the original:

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    Serial.println(inputString); 
    /* this is where my added code is, but for some reason it doesn't work*/
    if(inputString=="Ready") {
       Serial.print("Ok, please begin");
    }
    // clear the string: this doesn't work.
    inputString = "";
    stringComplete = false;
  }
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    } 
  }
}

What i have to think? Maybe there's a mistake on the MAX232 interface, like a loop on some pin, but... i don't think: i tried to look with an oscilloscope and i see what i'm expecting to see.

This is a weekend project, but i'm expecting it to work! :wink:

Thank you guys!

You are adding the character to the inputString BEFORE you check to see if it is an newline. That means your inputString contains "Ready\n" which is not the same as "Ready". Check for '\n' and '\r' FIRST and only add the character if it isn't one of those.

Thanks for the reply. I looked for "READY\n" but it doesn't enter in the if statement.
For me it sound strange... nor i think i can check for "\n" char before, because then i don't know how to manage the entire string. Could you make an example, maybe i didn't understand right.

Thanks!

EDIT: I'm a little bit confused, but your solution works great! I just made this way:

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    // add it to the inputString:
    if (inChar != '\n') {
      inputString += inChar;
    } 
    
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true; 
    }

    
  }
}

The only thing to adjust is that he repeats over and over the strings. The only way for stopping it is to detach rx or tx pin. Maybe there's a mistake with the interface? Or do i need something else?

Thanks again!

altagest:
The only thing to adjust is that he repeats over and over the strings. The only way for stopping it is to detach rx or tx pin. Maybe there's a mistake with the interface? Or do i need something else?

The code you have looks correct. It should only print once for each "Ready" it receives. Is there a program sending the "Ready"? If so, that may be wrong.

Nope... i have a GPS module that sends "Ready" once it's ready. After that, i can ask the module the string i need, but it only prints "Ready" one time. I checked with a terminal, interrogating the module and effectively it replies "Ready" after few seconds that he turns on. Maybe there's a flaw in the interface? What makes me think, is that in every loop, if the string is complete, i clean the inputString with

inputString = "";

but this doesn't seem to clean anything, because he repeats that string! It only stops when i detach RX pin. Ideas?

Many thanks!

Perhaps your GPS is responding "Ready" when you send it the command "Ok, please begin".

You might want to consider using SoftwareSerial for the GPS and reserving Serial or debug output to the Serial Monitor. You won't be able to use "SerialEvent()" but you can re-name it "MySerialEvent()" and put this at the top of loop():

if (GPS.available())
    MySerialEvent();

No: the GPS sends out "Ready" only when it turns on. After, it doesn't send "Ready" anymore. Good idea to use SoftwareSerial, i never used it, i think i'm gonna learn, and that's great :smiley:

By the way, i still consider that there are errors on the interface. It's too strange that he doesn't clear the string. My curiosity stay there: why the string isn't cleared? It's like there is a sort of buffer that, indeed, doesn't exist. He seems to receive and send at same time, throwing a sort of loop that only stops when i phisically detach the RX pin on the Arduino. Next try is to detach serial port from the GPS to see if Arduino still repeats. That will be the confirmation that there are mistake in the code or in the interface :wink: