Thursday, 14 October 2010

Arduino code to read lines from serial input

Here is short and useful example to read lines from serial port in Arduino.

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void setup (){
  Serial.begin(9600);
  Serial.flush();
  digitalWrite (13, HIGH);      //turn on debugging LED
}

void loop (){

  int i=0;
  char commandbuffer[100];

  if(Serial.available()){
     delay(100);
     while( Serial.available() && i< 99) {
        commandbuffer[i++] = Serial.read();
     }
     commandbuffer[i++]='\0';
  }

  if(i>0)
     Serial.println((char*)commandbuffer);

}

2 comments:

  1. Thanks a lot, had quite a lot of difficulty sending over whole words instead of singel characters :)

    ReplyDelete