title: Serial data over UART gets corrupted
body: <p>I'm developing an application for a development board (Beagle Bone Black) that will send some data over <code>UART</code> peripheral. The developing board runs <code>Linux Kernel</code> (some Debian distribution, <code>3.8.x</code> Linux Kernel version).</p>
  
  <p>For sending and receiving data over <code>UART</code> I use the standard <code>UNIX</code> API: <code>open()</code>, <code>read()</code>, and <code>write()</code> family functions.</p>
  
  <p>For setting the communication parameters (<code>baud rate</code>, <code>stop/start bits</code>, <code>parity</code>, etc...) I use the <code>termios</code> structure (from <code>termios.h</code>).</p>
  
  <p>This is some relevant code sequence where I make the I/O settings:</p>
  
  <pre><code>fd_debug = open("output.out", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
  
  fd_write = open(port.c_str(), O_WRONLY | O_NOCTTY | O_SYNC);
  std::cout &lt;&lt; std::endl &lt;&lt; "I opened: " &lt;&lt; port;
  
  struct termios settings;
  tcgetattr(fd_write, &amp;settings);
  
  cfsetospeed(&amp;settings, B19200);         /* baud rate */
  settings.c_cflag &amp;= ~PARENB;            /* no parity */
  settings.c_cflag &amp;= ~CSTOPB;            /* 1 stop bit */
  settings.c_cflag &amp;= ~CSIZE;
  settings.c_cflag |= CS8 | CLOCAL;       /* 8 bits */
  settings.c_lflag = ICANON;              /* canonical mode */
  settings.c_oflag &amp;= ~OPOST;             /* raw output */
  
  tcsetattr(fd_write, TCSANOW, &amp;settings); /* apply the settings */
  tcflush(fd_write, TCOFLUSH);
  </code></pre>
  
  <p>There I opened two file descriptors:</p>
  
  <ul>
  <li><code>fd_debug</code>: Linked to a file, for debugging purposes.</li>
  <li><code>fd_write</code>: Linked to the UART peripheral (<code>/dev/ttyO4</code> in my particular case).</li>
  </ul>
  
  <p>This is the function that is executed when I want to send one byte over <code>UART</code>:</p>
  
  <pre><code>int UARTIOHandler::write(uchar8 byte) {
      auto tp = std::chrono::steady_clock::now();
      std::cout &lt;&lt; std::endl &lt;&lt; "[write] Timestamp: " &lt;&lt; std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(tp.time_since_epoch()).count();
      ::write(fd_debug, &amp;byte, 1);
      return ::write(this-&gt;fd_write, &amp;byte, 1);
  }
  </code></pre>
  
  <p>For checking if the data I send is received correctly over <code>UART</code>, I have connected the <code>TX</code> and <code>RX</code> pins (loopback test) on my board (because I want to be able to receive back the data that I send), and run <code>minicom</code> on that particular <code>UART</code> port:</p>
  
  <pre><code>minicom -D /dev/ttyO4 -b 19200 -C test.test
  </code></pre>
  
  <p>After sending some data, I compared the two files (debugging file and the output file generated by <code>minicom</code> (which should contain the data received over <code>UART</code>)). The problem is that the data is not the same!</p>
  
  <p>This is the actually data sent (in hexadecimal):</p>
  
  <pre><code>55 33 02 04 06 08 0a 0c d5 55 0b 01 03 05 07 ef 55 3f 07 06 05 04 03 02 01 e3 55 16 01 02 03 04 05 06 07 08 db 55 3f 01 02 03 04 05 06 07 e3
  </code></pre>
  
  <p>This is the data received in the debug file (it's the same, so this confirms that there's some <code>UART</code> problem):</p>
  
  <pre><code>55 33 02 04 06 08 0a 0c d5 55 0b 01 03 05 07 ef 55 3f 07 06 05 04 03 02 01 e3 55 16 01 02 03 04 05 06 07 08 db 55 3f 01 02 03 04 05 06 07 e3
  </code></pre>
  
  <p>And this is the data received by <code>minicom</code> tool (that was set to listen to the same <code>UART</code> port, and the same settings (<code>baud</code>, <code>parity</code>, etc):</p>
  
  <pre><code>55 33 02 04 06 08 0a d5 55 01 03 4d 69 6e 69 63 6f 6d 32 2e 36 2e 31 07 ef 55 3f 07 06 4d 69 6e 69 63 6f 6d 32 2e 36 2e 31 04 03 02 01 e3 55 16 01 02 03 04 4d 69 6e 69 63 6f 6d 32 2e 36 2e 31 06 07 08 db 55 3f 01 02 03 04 4d 69 6e 69 63 6f 6d 32 2e 36 2e 31 06 07 e3
  </code></pre>
  
  <p>As can be observed from a point all data gets corrupted, and there's more bytes received.</p>
  
  <p>For checking the actual data from the output files I used <code>hexdump</code> like this:</p>
  
  <pre><code>hexdump -ve '1/1 "%.2x "' test.test
  </code></pre>
  
  <p>What could be the problem?</p>