title: How to read from serial port like picocom on Linux?
body: <p>I have a gps module that sends data (NMEA sentence) every 1 seconds to the serial port. I've been trying to read it from a c++ program.  </p>
  
  <p>When reading the serial port with picocom, data is displayed in a clean way, each line has a NMEA sentence). </p>
  
  <p><img src="https://i.stack.imgur.com/lNbuD.png" alt="Output from picocom"></p>
  
  <p>The result of my program is close but lines are sometimes mixed.</p>
  
  <p><img src="https://i.stack.imgur.com/XMrxp.png" alt="Output from my program"></p>
  
  <p>This is my code: </p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;fcntl.h&gt; 
  #include &lt;errno.h&gt; 
  #include &lt;termios.h&gt; 
  #include &lt;unistd.h&gt; 
  
  int main(){
  
      struct termios tty;
      memset(&amp;tty, 0, sizeof tty);
  
      int serial_port = open("/dev/ttyUSB0", O_RDWR);
  
      // Check for errors
      if (serial_port &lt; 0) {
          printf("Error %i from open: %s\n", errno, strerror(errno));
      }
  
          // Read in existing settings, and handle any error
      if(tcgetattr(serial_port, &amp;tty) != 0) {
          printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
      }
  
      tty.c_cflag &amp;= ~PARENB; // Clear parity bit, disabling parity (most common)
      tty.c_cflag &amp;= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)
      tty.c_cflag |= CS8; // 8 bits per byte (most common)
      tty.c_cflag &amp;= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
      tty.c_cflag |= CREAD | CLOCAL; // Turn on READ &amp; ignore ctrl lines (CLOCAL = 1)
      tty.c_lflag &amp;= ~ICANON;
      tty.c_lflag &amp;= ~ECHO; // Disable echo
      tty.c_lflag &amp;= ~ECHOE; // Disable erasure
      tty.c_lflag &amp;= ~ECHONL; // Disable new-line echo
      tty.c_lflag &amp;= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
      tty.c_iflag &amp;= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes
      tty.c_oflag &amp;= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
      tty.c_oflag &amp;= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
      tty.c_cc[VTIME] = 10;   
      tty.c_cc[VMIN] = 0;
      // Set in/out baud rate to be 9600
      cfsetispeed(&amp;tty, B9600);
      cfsetospeed(&amp;tty, B9600);
  
      // Save tty settings, also checking for error
      if (tcsetattr(serial_port, TCSANOW, &amp;tty) != 0) {
          printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
      }
  
      // Allocate memory for read buffer, set size according to your needs
      char read_buf [24];
      memset(&amp;read_buf, '\0', sizeof(read_buf));
  
      while(1){
          int n = read(serial_port, &amp;read_buf, sizeof(read_buf));
          std::cout &lt;&lt; read_buf ;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>How does picocom manage to display data correctly? Is is due to my buffer size or maybe <code>VTIME</code> and <code>VMIN</code> flags ?</p>