title: init, read and write for linux serial device with C
body: <p>I'm working on a new project where I want to make a connection with an FTDI which is connected to my debian machine. I am intending to write the code with C, not C++. Here lies my problem. All the examples I find are incomplete or are made for a c++ compiler in stead of the GCC compiler. </p>
  
  <p>The goal is to talk to my microcontroller which is connected to the FTDI. For debugging i want to start building a linux application which is able to:</p>
  
  <ul>
  <li>initialize a serial connection on startup with ttyUSB1 </li>
  <li>send a character string</li>
  <li>display character strings when they are received by the pc</li>
  <li>save the communication to a .txt file</li>
  </ul>
  
  <p>Is there any example code or tutorial to make this?</p>
  
  <p>If I succeed I will defenetly place the code here so that new viewers can use it to!</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Like I said I would post the code if I had it, and this is what worked for me:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;termios.h&gt;
  
  #define MODEM "/dev/ttyUSB0"
  #define BAUDRATE B115200    
  
  int main(int argc,char** argv)
  {   
      struct termios tio;
      struct termios stdio;
      struct termios old_stdio;
      int tty_fd, flags;
      unsigned char c='D';
      tcgetattr(STDOUT_FILENO,&amp;old_stdio);
      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;
      if((tty_fd = open(MODEM , O_RDWR | O_NONBLOCK)) == -1){
          printf("Error while opening\n"); // Just if you want user interface error control
          return -1;
      }
      cfsetospeed(&amp;tio,BAUDRATE);    
      cfsetispeed(&amp;tio,BAUDRATE);            // baudrate is declarated above
      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
              printf("\n");
          }
          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 serial port
              printf("\n");
          }
      }
      close(tty_fd);
      tcsetattr(STDOUT_FILENO,TCSANOW,&amp;old_stdio);
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>Most of the code came from <a href="http://en.wikibooks.org/wiki/Serial_Programming/Serial_Linux" rel="nofollow">http://en.wikibooks.org/wiki/Serial_Programming/Serial_Linux</a> but i also used a bit from the code posted below.</p>