Serial - Is possible to put a received data in an array of char ?

Good evening,
i need to read a char from serial and the compose an array of char.
What I see now that arduino received on char of time then I need to build an array of char for have the word.
below reported the simple sketch:

int incomingByte = 0;   // for incoming serial data

void setup() {
        Serial.begin(9600);  
        
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();
                Serial.print((char)incomingByte);
              }
}

I think is possible use that :

stream.readBytesUntil(character, buffer, length)

but didn't know ho to use it,

Could you give a simple sketch for build an array from incoming data on serial ?

Thanks 1000000 for the SUPPORT
Gnusso

assuming you can write up the variable, array definitions, void setup() to go with this:

x=0;
void loop(){
if (done_receiving == 0){ // more data expected
if (Serial.available()>0){
incomingArray [x] = Serial.read();
   if (incomingArray [x] == done_character)
   { done_receiving = 1;
     x = 0;}
   else {x=x+1;}
} // end serial available 
} // end done check
} // end void loop

Good Morning, firstly I'd like to say thanks you for the support :slight_smile:

so I've tried to do in this way ...

int incomingByte = 0;   // for incoming serial data

void setup() {
        Serial.begin(9600);  
        
}
  int x = 0;
int done_receiving;
void loop(){
char incomingArray[128];
if (done_receiving == 0)
   { // more data expected
     if (Serial.available()>0){
          incomingArray[x] = Serial.read();
            if (incomingArray[x] == '\0')
               { 
                 done_receiving = 1;
                  x = 0;
                }
       else 
         {
           x=x+1;
         }
         Serial.println(incomingArray);
         incomingArray[0] = '\0';
} // end serial available 

} // end done check
} // end void loop

but when I open the arduino monitor and try to type " test " my expectation is at the end to have printed "test" again but I received it:

testpippoʼßë¿Þ~÷¡ï¼w÷_øu¿Íùxê?¯}ïϧ?kî÷õ«Ñ?×þ¿öõù?¤?xßÿ7^?ºÖtÞxO²ÿ)?m?~m¹_5?<rïë;ß½jw<ÿí<¸Gëï?o~oÿ¨ï??ߐ×7VV
testpippoʼßë¿Þ~÷¡ï¼w÷_øu¿Íùxê?¯}ïϧ?kî÷õ«Ñ?×þ¿öõù?¤?xßÿ7^?ºÖtÞxO²ÿ)?m?~m¹_5?<rïë;ß½jw<ÿí<¸Gëï?o~oÿ¨ï??ߐ×7VV
testpippoʼßë¿Þ~÷¡ï¼w÷_øu¿Íùxê?¯}ïϧ?kî÷õ«Ñ?×þ¿öõù?¤?xßÿ7^?ºÖtÞxO²ÿ)?m?~m¹_5?<rïë;ß½jw<ÿí<¸Gëï?o~oÿ¨ï??ߐ×7VV
testpippoʼßë¿Þ~÷¡ï¼w÷_øu¿Íùxê?¯}ïϧ?kî÷õ«Ñ?×þ¿öõù?¤?xßÿ7^?ºÖtÞxO²ÿ)?m?~m¹_5?<rïë;ß½jw<ÿí<¸Gëï?o~oÿ¨ï??ߐ×7VV

due the fact there is my word "test" and near there is pippo that another word typed before ...

where is my fault ? should be great to have only "test"

could exit a specific function or method to call ?

Thanks
Andrea

you need to declare char incomingArray[128]; outside loop() otherwise you get a new instance every time loop is called.

quick refactor, give it a try ( not tested)

int incomingByte = 0;   // for incoming serial data
int x = 0;
bool moreData = true;
char incomingArray[128];

void setup()
{
  Serial.begin(9600);  
}

void loop()
{
  if (moreData)
  { 
    if (Serial.available()>0)
    {
      incomingArray[x] = Serial.read();
      if (incomingArray[x] == '\0'  || x == 127 )  // how to send a 0 char ?  + added test array full !
      { 
        moreData = false;
        x = 0;
      }
      else 
      {
        x++;
      }
    }  
  } 
  else // no moreData
  {
      Serial.println(incomingArray);
      incomingArray[0] = '\0';
  } 
}

