title: How do you check if a serial port is open in Linux?
body: <p>How does one check if a serial port is already open in Linux using Posix/C functions? I want to check the status of the serial port to check if the serial port is open or not.</p>
  
  <p>I would like to know what methods work for:</p>
  
  <ol>
  <li><p>Checking the <strong>file descriptor</strong> to see if the serial port is open and</p></li>
  <li><p>Checking the <strong>serial port file name</strong> to see if the the serial port is open which in the example below is "/dev/ttyUSB0"</p></li>
  </ol>
  
  <p>--</p>
  
  <pre><code>// This code is for example purposes only
  
  int open_port()
  {
    int fd;
  
    fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd &lt; 0)
    {
      perror("open_port: Unable to open /dev/ttyf1 - ");
    }
  
    return fd;
  } 
  </code></pre>
  
  <p>I imagine there is a "standard" way of doing this, and that is what I am trying to get to.</p>