I want to append text received from different channel of serial port. just as a trial, (not yet including the mux and stuff) i try a simple text append. the code is below.
it doesn't perform as expected.
then i move the string declaration from top to the inside of void loop. then it works as expected. Can someone explain to me why?
I also had to copy strOut to another string container (strBuf here) before appending the next data. So the operator += just simply doesn't work for string?
oh, i send the word using both CR and LF, and detect LF as the word end. any smarter method?
when it was declared outside void, i meant it to be global variable, it appended the text i send thru serial with gibberish letter, and it works only until the third word i sent. none of them appended properly. the 4th and next gave no respond at all, just like a computer in crash.
as expected means it appended the text properly, without gibberish letter, and without hang. after the 8th word was sent, it was able to repeat the cycle. just as how i intented the sketch to be.
in short, problem solved. But i just dont understand the logic behind.
when it was declared outside void, i meant it to be global variable
What does "it" refer to?
There is no "outside void". There is a function, called loop() whose return type is void. If it's return type was int, you would say "when it was declared outside int..." would you?
it appended the text i send thru serial with gibberish letter
I'm pretty sure that the Arduino doesn't understand gibberish any more than we do. There's that "it" again, with no clue as to what "it" is.
You can use Serial.print() to print the arrays and Strings at any time. Of course, if you are stuffing characters in an array, and expecting to use that array as a string (lower case s), you must keep the array of characters NULL terminated, which you aren't bothering to.
as for the second "it", i mean arduino, or the result that i receive from the serial monitor.
Of course, if you are stuffing characters in an array, and expecting to use that array as a string (lower case s), you must keep the array of characters NULL terminated, which you aren't bothering to.
I prefer to add a NULL after each character is added to the array. That way, at all times the array is NULL terminated. And, one NULL is sufficient. Filling the rest of the array with NULLs is a waste of effort.
//all channel read, output and reset ch counter to 0
Serial.println(strOut);
chan =0;
}
This comment is wrong, and the action taken is also wrong. You are assuming that all the serial data has arrived for all channels before you start reading anything. Lousy assumption.
And, one NULL is sufficient. Filling the rest of the array with NULLs is a waste of effort.
i see. noted.
You are assuming that all the serial data has arrived for all channels before you start reading anything.Lousy assumption.
oh, how is that?
i defined the counter chan to be zero at first. the while(chan <= 7) wont exit until the counter chan go past 7. And the counter wont go up until there is any LF string received.
btw, that was not the question here. the same sketch, with all the lousy assumption works just fine, as long as i move the code
String strOut;
from the ninth line to inside void loop{}
so what lousier assumption did i make?