Hi,
I'm trying to send recieve commands through serial.
Here is my code:
void setup(){
//Initialize location
//her.Position(LAT, LON);
//her.TimeZone(TIME_ZONE * 60);
//her.Sidereal(date_time, true);
Serial.begin(9600);
}
void loop(){
int index = 0;//the incoming message length
while (Serial.available()){//check for incoming message
delay(CMD_SIZE);// Each character does 1ms in order to transfer
cmd[index] = Serial.read();//Gets 1 character and decreases the buffer
delay(2);
//Serial.print(cmd[index], BYTE);
index++;
}
if (index > 0){
//Serial.print(index);
if (strcmp(cmd, ":GS#") == 0){//Get the Sidereal Time
Serial.print("13:25:54#");
}
if (strcmp(cmd, ":GVF#") == 0){//Get Version - CAUSE A PROBLEM
Serial.print("ETX Autostar#");
}
if (strcmp(cmd, ":GZ#") == 0){//Get telescope azimuth
Serial.print("150*45#T");
}
if (strcmp(cmd, ":GW#") == 0){//Get alignment status
Serial.print("AS2#");
}
Serial.flush();
}
}
My problem is in the second nested if statement. When sending the other commands, arduino response appropriately but when enters in the second nested if then I cannot recieve responses anymore. For some reason only 2 letters between : and # characters are working (i.e. :GW#, :GZ#, :GS#) but the :GVF# works only once and then arduino stops sending responses.
...
while (Serial.available()){//check for incoming message
delay(CMD_SIZE);// Each character does 1ms in order to transfer
cmd[index] = Serial.read();//Gets 1 character and decreases the buffer
delay(2); //Serial.print(cmd[index], BYTE);
index++;
}
cmd[index] = 0;
...
[quote author=Coding Badly link=topic=53312.msg380894#msg380894 date=1298495462]
...
while (Serial.available()){//check for incoming message
delay(CMD_SIZE);// Each character does 1ms in order to transfer
cmd[index] = Serial.read();//Gets 1 character and decreases the buffer
delay(2); //Serial.print(cmd[index], BYTE);
index++;
}
cmd[index] = 0;
...
[/quote]The last line would only set 0 to the last character. Do I need to set 0 all the array elements?
PaulS:
while (Serial.available()){//check for incoming message
delay(CMD_SIZE);// Each character does 1ms in order to transfer
If there is a character in the serial buffer, wait for it to get there. Makes (no) sense to me.
1ms at what baud rate? The time it takes to receive a character at 300 baud and at 115200 baud will be different.
while (Serial.available()){//check for incoming message
Serial.available() returns 0 if there is no data, and the body of the loop is skipped. It returns a positive value if there IS data in the buffer, and the body of the loop IS executed.
Once you KNOW that there is data to read, you stand around waiting for the character to arrive, and you want to know how long to wait?
That is like asking how long should I wait after the doorbell rings before I go answer the door, to be sure that the person that rang the doorbell has arrived.