Problem sending data in api mode

You still haven't told us which version of the IDE you are using, nor have you told us why you need to flush the serial port. What do you understand the flush() to do?