sterretje's reply #9 identifies the problematic code:
You also print the received text every time that you receive a single character.
This means that you are printing 1+2+3+ ... +n
characters. There is a formula for that sum: (n2 + n) / 2.
For a response of 144 characters, you are trying to print more than 10,000 characters. -_- Don't do that. Here's what's happening:
After the 1st character comes in, you print 1 character. That character (and a newline) is copied to the Serial output buffer, and the print returns. The 1st characters starts to go out, but the output buffer still has 1 character (a newline).
After the 2nd character comes in, you print 2 characters. The 1st character has gone out, but the newline has not, so the 1st and 2nd characters (and a newline) are copied to the output buffer, and the print returns. The output buffer contains 3 characters.
After the 3rd character comes in, you print 3 characters. The newline character from the 1st print has gone out, but the output buffer still contains the 3 characters from the 2nd print. This 3rd print adds 4 characters to the output buffer and returns. The output buffer contains 7 characters.
After the 4th character comes in, you print 4 characters. The 1st character from the 2nd print has gone out, but the output buffer still contains the 6 characters. This 4th print adds 5 characters to the output buffer and returns. The output buffer contains 11 characters.
...
After the 11th character comes in, the 64-character output buffer is full. You will have to wait 12 character times (12ms) to print the 12 characters you have so far (11 characters of the response plus a newline). During that time, 12 new characters will arrive.
Then you will read the 12th character and print the 12 characters in BTbuffer
(plus a newline). But the output buffer is still full, so that Serial.print
will have to wait 13 character times before it can return. Aaaaand another 13 characters will arrive. There are now 25 characters in the input buffer.
Then you will read the 13th character and print 14. But the output buffer is still full, so that Serial.print
will have to wait 14 character times before it can return. Aaaaand another 14 characters arrive. There are now 39 characters in the input buffer.
Ditto 14th... There are now 53 characters in the input buffer.
Ditto 15th... But this is where it fails, because the input buffer only has room for 64 characters. You have lost characters 65..68.
You are simply printing too much. It's not a problem copying characters. The characters were dropped, because the input buffer overflowed while the output buffer was being emptied.
Instead, only print the response when the entire line has arrived:
void pumpBTBuffer() {
char c = BTSerial.read();
if (BTBufferIndex < sizeof(BTBuffer) -1)
BTBuffer[BTBufferIndex++] = c;
if (c == '\n') {
BTBuffer[BTBufferIndex] = '\0'; // NUL-terminate
Serial.println(BTBuffer);
// This is a good time to return 'true' or change state
}
}
Using sizeof
allows you to change the buffer size and not have to search and replace all the 300s (or 299s) with the new size.
Robin2's code does not have this problem.
Cheers,
/dev
P.S. This may be off by a character or two... 