title: Sleep() is a bad design, but appears to be my only option
body: <p>I'm writing an IO class to upload/download files to a controller over RS-232 serial. Unfortunately, I cannot send a whole file all at once, I have to break it into packets and send it a little bit at a time. Here's the basic approach... </p>
  
  <pre><code>ifstream file ("path/to/file.ext", ios::in | ios::binary);
  
  while( !file.eof() )
  {
      //... zero buffer, and add packet header (8 bytes)
      size_t nResult = file.read( &amp;buffer[8], 129 );
      Serial.Write( buffer, nResult+8 );
      //... see if controller wrote anything to the serial port and process it's command
      Sleep( 600 );
  }
  </code></pre>
  
  <p>I know that using Sleep() is not a good design practice, but if I remove the Sleep() statement or even shorten the amount time the loop sleeps, then the controller throws errors about it's buffer being full, and the transfer fails. <strong>Is there a better way to do this?</strong></p>
  
  <p>Before you say it, no I cannot send a message to the controller to determine if it's ready for the next packet. It does not have that functionality. 
  <br />
  <br /></p>
  
  <p><strong>Edit:</strong>
  I forgot to mention that the interval at which I'm having to sleep is somewhat "blind." The protocol specification provided by the manufacturer does not detail any length of time needed between packets. So I had to determine that value by trial and error. I'm afraid that it may not work on every PC and more so that it may not work on every controller. </p>
  
  <p>This development is being done for Windows XP/Vista/7.</p>
  
  <p><strong>Edit #2:</strong>
  Also, the amount of data per packet is actually a trial and error guess as well. The protocol specification allows for packets of 65,535 bytes (including the header). But if you send more than 129 bytes at a time, you start seeing issues where sometimes it works and sometimes it doesn't. There also seems to be a relationship between the time you have to sleep and the amount of bytes you can send. If I drop the packet size down to 20 bytes per packet, I can drop the sleep time down to 400 milliseconds. I believe the reason for these issues stem from the time it takes to the controller to move data from its buffer to the file. </p>