PaulS:
That code is nonsense.
if (Serial.available()) { // when i send message
delay(10); // wait for message to get in the buffer before proceeding
while (Serial.available()) {
if (var == 1) {
Len = Serial.available() + 1; // will be the length for the buffer in the following code
var = 0;
}
char c = Serial.read(); // read a byte as char
tekst += c; // adds the char to the empty string
}
If you sent properly delimited data, you could read and store the data, as it arrives, as fast as it arrives, without hoping that it all gets there before you read any of, while you've stuck your thumb up your ass.
You KNOW how much data you read. You've pissed away resources using the String class. The tekst instance KNOWS how many characters it contains. Ask it. Don't expect to capture the length in Len. What are you going to to when more data arrives while you are reading data, and the value in Len doesn't match the number of characters in tekst?
You are not copying the NULL terminator into old_buf. Why not?
Since you are not, old_buf is NOT a string, and you should NOT be passing it to functions that expect strings.
thank you for you negative response, il explain something about the code:
if (Serial.available()) {
delay(10);
while (Serial.available()) {
char c = Serial.read();
tekst += c;
}
first of all, the nonsense code is ment to receive message from variale length, for instance I can send: "F U" or I can send: "F*ck you and your negativity"
I can also put a 1ms delay int he while loop itself and ditch the 10ms. Do you even know what happens if you dont use a delay here??
The code will jump into the while loop, the code reads a character, the code adds the character to the empty string and than! the code checks the while condition again, but!! the code ran through the while loop so incredibly fast the 2nd byte has not reached the buffer yet, and the code will jump out of the while loop, having read one whole single byte, despite I send many more bytes. afterall the baudrate is only set on 9600 bps.
The most efficient way using delays like this, is to set the baud rate to 115200bps and than you need a delay of only 40microseconds in the while loop itself. like this:
if (Serial.available()) { // when i send message
while (Serial.available()) {
delayMicroseconds(40);
tekst += char(Serial.read()); // read a byte as char and adds it to the string
}
It would be even better to use a timer so you can check every millisecond if Serial.available() has incremented and if not, you have a full message in the buffer.