Concat serial

Hi Guys.

How Could I concat all datas received in serial port ? I'm sending byte by byte to serial and I would like concat that bytes and a string.

received = 1
received = 2
received = 3
received = 4
received = 5

String numbers = 12345;

Thank you.

Put the numbers into a character array as received. It would be better if you have a termination character to tell the program that all bytes are received. Many people use the line feed and/or carriage return characters as terminators. Maybe you will find this helpful.
serial lnput basic

I agree with groundfungus: make a data packet that is terminated with a known character that can be used as a sentinel to mark the end of the data packet. The newline character ('\n') is common. They you could used something like:

   char inBuffer[20];   // ...or however many characters you need for a packet
   int index;

   if (Serial.available() > 0) {
      index = Serial.readBytesUntil('\n', inBuffer, 19);   // need room for null
      inBuffer[index] = '\0';                              // It's now a string
      // whatever else you need to do

ok, but How could I showing the string ( inBuffer ) on the terminal ?

Using the code that econjack provided you can just print inBuffer.


That is because inBuffer is a null terminated character array. The null tells the print statement where the end of the data is. Without the null you would have to print an array element (character) at a time using a for loop.

The purpose of this line:

      inBuffer[index] = '\0';                              // It's now a string

is to create a null-terminated string. Because readBytesUntil() returns the number of characters that have been read, you can use that value to position the null termination character. Therefore, goundfungus' Serial.println() will work just fine.

The below might be close to what you are looking for. Will your data ever have an end of packet marker?

//zoomkat 6-29-14 Simple serial echo test
//type or paste text in serial monitor and send

String readString;

void setup() {
  Serial.println("Simple serial echo test"); // so I can keep track of what is loaded

void loop() {

  while (Serial.available()) {
    char c =;  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character

  if (readString.length() >0) {
    //Serial.println(readString);  //so you can see the captured String 
    Serial.print(readString);  //so you can see the captured String 

ok, thank you so much