title: Serial communications with a USB to Serial converter on Ubuntu
body: <p>I have an FTDI chip based USB to serial converted that I will at one point try to communicate to a TV with via RS232.  I am running Ubuntu Maverick.</p>
  
  <p>I'm new to serial communications and can't figure out why I can't simply read bytes from the port.</p>
  
  <p>A serial loopback test was successful.  I shorted Tx and Rx together and ran the following C program and my keyboard was echoed on the screen.</p>
  
  <pre><code>#include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;termios.h&gt;
  
  int main(int argc,char** argv)
  {
      struct termios tio;
      struct termios stdio;
      int tty_fd;
      fd_set rdset;
  
      unsigned char c='D';
  
      printf("Please start with %s /dev/ttyS1 (for example)\n",argv[0]);
      memset(&amp;stdio,0,sizeof(stdio));
      stdio.c_iflag=0;
      stdio.c_oflag=0;
      stdio.c_cflag=0;
      stdio.c_lflag=0;
      stdio.c_cc[VMIN]=1;
      stdio.c_cc[VTIME]=0;
      tcsetattr(STDOUT_FILENO,TCSANOW,&amp;stdio);
      tcsetattr(STDOUT_FILENO,TCSAFLUSH,&amp;stdio);
      fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);       // make the reads non-blocking
  
  
  
  
      memset(&amp;tio,0,sizeof(tio));
      tio.c_iflag=0;
      tio.c_oflag=0;
      tio.c_cflag=CS8|CREAD|CLOCAL;           // 8n1, see termios.h for more information
      tio.c_lflag=0;
      tio.c_cc[VMIN]=1;
      tio.c_cc[VTIME]=5;
  
      tty_fd=open(argv[1], O_RDWR | O_NONBLOCK);
      cfsetospeed(&amp;tio,B115200);            // 115200 baud
      cfsetispeed(&amp;tio,B115200);            // 115200 baud
  
      tcsetattr(tty_fd,TCSANOW,&amp;tio);
      while (c!='q')
      {
              if (read(tty_fd,&amp;c,1)&gt;0)        write(STDOUT_FILENO,&amp;c,1);              // if new data is available on the serial port, print it out
              if (read(STDIN_FILENO,&amp;c,1)&gt;0)
              {
                write(tty_fd,&amp;c,1);                     // if new data is available on the console, send it to the serial port
              }
      }
  
      close(tty_fd);
  }
  </code></pre>
  
  <p>I then plugged the USB side into a USB (/dev/ttyUSB0) port on my PC and the 9-pin side of the cable to the serial port (/dev/ttyS0).</p>
  
  <p>I ran the previous program on /dev/ttyUSB0 and typed in another terminal window:</p>
  
  <pre><code>echo "Hello world!" &gt; /dev/ttyS0
  </code></pre>
  
  <p>Nothing appears.  I also tried running the program in two separate terminals on both /dev/ttyUSB0 and /dev/ttyS0 and I couldn't communicated from one to the other.</p>
  
  <p>Does anyone know what I'm doing wrong here?</p>
  
  <p>Thanks in advance!</p>