- Serial.flush(). It's used to clean the Serial port. It's possible that I used it more than neccesary, but sometimes I get some garbage in Serial port (it could be for using '\t' or '\n').
- IDE 1.0 Linux.
Starting with 1.0, Serial.flush() blocks until the outgoing serial buffer is empty. How is that necessary for you?
Prior to 1.0, Serial.flush() did NOT "clean the Serial port". It discard random amounts of unread data in the incoming serial buffer that had not been read yet. It did nothing about data that had not yet arrived in the buffer. Throwing away random amounts of unread data was not useful then.
What it does now is even less useful. So, get rid of ALL the Serial.flush() calls.
- How long...? I don't have too much knowledge reading AT commands from MCU yet.
That has nothing to do with how long the XBee takes to enter command mode, or how long it remains in command mode if it does not receive a command.
You need to look at digi.com's site to find that answer.
AT Command Mode
To Enter AT Command Mode:
Default AT Command Mode Sequence (for transition to Command Mode):
• No characters sent for one second [GT (Guard Times) parameter = 0x3E8]
• Input three plus characters (“+++”) within one second [CC (Command Sequence Character)
parameter = 0x2B.]
• No characters sent for one second [GT (Guard Times) parameter = 0x3E8]
So, waiting 1.2 seconds after putting the XBee in command mode is going to find it back in relay mode.