Found a piece of code, working fine. But I do not fully understand.
do {
inString[inCount] = Serial.read();
if (inString [inCount] == 13) break;{
(++inCount < 20);
inString[inCount] = 0; // null terminate the string
}
}
while (Serial.available());
If the variable == 13 then the do-while loop will exit.
But why the { } after the break? Does it belong to the if? Skips the else?
What is (++inCount < 20);? Another if condition?
I don't know if you just copied and typed in by yourself, but IMHO it's wrong.
That curly brackets are irrilevant when used that way, the "(++inCount < 20);" doesn't have any sense (except for incrementing that variable) and an "if()" is missing.
Thus, I think it should read like:
do {
inString[inCount] = Serial.read();
if (inString [inCount] == 13) break;
if (++inCount < 20) {
inString[inCount] = 0; // null terminate the string
}
}
while (Serial.available());
That "if (++inCount < 20)" is needed to correctly end the received string (I suppose the variable is defined as "char inString[20];").
Yep, I hope that while loop is inside an outer "if (Serial.available())". I'd better put the while test before:
while (Serial.available())
{
inString[inCount] = Serial.read();
if (inString [inCount] == 13) break;
if (++inCount < 20) {
inString[inCount] = 0; // null terminate the string
}
}
Then, there's also a weird fact, if a '\r' (13) is received, the string terminator is missing (while the '\r' character will be part of the buffer). And what if a '\n' is received after the '\r'?
That's not really part of the OP question, but I'd use a different approach, like:
inCount = 0;
while (Serial.available()) {
char c = Serial.read();
// If is LF or max buffer length reached, then exit
if ( c == '\n' || inCount == 20) break;
// If not CR and the buffer is not full, add the character
if ( c != '\r' && inCount < 20 ) {
inString[inCount++] = c;
inString[inCount] = 0; // Always terminate the string!
}
}
Yep, but that was just my guess (maybe "char inString[21];" you're right) not knowing the complete code. Obviously on my code (the last one on post #6) just replace 20 with the buffer length+1 whatever it is, and it won't overrun
No, because of the "++inCount" you probably missed from that code. The '\0' (not '0') will always be added after the received character.
Obviously my code is just a snippet, as no full code has been published, so somewhere in the code should be a "inCount = 0;" statement to "reset" the input characters counter to zero, followed by a "inString[0] = 0;" to properly set the initial "empty string" waiting for the next data.
Anyway, this is the best solution IMO, where I added just the buffer reset before the while:
inCount = 0;
inString[inCount] = 0; // I forgot this!
while (Serial.available()) {
char c = Serial.read();
// If is LF or max buffer length reached, then exit
if ( c == '\n' || inCount == 20) break;
// If not CR and the buffer is not full, add the character
if ( c != '\r' && inCount < 20 ) {
inString[inCount++] = c;
inString[inCount] = 0; // Always terminate the string!
}
}