title: how to read /dev/ttyUSB0 device in Linux?
body: <p>I have connected a USB RFID in serial port and use to read RFID tags. So I need to read the data from the device and process the output.
  I am getting the data with screen command from Linux command line but I was not able to take the value from screen and cannot pass to my application.
  Is there any other way to read from /dev/ttyUSB0?
  I have used the code shown below but it shows currently the resource is unavailable (even though I have given chmod permissions)</p>
  
  <pre><code>#include &lt;stdio.h&gt;      // standard input / output functions
  #include &lt;stdlib.h&gt;
  #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
  
  int main()
  {
      /* Open File Descriptor */
      int USB = open( "/dev/ttyUSB0", O_RDWR| O_NONBLOCK | O_NDELAY );
  
      /* Error Handling */
      if ( USB &lt; 0 )
      {
          //cout &lt;&lt; "Error " &lt;&lt; errno &lt;&lt; " opening " &lt;&lt; "/dev/ttyUSB0" &lt;&lt; ": " &lt;&lt; strerror (errno) &lt;&lt; endl;
          perror("USB ");
      }
  
      /* *** Configure Port *** */
      struct termios tty;
      memset (&amp;tty, 0, sizeof tty);
  
      /* Error Handling */
      if ( tcgetattr ( USB, &amp;tty ) != 0 )
      {
          //cout &lt;&lt; "Error " &lt;&lt; errno &lt;&lt; " from tcgetattr: " &lt;&lt; strerror(errno) &lt;&lt; endl;
          perror("tcgerattr ");
      }
  
      /* Set Baud Rate */
      cfsetospeed (&amp;tty, B9600);
      cfsetispeed (&amp;tty, B9600);
  
      /* Setting other Port Stuff */
      tty.c_cflag     &amp;=  ~PARENB;        // Make 8n1
      tty.c_cflag     &amp;=  ~CSTOPB;
      tty.c_cflag     &amp;=  ~CSIZE;
      tty.c_cflag     |=  CS8;
      tty.c_cflag     &amp;=  ~CRTSCTS;       // no flow control
      tty.c_lflag     =   0;          // no signaling chars, no echo, no canonical processing
      tty.c_oflag     =   0;                  // no remapping, no delays
      tty.c_cc[VMIN]      =   0;                  // read doesn't block
      tty.c_cc[VTIME]     =   5;                  // 0.5 seconds read timeout
  
      tty.c_cflag     |=  CREAD | CLOCAL;     // turn on READ &amp; ignore ctrl lines
      tty.c_iflag     &amp;=  ~(IXON | IXOFF | IXANY);// turn off s/w flow ctrl
      tty.c_lflag     &amp;=  ~(ICANON | ECHO | ECHOE | ISIG); // make raw
      tty.c_oflag     &amp;=  ~OPOST;              // make raw
  
      /* Flush Port, then applies attributes */
      tcflush( USB, TCIFLUSH );
  
      if ( tcsetattr ( USB, TCSANOW, &amp;tty ) != 0)
      {
          //cout &lt;&lt; "Error " &lt;&lt; errno &lt;&lt; " from tcsetattr" &lt;&lt; endl;
      }
  
      /* *** WRITE *** */
  
      unsigned char cmd[] = {'I', 'N', 'I', 'T', ' ', '\r', '\0'};
      //int n_written = write( USB, cmd, sizeof(cmd) -1 );
  
      /* Allocate memory for read buffer */
      char buf [256];
      memset (&amp;buf, '\0', sizeof buf);
  
      /* *** READ *** */
      int n = read( USB, &amp;buf , sizeof buf );
  
      /* Error Handling */
      if (n &lt; 0)
      {
          //cout &lt;&lt; "Error reading: " &lt;&lt; strerror(errno) &lt;&lt; endl;
          perror("read error ");
      }
  
      /* Print what I read... */
      //cout &lt;&lt; "Read: " &lt;&lt; buf &lt;&lt; endl;
      printf("%s",buf);;
  
      close(USB);
  }
  </code></pre>