Hi I've tried to use the code suggest from you this is the reseult :

tdÌÕ»å±Ë¿Ê¼ßë¿Þ~w¡ï<w×_øt¿ÍÙxê?­}ïϧ?kö÷ý«Ñ¾×þ?öõù?´?|ßÿ7^?ºÖuÚxO¶ÿ)?m¿o¹ß5?<rïË;ß½jw>ÿí<¸Gëï?o~oÿ¨ï?ß	ëÚ.ï~m§cÿU
teÌÕ»å±Ë¿Ê¼ßë¿Þ~w¡ï<w×_øt¿ÍÙxê?­}ïϧ?kö÷ý«Ñ¾×þ?öõù?´?|ßÿ7^?ºÖuÚxO¶ÿ)?m¿o¹ß5?<rïË;ß½jw>ÿí<¸Gëï?o~oÿ¨ï?ß	ëÚ.ï~m§cÿU
tesÕ»å±Ë¿Ê¼ßë¿Þ~w¡ï<w×_øt¿ÍÙxê?­}ïϧ?kö÷ý«Ñ¾×þ?öõù?´?|ßÿ7^?ºÖuÚxO¶ÿ)?m¿o¹ß5?<rïË;ß½jw>ÿí<¸Gëï?o~oÿ¨ï?ß	ëÚ.ï~m§cÿU
test»å±Ë¿Ê¼ßë¿Þ~w¡ï<w×_øt¿ÍÙxê?­}ïϧ?kö÷ý«Ñ¾×þ?öõù?´?|ßÿ7^?ºÖuÚxO¶ÿ)?m¿o¹ß5?<rïË;ß½jw>ÿí<¸Gëï?o~oÿ¨ï?ß	ëÚ.ï~m§cÿU

due the fact I think each time print the array + a protocol maybe ... :frowning:

I'm looking for have the clean up word without success...

any suggestion =?

Thanks
Andrea

retry

int incomingByte = 0;   // for incoming serial data
int x = 0;
bool moreData = true;
char incomingArray[128];

void setup()
{
  Serial.begin(9600);  
}

void loop()
{
  if (moreData)
  { 
    if (Serial.available()>0)
    {
      incomingArray[x] = Serial.read();

      incomingArray[x+1] = '\0'; // keep string closed with \0

      if (incomingArray[x] == '\0'  || x == 127 )  // how to send a 0 char ?  + added test array full !
      { 
        moreData = false;
        x = 0;
      }
      else 
      {
        x++;
      }
    }  
  } 
  else // no moreData
  {
      Serial.println(incomingArray);
      incomingArray[0] = '\0';
      moreData=true;
  } 
}
          incomingArray[x] = Serial.read();
            if (incomingArray[x] == '\0')

If the sender is sending ASCII data, incomingArray[ x ] will NEVER be NULL. The NULL is a trigger to the sender to stop sending. The NULL is not sent.

i need to read a char from serial and the compose an array of char.

You may want to just capture the individual chacters and form them into a string, which is a fairly simple operation. Below is a very simple example where a string is sent from the serial monitor and captured by the arduino, then sent back to the serial monitor.

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    readString="";
  } 
}

Good Afternoon Zoomkat,
I've tested the example and it's working ... so now the issues is,

this string need to be send out via virtual wire but virtual wire use only a array of char and not STRING.

For this reason I was thinking to an array of char , now with the string is necessary to cicle it and fill the char array ?

Maybe something like that ?

if (readString.length() >0) {
    Serial.println(readString);
     for (int i=0; i <= readString.length() ; i++)
       {
          messaggio[i] = readString[i];
       } 
   
    Serial.println(messaggio);
    vw_send((uint8_t *)messaggio, strlen(messaggio));
    vw_wait_tx(); // Wait until the whole message is gone
    messaggio[0]='\0';
    readString="";
  }

but in this way I add another passage ... the best should be do this in one shot ...

Thanks
Andrea