title: write() to serial port fails when called twice
body: <p>I'm trying to write a char array to the serial port twice.  For some reason the second time I call write() it fails and I have no idea why.  The weird part is that the first write succeeds.</p>
  
  <p>Here's my write function:</p>
  
  <pre><code>void write_port()
  {
    unsigned char writeBuffer[5];
    int i;
    size_t len;
  
    writeBuffer[0] = 0x00;
    writeBuffer[1] = 0x01;
    writeBuffer[2] = 0x02;
    writeBuffer[3] = 0x03;
    writeBuffer[4] = 0x04;  
  
    len = sizeof(writeBuffer);
  
    for (i=0; i &lt; len; i++)
    {
      printf("Write: %x\n", writeBuffer[i]);
    }
  
    //First Write:
    int n = write(fd, writeBuffer, len);
    if (n &lt; 0)
      fputs("Serial Port Write Failed!\n", stderr);
    else
      printf("Wrote %0d bytes to serial port\n", n);
  
    //Second Write:
    n = write(fd, writeBuffer, len);
    if (n &lt; 0)
      fputs("Serial Port Write Failed!\n", stderr);
    else
      printf("Wrote %0d bytes to serial port\n", n);  
  }
  </code></pre>
  
  <p>Here's where I configure the port:</p>
  
  <pre><code>int open_port(void)
  {
    struct termios options;
  
    fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
  
    bzero(&amp;options, sizeof(options));   // set everything to 0 
  
    if (fd != -1)
    {
      printf("Serial Port Open\n");
  
      tcgetattr(fd, &amp;options_original);
      tcgetattr(fd, &amp;options);
      cfsetispeed(&amp;options, B460800);
      cfsetospeed(&amp;options, B460800);
      options.c_cflag |= (CLOCAL | CREAD); /* Enable the receiver and set local mode */
      options.c_cflag &amp;= ~ECHO; // Disable echoing of input characters
      options.c_cflag &amp;= ~ECHOE;
      options.c_cflag &amp;= ~PARENB; // parity disabled
      options.c_cflag &amp;= ~CSTOPB; // 1 stop bit
      options.c_cflag &amp;= ~CSIZE;  // Mask the character size bits
      options.c_cflag |=  CS8;    // 8 data bits    
      options.c_lflag &amp;= ~(ICANON | ECHO | ECHOE | ISIG);  /* Raw Input Mode */
  
      if (tcsetattr(fd, TCSANOW, &amp;options))    /* Set the new options for the port */
      {
        perror("could not set the serial settings!");
        return -1;
      }
    }
    else
    {
      /* Could not open the port */
      perror("open_port: Unable to open /dev/ttyUSB0 - ");
    }
  
    return (fd);
  }
  </code></pre>
  
  <p>Finally here's my console output.</p>
  
  <pre><code>Serial Port Open
  Flushing IO Buffers
  Write: 0
  Write: 1
  Write: 2
  Write: 3
  Write: 4
  Wrote 5 bytes to serial port
  Serial Port Write Failed!
  Serial Port Closed
  </code></pre>
  
  <p>So for some unknown reason I get -1 back from the second write() and have absolutely no idea why, what in the world could cause that?</p>