Pages: [1]   Go Down
Author Topic: SerialEvent()... or what?  (Read 2037 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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! smiley-wink

Thank you guys!
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8966
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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!
« Last Edit: January 23, 2012, 03:32:29 am by altagest » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8966
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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!
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8966
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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():
Code:
if (GPS.available())
    MySerialEvent();
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin

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 smiley-wink
Logged

Pages: [1]   Go Up
Jump to: