Thank you for posting all your code.
It is necessary to sleep a short while after opening the serial port, because the Arduino is busy resetting and waiting through the bootloader timeout. If you don't sleep about 1000 ms before sending the first command, it is usually simply lost, because your arduino program isn't running yet. So the arduino will wait forever for input that has already gone by. Which I believe is the symptom you have.
It doesn't look to me like this code is waiting before the first serial write.
-br