title: wrong output with serial in C
body: <p>I am trying to send data over a serial line in C. I suspect my problem lies in the options I've set, but I can't figure it out. </p>
  
  <p>In the following program, I'm sending the bytes 0x79, 0x80, 0x81, but at the other end, I'm receiving 0x86, 0x06, 0x00</p>
  
  <pre><code>#include &lt;stdio.h&gt;   /* Standard input/output definitions */
  #include &lt;string.h&gt;  /* String function definitions */
  #include &lt;unistd.h&gt;  /* UNIX standard function definitions */
  #include &lt;fcntl.h&gt;   /* File control definitions */
  #include &lt;errno.h&gt;   /* Error number definitions */
  #include &lt;termios.h&gt; /* POSIX terminal control definitions */
  
  /*
   * 'open_port()' - Open serial port 1.
   *
   * Returns the file descriptor on success or -1 on error.
   */
  char SERIAL_DEVICE[] = {"/dev/ttyUSB0"};
  struct termios options;
  
  void open_port(int* fd)
  {
      *fd = open(SERIAL_DEVICE, O_RDWR | O_NOCTTY | O_NDELAY);
  
      if (*fd == -1){
          /* Could not open the port. */
          perror("open_port: Unable to open serial port - ");
  
      } else 
          fcntl(*fd, F_SETFL, 0);
  
  }
  
  void send(int message[], size_t bytes_to_send, int* fd){
      int n,i;
  
      for(i=0;i&lt;bytes_to_send;i++){
          printf("%x", message[i]);
      }
      n = write(*fd, message, bytes_to_send);
      if (n &lt; 0)
        fputs("write() failed!\n", stderr);
  }
  
  int main(){
  
      int fd; /* File descriptor for the port */
  
      int message[] = {0x79, 0x80, 0x81};
  
      // load the options structure with the cuurent port options
      tcgetattr(fd, &amp;options);        
      cfsetispeed(&amp;options, B19200); // set in baudrate
      cfsetospeed(&amp;options, B19200); // set out baudrate
      options.c_cflag |= (CLOCAL | CREAD);
  
      // No Parity 8N1
      options.c_cflag &amp;= ~PARENB;
      options.c_cflag &amp;= ~CSTOPB;
      options.c_cflag &amp;= ~CSIZE;
      options.c_cflag |= CS8;
      options.c_iflag &amp;= ~(IXON | IXOFF | IXANY);
      options.c_iflag |= (IGNPAR | ISTRIP);
  
      //raw input and output
      options.c_lflag &amp;= ~(ICANON | ECHO | ECHOE | ISIG);
      options.c_oflag &amp;= ~OPOST;
  
      // Flush buffers and set new options
      tcsetattr(fd, TCSAFLUSH, &amp;options);
  
      open_port(&amp;fd);
  
      int array_length = sizeof(message) / sizeof(message[0]);    
      int i;
      for(i=0;i&lt;array_length;i++){
          printf("%x", message[i]);
      }
  
      send(message, array_length, &amp;fd);
  
      close(fd);
      return 0;
  }
  </code></pre>