title: Writing binary data over serial in windows
body: <p>I need to send binary data over a serial port, without any bytes getting reinterpreted as control characters along the way. I'm currently setting up my serial port as follows:</p>
  
  <pre><code>#include &lt;windows.h&gt;
  
  // open serial port
  HANDLE hSerial;
  hSerial = CreateFile ("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  
  // get serial parameters
  DCB dcbSerialParams = {0};
  dcbSerialParams.DCBlength = sizeof (dcbSerialParams);
  if (!GetCommState(hSerial, &amp;dcbSerialParams)) {
      cout &lt;&lt; "error getting state\n";
      exit(0);
  }
  
  // set serial params
  dcbSerialParams.BaudRate = CBR_115200;
  dcbSerialParams.ByteSize = 8;
  dcbSerialParams.StopBits = ONESTOPBIT;
  dcbSerialParams.Parity   = NOPARITY;
  if (!SetCommState (hSerial, &amp;dcbSerialParams)) {
      cout &lt;&lt; "error setting parameters\n";
      exit(0);
  }
  
  // set time outs
  COMMTIMEOUTS timeouts = {0};
  timeouts.ReadIntervalTimeout = 50;
  timeouts.ReadTotalTimeoutConstant = 10;
  timeouts.ReadTotalTimeoutMultiplier = 10;
  timeouts.WriteTotalTimeoutConstant = 10;
  timeouts.WriteTotalTimeoutMultiplier = 10;
  if (!SetCommTimeouts (hSerial, &amp;timeouts)) {
      cout &lt;&lt; "problem setting timeout values\n";
      exit(0);
  } else cout &lt;&lt; "timeouts set\n";
  </code></pre>
  
  <p>When I issue ReadFile commands, I can get and display bytes from 0 to 255 with no problem. but I'm having no such luck with WriteFile. Is there a way to explicitly set a binary write mode?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Ok, here's some more info. I have a windows machine and a linux single board computer hooked up through serial, the code above on the windows side is followed by:</p>
  
  <pre><code>unsigned char temp = 0;
  
  bool keepReading = true;
  while (keepReading) {
      DWORD dwBytesRead = 0;
      ReadFile (hSerial, &amp;temp, 1, &amp;dwBytesRead, NULL);
      if (1 == dwBytesRead) cout &lt;&lt; (unsigned int) temp &lt;&lt; " ";
      if (255 == temp) keepReading = false;
  }
  cout &lt;&lt; endl;
  
  bool keepWriting = true;
  char send = 0;
  while (keepWriting) {
      DWORD dwBytesWritten = 0;
      WriteFile (hSerial, &amp;send, 1, &amp;dwBytesWritten, NULL);
      send++;
      if (256 == send) keepWriting = false;
  }
  </code></pre>
  
  <p>My code on the linux side looks like this:</p>
  
  <pre><code>int fd = open("/dev/ttymxc0", O_RDWR | O_NOCTTY);
  struct termios options;
  bzero (options, sizeof(options));
  options.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  options.c_iflat = IGNPAR;
  options.c_oflag = 0;
  options.c_lflag = ICANON;
  options.c_cc[VMIN] = 1;
  options.c_CC[VTIME] = 0;
  tcflush (fd, TCIFLUSH);
  tcsetattr (fd, ICSANOW, &amp;options);
  
  bool keepWriting = true;
  char send = 0;
  while (keepWriting) {
      write (fd, &amp;send, 1);
      send++;
      if (256 == send) keepWriting = false;
  }
  
  bool keepReading = true;
  while (keepReading) {
      char temp = 0;
      int n = read (fd, &amp;temp, 1);
      if (-1 == n) {
          perror ("Read error");
          keepReading = false;
      } else if (1 == n) {
          cout &lt;&lt; temp &lt;&lt; " ";
      }
      if (256 == temp) keepReading = false;
  
  }
  cout &lt;&lt; endl;
  
  close(fd);
  </code></pre>
  
  <p>I start up the code on both machines, and the first set of while loops runs fine. The terminal on the windows side displays 0 through 255. Then it just sits there. If I output the number of bytes read on the linux side for the second set of while loops, it continually gives me 0 bytes. This would indicate a closed port normally, but I just sent a bunch of info through it so how could that be?</p>