4

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.

Community
  • 1
  • 1
mekin
  • 99
  • 4
  • Would reading and discarding the remaining data work? – Eugene Sh. Oct 13 '15 at 17:57
  • @EugeneSh. Possibly, but how would I distinguish between old data that should be discarded and new data that shouldn't? Surely there must be a way to open a port and immediately flush the i/o buffer. Flushing it on close might also help. – mekin Oct 13 '15 at 18:14
  • Are you sure the old data is actually being buffered by the computer, and not the Bluetooth device? –  Oct 13 '15 at 20:53
  • @duskwuff Yes, I'm sure. I have additional diagnostics I can connect to the Bluetooth device to see what it is sending. – mekin Oct 13 '15 at 21:27

0 Answers0