title: I want to receive data CONTINUOUSLY from a COM port & simultaneously want to write to file
body: <p>I want to read serial COM port and to write the data to a file in LINUX. Actually I'm sending data from hyperterminal from other PC.</p>
  
  <p>The problem is without while loop I can write only one line.</p>
  
  <p>But with while(1) loop I can't write anything to file.</p>
  
  <p>Or else I have to send BIG file, then application exits/terminates and writes to the file.</p>
  
  <p>My application should write the data (it may 2 lines or any thing); after that it has to wait for next data.</p>
  
  <p>So please help me out.....</p>
  
  <p>Here is my Code</p>
  
  <pre><code>=========================================
  
      #include &lt;termios.h&gt;
      #include &lt;stdio.h&gt;
      #include &lt;unistd.h&gt;
      #include &lt;fcntl.h&gt;
      #include &lt;sys/signal.h&gt;
      #include &lt;sys/types.h&gt;
      #include &lt;assert.h&gt;
      #include &lt;string.h&gt;
      #include &lt;time.h&gt;
  
  
  #define BAUDRATE B115200
  #define MODEMDEVICE "/dev/ttyS0"
  #define _POSIX_SOURCE 1 /* POSIX compliant source */
  #define FALSE 0
  #define TRUE 1
  
  volatile int STOP=FALSE;
  
  void signal_handler_IO (int status); /* definition of signal handler */
  int wait_flag=TRUE; /* TRUE while no signal received */
  struct timeval timeout;
  char n;
  fd_set rdfs;
  
  /*Some variables*/
  
  int num, offset = 0, bytes_expected = 255;
  
  main()
  {
  int fd,c, res,i;
  char In1;
  struct termios oldtio,newtio;
  struct sigaction saio; /* definition of signal action */
  char buf[255];
  FILE *fp;
  
  /* open the device to be non-blocking (read will return immediatly) */
  fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (fd == -1)
  {
  perror("open_port: Unable to open /dev/ttyS0 - ");
  return 1;
  }
  else
  {
  fcntl(fd, F_SETFL, 0);
  n = select(fd + 1, &amp;rdfs, NULL, NULL, &amp;timeout);
  }
  
  fp = fopen("/root/Desktop/gccAkash/OutputLOG.txt","a+");
  assert(fp != NULL);
  
  /* install the signal handler before making the device asynchronous */
  saio.sa_handler = signal_handler_IO;
  sigemptyset(&amp;saio.sa_mask);
  saio.sa_flags = 0;
  saio.sa_restorer = NULL;
  sigaction(SIGIO,&amp;saio,NULL);
  
  /* allow the process to receive SIGIO */
  fcntl(fd, F_SETOWN, getpid());
  fcntl(fd, F_SETFL, FASYNC);
  tcgetattr(fd,&amp;oldtio); /* save current port settings */
  
  /* set new port settings for canonical input processing */
  newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
  newtio.c_iflag = IGNPAR | ICRNL;
  newtio.c_oflag = 0;
  newtio.c_lflag = ICANON;
  newtio.c_cc[VMIN]=0; //it will wait for one byte at a time.
  newtio.c_cc[VTIME]=10; // it will wait for 0.1s at a time.
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd,TCSANOW,&amp;newtio);
  
  
  
  
  while (STOP==FALSE)
  {
  if (wait_flag==FALSE) //if input is available
  {
  do {
  res = read(fd,buf+offset,255);
  offset += res;
  } while (offset &lt; bytes_expected);
  
  if (offset!=0)
  {
  for (i=0; i&lt;offset; i++) //for all chars in string
  {
  In1 = buf[i];
  fputc ((int) In1, fp);
  printf("charecter:%c\n\r",In1);
  } //EOFor
  
  }//EOIf
  
  buf[res]=0;
  printf("Received Data is:%s\n\r",buf);
  if (res==0)
  STOP=TRUE; //stop loop if only a CR was input
  wait_flag = TRUE; // wait for new input
  }
  
  } //while stop==FALSE
  
  while(readchar)
  
  /* restore old port settings */
  tcsetattr(fd,TCSANOW,&amp;oldtio);
  close(fd);
  }
  
  /************************************************** *************************
  * signal handler. sets wait_flag to FALSE, to indicate above loop that *
  * characters have been received. *
  ************************************************** *************************/
  
  void signal_handler_IO (int status)
  {
  printf("received SIGIO signal.\n");
  wait_flag = FALSE;
  } 
  </code></pre>