populate string array from serial

Im trying to store a series of strings being sent over serial. I have a variable number of strings that will be sent and I would like to fill an array as they come in.

int numStrings = 20; //this number will vary between 1 and 25
String inData;
String myString[25];

int i = 0;

while (i < numStrings) {
  char recieved = Serial.read();
  inData += recieved;
  if (recieved == '\n') {
    myString[i] = inData;
    Serial.print(myString[i]);
    i++;
    inData = ""; // Clear received buffer
  }
}

the myString array is blank and I cant figure out what im doing wrong.

beyond that im guessing there is a better way to use a char array instead of string array for storing?

I appreciate any help -thank you.

You are not processing serial input correctly. You "forcefully" read data without checking if your data is even there.

Look up the Basic Serial Input turorial.

Don't use the String class. Use a char array for your strings. As a partial answer:

char myString[25];
// bunch of code down to loop()

void loop()
{
   int charsRead;

   while (Serial.available() > 0) {                                             // Anything in Serial buffer?
      charsRead = Serial.readBytesUntil('\n', myString, sizeof(myString) - 1);  // need to add null
      myString[charsRead] = '\0';                                               // make it a string
      Serial.println(myString);
    }
  }

The readBytesUntil() method of the Serial object returns the number of characters read from the Serial buffer. You can use that fact to figure out where the end-of-string is in the array and write the null string termination character ('\0') at that element in the char array.

In the receiver section, you can simply execute the following codes to populate your character type array. The data bytes that are arriving are of character type (ASCII Codes) and not binary bytes.

char myArray[50] = "";  //arbitrary size of the array; null will automatically appended.
int i = 0;

void loop()
{
   if(Serial.available()>0)
   {
      
      byte x = Serial.read();
      if (x != 0x04)                 //0x04 code to mark the end of Text message
      {
         myArray[i] = x;
         i++;
      }
      else
      {
         i = 0;
         Serial.println(myArray);
      }
}

econjack, GolamMostafa,

Thank you both for taking the time and for your detailed reply. After studying both responses I realized I overlooked / didn't fully grasp the serial.available function.

This code worked for what I was looking to accomplish:

int numStrings = 20; //this number will vary between 1 and 25
String inData;
String myString[25];

int i = 0;

while (i < numStrings) {
  while (Serial.available() > 0) {
    char recieved = Serial.read();
    inData += recieved;
    if (recieved == '\n') {
      myString[i] = inData;
      Serial.print(myString[i]);
      i++;
      inData = ""; // Clear received buffer
    }
  }
}

Although this works i can see from your examples theres some things Im probably missing. I need to spend more time studying Serial.readBytesUntil - im curious about the size of the string in this function as I dont know how long the string will be thats being sent. this is why I assembled byte by byte until the /n is received. Thanks again. I appreciate you taking the time and giving me some examples to look at this really helped me out.

You still need to NOT use the String class. You can assign a length to the array that will accommodate the largest anticipated input stream and simply change your code to

char myString[80] = {};  // use whatever value for 80 that is necessary
if(Serial.available())
{
    static byte index = 0;
    char ch = Serial.read();
    if(ch == 10)  // ASCII for newline
    {
        myString[index] = 0;  // char array terminator
        index = 0;
        Serial.print(myString);
    }
    else
    {
        myString[index++] = ch;
    }
}

cja7928:
After studying both responses I realized I overlooked / didn't fully grasp the serial.available function.

1. In any data communication, there exists per-understanding (known as protocol) between the sender and receiver as to how many data bytes will be transferred, are they ASCII codes or binary codes, how the beginning of transmission be marked, how the end of transmission be marked, and etc.

2. Serial.available()
When a valid character is present in the receiver section of the UART Port, the MCU is immediately interrupted; the MCU goes to the relevant interrupt service routine (ISR), and it reads the data byte from the receiver section and saves it into a 64-byte wide FIFO type unseen Buffer of the Arduino Platform.

By executing if(Serial.available()>0){} structure, we can be ensured that there is at least one character present in the FIFO Buffer. By executing byte n = Serial.available(); instruction, we can be known about the number of characters that have been (so far) accumulated in the buffer.

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. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The examples store the incoming message in a cstring.

...R

GolamMostafa:
1. In any data communication, there exists per-understanding (known as protocol) between the sender and receiver as to how many data bytes will be transferred, are they ASCII codes or binary codes, how the beginning of transmission be marked, how the end of transmission be marked, and etc.

2. Serial.available()
When a valid character is present in the receiver section of the UART Port, the MCU is immediately interrupted; the MCU goes to the relevant interrupt service routine (ISR), and it reads the data byte from the receiver section and saves it into a 64-byte wide FIFO type unseen Buffer of the Arduino Platform.

By executing if(Serial.available()>0){} structure, we can be ensured that there is at least one character present in the FIFO Buffer. By executing byte n = Serial.available(); instruction, we can be known about the number of characters that have been (so far) accumulated in the buffer.

Thank you - that makes more sense now and explains exactly why my code wasn't working.

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. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The examples store the incoming message in a cstring.

...R

DKWatson:
You still need to NOT use the String class. You can assign a length to the array that will accommodate the largest anticipated input stream and simply change your code to

char myString[80] = {};  // use whatever value for 80 that is necessary

if(Serial.available())
{
   static byte index = 0;
   char ch = Serial.read();
   if(ch == 10)  // ASCII for newline
   {
       myString[index] = 0;  // char array terminator
       index = 0;
       Serial.print(myString);
   }
   else
   {
       myString[index++] = ch;
   }
}

thank you both. I will start working on changing to cstring right away. really appreciate the advice.