Go Down

Topic: Problem receiving string through the serial port (Read 751 times) previous topic - next topic

blacblu

Basically, what I'm trying to do is to make the Arduino send "Requesting: 1". Then I will type a number, 12345, for example. And then it should answer "Received: 12345". And now send "Requesting: 2"

But I can't manage to format the individual characters inside the string variable.

Code: [Select]

const char STOP_BYTE = '\n';
char buffer[20];
void setup(){
   Serial.begin(2400);
   delay(1000); 
}

void loop() {
  char DataReceived[15];
  static byte i=0;
  for (int counter=1;counter<6;counter++){
    Serial.print("Requesting: ");//standard request message
    Serial.println(counter);
    while(Serial.available()==0){}
    if (Serial.available() > 0) {
    char inByte = Serial.read();
    if (inByte==STOP_BYTE) { // We've received the end of our input
      buffer[i] = '\0'; // null terminate the string
      int data = atoi(buffer);
      // Do something with the data int
      i=0; // reset the index
    } else {
      buffer[i++] = inByte; // add character to buffer and increment index
    }
    Serial.print("Received: ");
    for(int x=1;x<16;x++){
      Serial.print(buffer[x]);
    }
    Serial.println("");
    }
  }
}

nickgammon

Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Go Up