I'm writing an application in C that uses the serial port for communication with a Bluetooth device. I've found I can close the com port, power cycle the Bluetooth device, and reopen the port to find stale data sitting in the read buffer from a previous connection. I need to flush the I/O buffer. The solution would seem to be to use
tcflush(fd, TCIOFLUSH)
which, according to OS X's man page for tcflush, should
Flush both data received but not read and data written but not transmitted.
However, that seems to be ineffective.
According to Unix: How to clear the serial port I/O buffer? the solution on Unix is to use
ioctl(fd, TCFLUSH, 2)
But TCFLUSH
isn't defined on Mac and I'm not sure what the equivalent is.