title: How do I read data from serial port in Linux using C?
body: <p>I am new to serial programming in Linux using C. I have found a small piece of code to write data on serial port which I am sharing here. After running this code I may assume that data has written on a specific port. Now I would like to open another terminal and using separate code want to read the data written on that specific port - how do I do that?</p>
  
  <pre><code>#include &lt;stdio.h&gt;   /* Standard input/output definitions */
  #include &lt;string.h&gt;  /* String function definitions */
  #include &lt;unistd.h&gt;  /* UNIX standard function definitions */
  #include &lt;fcntl.h&gt;   /* File control definitions */
  #include &lt;errno.h&gt;   /* Error number definitions */
  #include &lt;termios.h&gt; /* POSIX terminal control definitions */
  
  /*
   * 'open_port()' - Open serial port 1.
   *
   * Returns the file descriptor on success or -1 on error.
   */
  int
  open_port(void)
  {
    int fd; /* File descriptor for the port */
  
    fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1)
    {
     /* Could not open the port. */
      perror("open_port: Unable to open /dev/ttyS0 - ");
    }
    else
      fcntl(fd, F_SETFL, 0);
  
    n = write(fd, "ATZ\r", 4);
    if (n &lt; 0)
      fputs("write() of 4 bytes failed!\n", stderr);
  
    return (fd);
  }
  </code></pre>
  
  <p>The code above will write the data on a specific port.</p>