In my recent project, I am working with serial data. Till now, I'm able to receive data and display on LCD. Now, suddenly I can't get any serial data. Nothing display on LCD.
Lets say your incoming string which is 54 bytes long display at the first time, then a second string arrive , from what I can read of your code you will concatenate the next string to the oldest one resulting in something like
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.
hsn32:
I know that concept. But not idea about how to use it. Because the length of that upcoming data may be not known.
The exact length isn't that important, what's important is the max length it might be
Just make a string that can hold the max length. DON'T make this extremely big. If you're thinking >100 then you should rethink what you send to the Arduino. And you don't fix this by using String, on the contrary, with the possibility to keep extending the String the problem of memory corrupting may occur rather fast.
Robin2:
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parsing example.
...R
Yes, using String Heap fragmentation happened. Now I check it put your link about "cstings". So, here we have to use a Header file and use functions. But which function I have to use?
You must end the last element of the array with a '\0' to became a string
Then it can be printed.
Maybe something like this will work(Don't have time to test it now)
uint8_t indexPos = 0;
while(Serial.available())
{
char in = Serial.read();
response[indexPos++] = in;
}
//Terminate the array in the end
response[indexPos] = '\0';
//reset the index pos to process the next one
indexPos = 0;
//ready to be printed
lcd.setCursor(0, 0);
lcd.print(response);
hsn32:
Yes, using String Heap fragmentation happened. Now I check it put your link about "cstings". So, here we have to use a Header file and use functions. But which function I have to use?