Flushing on UART doesn't work as expected
<p>I need to write a sequence of values (buffer, ~<code>10bytes</code>) via <code>UART</code>.</p>

<p>This sequence needs to start with a <code>BREAK</code> delimiter, and in my case I need to decrease the baud rate to a lower value.</p>

<p>Details about my environment:</p>

<ul>
<li>Development board: BeagleBone Black.</li>
<li>Linux Kernel version:  3.8.13-bone70.</li>
<li>Serial driver used by the <code>tty</code> discipline: omap-serial.</li>
</ul>

<p>What I finally get is something like this:</p>

<pre><code>UARTIOHandler-&gt;setBaudRate(B9600);

unsigned char breakChar[] = { 0 };

UARTIOHandler-&gt;write(breakChar, 1);

UARTIOHandler-&gt;setBaudRate(B19200);

UARTIOHandler-&gt;write({1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
</code></pre>

<p>The <code>write</code> method is implemented this way:</p>

<pre><code>int UARTIOHandler::write(const std::initializer_list&lt;uchar8&gt; &amp;data) {
    uchar8 buffer[data.size()];

    int counter = 0;
    for(auto i : data) {
        buffer[counter++] = i;
    }

    auto output = ::write(this-&gt;fd_write, buffer, data.size());

    this-&gt;flush();

    return output;
}
</code></pre>

<p>And finally the <code>flush()</code> method:</p>

<pre><code>void UARTIOHandler::flush() {
    tcflush(this-&gt;fd_write, TCIOFLUSH);
}
</code></pre>

<p>The problem with this code is that the flushing doesn't always work, sometimes the distance between the <code>BREAK</code> and the first byte of data (observed on a scope) is <code>~500us</code> (which is fine for my application), and sometimes is up to <code>~3ms</code>.</p>

<p><em>EDIT:</em> This is the actual behavior:
For the first five seconds everything works fine (the distance between the <code>BREAK</code> and the rest of the message doesn't exceed <code>~1ms</code>), then, after five seconds there are some frames that exceed this inter-byte timing (for up to <code>~3ms</code>).
There's always the code that I posted which is executed, so there's no possible way that I somehow forget flushing the buffers.</p>

<p>Why do this variations happen?</p>

<p>I have searched for relevant problems and found <a href="https://stackoverflow.com/questions/13013387/clearing-the-serial-ports-buffer">this</a>, one workaround described there is to use a delay in front of the <code>tcflush(...)</code> function call. I can't use this method in my application because it will affect the functionality.</p>

<p>Another <a href="http://lkml.iu.edu//hypermail/linux/kernel/0707.3/1776.html" rel="nofollow noreferrer">comment</a> in that topic suggests that this was a bug in the Linux kernel, could this also be in my case?</p>