UART problem

hi
I was no able to discover the reason why a strange happening using UART. So
the idea was test it with arduino Ide and with a standard example of Arduino.
It happens the same as in my program. The example is the following:
/*
Serial Event example
...................
...................
/
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);
// clear the string:
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;
}
}
}

Ok it works, but i made a modification including the following instruction
while(stringComlete == false){};
so the loop become:
void loop() {
// print the string when a newline arrives:
while(stringComlete == false){};
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;

}

It doesn't work any more.
It is the same problem had in my program. I can not debug arduino Ide, but
I will report what happen to me. The stringComple never become true. When
you send a string, it is read an memorized in the flash of the cpu followed
by 0x0d 0x0a but it seems that the if routine in the interrupt is not any
more able to detect the '\d' character.
The question is WHY?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

And please don't double Post

...R

Hi
At fist sorry for the post
I have read what suggested, but I agreed with you partially.
Reading the port you have two way the one is to make the polling on the flag for the buffer ready to be read the second is using interrupt.
Should be no difference in using the two way, for sure if you are using them correctly . I posted the example because it is strange.
Should be no reason at all because changing

if (stringComplete == true) {
Serial.println(inputString);
inputString = "";
stringComplete = false;
}

with

while(stringComplete == true) {}
Serial.println(inputString);
inputString = "";
stringComplete = false;

the interrupt should be affected except if.....I do not know

I have also upload this example on a nano board using studio7 and an ATMEL-ICE trying to solve the problem. The curious thing is that the string is
correctly received, is stored in the flash memory at location 0x... but the last character 0x0a is not recognized any more by

if (inChar == '\n') {
stringComplete = true;
}

I have placed a breakpoint in stringComplete = true
I have modified (inChar == '\n') with (inChar == 0x35) then I sent 5555
the result is the same: in the memory you have 0x35 0x35 0x35 0x35 0x0d 0x0a
but no breakpoint it means that the if instruction is never be done.
This is the reason of the post. How is it possible?

I can't follow your code the way you have posted it. Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor for study. See How to use the Forum

And please post complete programs that I can compile.

...R

This is the code

/*
Serial Event example

When new serial data arrives, this sketch adds it to a String.
When a newline is received, the loop prints the string and
clears it.

A good test for this is to try it with a GPS receiver
that sends out NMEA 0183 sentences.

Created 9 May 2011
by Tom Igoe

This example code is in the public domain.

*/

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:
while (stringComplete) {}; // modified instruction
Serial.println(inputString);
// clear the string:
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;
}
}
}

elpablito:
This is the code

That is no better. Please study Reply #3 carefully. Help us to help you.

...R

Hi
Thanks
Solved. void serialEvent() is not an interrupt subroutine.