title: What is the best way to read and write data to a buffer for my bar code reader?
body: <p>I need to write a driver for a bar code reader for Linux in C. The bar code reader works through the serial bus. When I send a set of commands to the bar code reader, the bar code reader should return status messages to me. I managed to configure the port and to create a signal handler. In the signal handler, I read the data which the serial bus receives.</p>
  
  <p>So the question is: Should I read the data in buffer and then work with it? And can I even write the data in the buffer with the port configured in this way? When the device replies to me, according to that reply data, I need to send another command to the device. 
  Also, can I use <code>write()</code> to write the messages? If I can’t use that, what command should I use?
  And can you help me a little with the write  command?</p>
  
  <p>The command that I send to the device is always 7 bytes, but the reply data varies between 7-32 bytes. If the read function sends different number of bytes that are read, how can I be sure that I received all of the data, so I can work with it?</p>
  
  <p>Here is some code that I've written. Am I going in the right direction? 
  Once more, the idea is very simple: I am sending a command to a device, and the device interupt me and replies. I read what was send, I work with the reply data, and according to that data, I am send another command.
  Thanks in forward. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/signal.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;termios.h&gt;
  
  void signal_handler_IO (int status);   /* definition of signal handler */
  
  int n;
  int fd;
  int connected;
  char buffer[14];
  int bytes;
  struct termios termAttr;
  struct sigaction saio;
  
  int main(int argc, char *argv[])
  {
       fd = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY | O_NDELAY);
       if (fd == -1)
       {
          perror("open_port: Unable to open /dev/ttyO1\n");
          exit(1);
       }
  
       saio.sa_handler = signal_handler_IO;
       saio.sa_flags = 0;
       saio.sa_restorer = NULL; 
       sigaction(SIGIO,&amp;saio,NULL);
  
       fcntl(fd, F_SETFL, FNDELAY);
       fcntl(fd, F_SETOWN, getpid());
       fcntl(fd, F_SETFL,  O_ASYNC ); 
  
       tcgetattr(fd,&amp;termAttr);
       //baudRate = B115200; 
       cfsetispeed(&amp;termAttr,B115200);
       cfsetospeed(&amp;termAttr,B115200);
       termAttr.c_cflag &amp;= ~PARENB;
       termAttr.c_cflag &amp;= ~CSTOPB;
       termAttr.c_cflag &amp;= ~CSIZE;
       termAttr.c_cflag |= CS8;
       termAttr.c_cflag |= (CLOCAL | CREAD);
       termAttr.c_lflag &amp;= ~(ICANON | ECHO | ECHOE | ISIG);
       termAttr.c_iflag &amp;= ~(IXON | IXOFF | IXANY);
       termAttr.c_oflag &amp;= ~OPOST;
       tcsetattr(fd,TCSANOW,&amp;termAttr);
       printf("UART1 configured....\n");
  
       connected = 1;
       while(connected == 1){
           //write function and read data analyze(Processing)
       }
  
       close(fd);
       exit(0);             
  }
  
  void signal_handler_IO (int status)
  {
      bytes = read(fd, &amp;buffer, sizeof(buffer));
      printf("%s\n", buffer);
  }
  </code></pre>