I used a usleep function to read from serial port properly. I don't know why I did that. Probably function tcdrain doesn't work very well