title: Distinguishing device disconnects from timeouts
body: <p>When using <a href="http://msdn.microsoft.com/en-us/library/aa365467%28v=vs.85%29.aspx" rel="nofollow"><code>ReadFile</code></a> on a communications resource it is possible to detect read timeouts by checking if less bytes than the requested amount of bytes were read. For example, when I want to read 1 byte, in case of a read timeout, 0 bytes are returned and the function succeeds.</p>
  
  <p>However, when I disconnect the device (after reading some bytes) from the COM-Port and start a read operation on its old handle, the same thing happens: Function succeeds and says that 0 bytes were returned.</p>
  
  <p>How can I distinguish between a device disconnect and a simple read timeout since no data is currently available?</p>
  
  <p>Currently, I'm checking if 0 bytes were returned, and if yes, I check if the device is still connected using WMI. However, this does not look very clean and I hope that I can find a better solution here.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Some more information about the device (as asked in comments):</p>
  
  <pre><code>dcb.fDtrControl = DTR_CONTROL_ENABLE;
  dcb.fRtsControl = RTS_CONTROL_ENABLE;
  </code></pre>
  
  <p>CTS and DSR are both set during data transfer and also after the device gets disconnected by unplugging the cable from the USB port.</p>