I'm trying to use char arrays for receiving serial data, and there is something I don't understand.
This code gives only a one as result (it's the last character of the incoming line)
char page;
char data;
long value;
void setup(){
Serial.begin(9600);
Serial1.begin(115200);
}
void loop(){
SerialCommunication();
}
void SerialCommunication(){
char recieved;
char currentLine[30];
char valueString[20];
char response[255];
byte charPos = 0;
while (Serial1.available()) {
recieved = Serial1.read();
if (recieved == '\n') {
Serial.println(currentLine);
charPos = 0;} // set char position of string to the beginning
else if (recieved!= '\r') { // if you got anything else but a carriage return character,
currentLine[charPos] = recieved; // add it to the end of the currentLine
charPos++; // and go to the next char posistion
// Serial.print("charPos = ");
// Serial.println(charPos);
currentLine[charPos] = '\0'; // null terminate the string
}
}
}
But when I uncomment the lines
Serial.print("charPos = ");
Serial.println(charPos);
I get the full line
charPos = 1
charPos = 2
charPos = 3
charPos = 4
charPos = 5
charPos = 6
charPos = 7
charPos = 8
charPos = 9
charPos = 10
charPos = 11
charPos = 12
charPos = 13
charPos = 14
charPos = 15
charPos = 16
charPos = 17
GET /AZ: HTTP/1.1
I hope someone can tell me what I might be doing wrong.