title: How do I force a serial port write method to wait for the line to clear before sending its data?
body: <p>Here's some background on what I'm trying to do:</p>
  
  <ol>
  <li>Open a serial port from a mobile device to a Bluetooth printer.</li>
  <li>Send an EPL/2 form to the Bluetooth printer, so that it understands how to treat the data it is about to receive.</li>
  <li>Once the form has been received, send some data to the printer which will be printed on label stock.</li>
  <li>Repeat step 3 as many times as necessary for each label to be printed.</li>
  </ol>
  
  <p>Step 2 only happens the first time, since the form does not need to precede each label. My issue is that when I send the form, if I send the label data too quickly it will not print. Sometimes I get "Bluetooth Failure: Radio Non-Operational" printed on the label instead of the data I sent.</p>
  
  <p>I have found a way around the issue by doing the following:</p>
  
  <pre><code>for (int attempt = 0; attempt &lt; 3; attempt++)
  {
      try
      {
          serialPort.Write(labelData);
          break;
      }
      catch (TimeoutException ex)
      {
          // Log info or display info based on ex.Message
          Thread.Sleep(3000);
      }
  }
  </code></pre>
  
  <p>So basically, I can catch a TimeoutException and retry the write method after waiting a certain amount of time (three seconds seems to work all the time, but any less and it seems to throw the exception every attempt). After three attempts I just assume the serial port has something wrong and let the user know.</p>
  
  <p>This way seems to work ok, but I'm sure there's a better way to handle this. There are a few properties in the SerialPort class that I think I need to use, but I can't really find any good documentation or examples of how to use them. I've tried playing around with some of the properties, but none of them seem to do what I'm trying to achieve. </p>
  
  <p>Here's a list of the properties I have played with:</p>
  
  <ul>
  <li>CDHolding </li>
  <li>CtsHolding</li>
  <li>DsrHolding</li>
  <li>DtrEnable</li>
  <li>Handshake</li>
  <li>RtsEnable</li>
  </ul>
  
  <p>I'm sure some combination of these will handle what I'm trying to do more gracefully. </p>
  
  <p>I'm using C# (2.0 framework), a Zebra QL 220+ Bluetooth printer and a windows Mobile 6 handheld device, if that makes any difference for solutions.</p>
  
  <p>Any suggestions would be appreciated.</p>
  
  <p>[UPDATE]</p>
  
  <p>I should also note that the mobile device is using Bluetooth 2.0, whereas the printer is only at version 1.1. I'm assuming the speed difference is what's causing the printer to lag behind in receiving the data.</p>