Problem with serial.read()

Hi, I’m wondering if I send a string via the serial monitor to the arduino, it seems, that after the first character a line break is sent?

When I send the string “Hello” to the arduino, I get the following reply:

The entire line: H
The entire line: ello

Am I doing something wrong?

#include <WString.h>

char inData[10]; // Allocate some space for the string
int index = 0; // Index into array; where to store the character
boolean inputAvailable = false;
boolean cr = false;

void setup(){

      Serial.begin(9600);
        Serial.flush();
      }
      
void loop() 
{
        while(Serial.available() > 0 ){
            inputAvailable = true;
            int tmp = Serial.read();
            inData[index] = (char) tmp;
         
            index++;
 
        }
        inData[index] = '\0';  
        if(inputAvailable){
        Serial.print("The entire line: ");
        Serial.println(inData);
        memset((void*)&inData, 0, sizeof(int)*10); //the line is complete, now clear the contents of the inData array
        }
        inputAvailable = false;
       
}

The serial buffer is filled from one end, and emptied from the other.

The loop function is called in an infinite loop. The while loop you are executing reads all the serial data that has arrived since the last time the serial buffer was emptied. It does not wait for all the serial data that was sent to arrive. How could it? It has no idea when that will occur.

It is up to you to read until all the data has arrived. How would I do that, you might be asking. Well, it is important to control what is being sent.

If you send 'H','e','l','l','o','!', instead of 'H','e','l','l','o', you can read until the '!' has arrived, and then process all the data up to, but not including the !.

try this.

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

void loop() { Serial.println("Hello World!"); }

I was getting that too.
PaulS is correct, but let me elaborate a bit.

The “problem” is that the Arduino is in a very fast tight loop. It’s constantly looking for stuff coming in on the serial port. When the first character arrives, the Arduino starts processing it before the other characters arrive.

Remember that the serial monitor sends the characters after the carriage return is pressed, but the stream of bits are sent one by one. The Arduino has no way of knowing what’s coming down that pipe, or how long it may be.

As a matter of fact, if you started your code off with a large delay, during which you entered two words and hit return after each, both would be sitting in that buffer when you came out of the delay.
So if you sent: DogFood from Arduino’s point of view, it would look like: DogFood

The task of grouping meaningful data together when dealing with low level serial communication is up to the programmer.

An easy way of doing this is always end a stream of characters with a known character, say a period. When characters start to arrive, keep looking for more until you see the end.

Another way, if you’re just playing with the serial monitor and want to send stuff back and forth, then put a delay in your program after something is detected, then read it all in at once. The delay allows all the other bits to arrive. This places the burden of timing on the users and not the Arduino. An example of this:

/*
Created 25 Aug. 2010
by DVboy
*/

int inByte = 0; // incoming serial byte
char myCmd[128];
int inputSize=0;

void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
pinMode(2, INPUT); // digital sensor is on digital pin 2
Serial.println(“Ready:”);
}

void loop()
{
inputSize=0;
if (Serial.available() > 0) {
delay(300);
inputSize = Serial.available();
Serial.print(“inputSize=”);
Serial.println(inputSize);

for (int i = 0; i < inputSize; i++){
myCmd = Serial.read();

  • }*
  • // say what you got:*
  • Serial.print("I received: ");*
  • Serial.println(myCmd);*
  • }*
    }

The transfer of a byte needs around 9,6000/baud ms. The Problem of the OP is the gap between the arrival of two bytes, which is of that order, when characters are sent through a program, (rather than types in chracter by character)

so it might suffice to wait one byte time.

while(1) {
  if (!Serial.available()) {
    delay(9600/bauds+1);
    if (!Serial.available()) break;
  }
    // process byte
}