Slightly strange problem I can't seem to explain probably because of my lack of knowledge of the architecture. Anyways, here goes.
I am reading long strings through the Serial port. When I read the first string and place it in an array, everything I sent gets received, no problem. However, second string I send, seems to pick up an extra character on the way and when I read back my character array, I get a leading "new line" character.
char input;
char cArray[80];
String commandString;struct g_code
{
char letter;
int numb;
float x_coord;
float y_coord;
float z_coord;
float i_value;
float j_value;
} command;void setup(){
//Setup Communications
Serial.begin(9600);
}void loop(){
//Check for Serial Commands
if(Serial.available()>0){
receiveCommand();
Serial.println("Received");
processString();
Serial.println(command.letter);
}}
void receiveCommand(){
int i=0;
do{
//Read Serial buffer
input = Serial.read();
if(input != -1) {
// Add to Array if not equal to -1.
// -1 means there is no data to be read over
// Serial right now.
cArray[ i] = input;
i++;
}
}while(input != '\r');
Serial.flush();
}void processString(){
for(int x=0; x<80; x++){
Serial.print(cArray[ x],DEC);
}
Serial.println();command.letter = cArray[0];
}
and these are my results over the Serial monitor, notice the leading 10 which is ASCII for new line.
Received
714850130000000000000000000000000000000000000000000000000000000000000000000000000000
G
Received
1071485013000000000000000000000000000000000000000000000000000000000000000000000000000
Any thoughts on this? I've noticed that if I add some delays ~50ms or more between the Serial.available and Serial.read functions, this doesn't seem to happen. Timing issue?