title: Waiting for Serial Port response between command calls
body: <p>I am working with an electrometer using RS-232(Serial) communication. It accepts one command at a time and sends back a response indicating if the command completed successfully or not.</p>
  
  <p>For demonstration only consider the following method...</p>
  
  <pre><code>    private void SetDevice()
      {
          sc.SetBias();
          sc.SetRange();
          sc.SetCollectionTime();
          sc.SetID();
      }
  </code></pre>
  
  <p>The method signature for <code>sc.SetID()</code> is as follows.</p>
  
  <pre><code>    public void SetID()
      {
          ComPort.Write("*IDN?");
      }
  </code></pre>
  
  <p>The above gives you an idea of what the other individual command calls are like. </p>
  
  <p>Since the <a href="https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived%28v=vs.110%29.aspx" rel="nofollow">DataReceived Event</a> indicates it is raised on a secondary thread how should I proceed with waiting for a response between each command call? Presently after <code>sc.SetBias()</code> is called <code>sc.SetRange()</code> fires without regards to if the previous command executed successfully. </p>
  
  <p>I can appreciate that by starting the <code>DataReceivedEvent</code> in a new thread it prevents my UI from locking but in this case when it is critical that the response gets paired with the appropriate command call I'm not sure of the best available option and would appreciate some guidance in my research. Thus far I'm considering getting a handle to the current UI thread and using <code>Thread.Sleep(sleepTime)</code> but without knowing exactly how reliable the response time is of the electrometer is this really best practice? And would this not need then to be called after every command sent?</p>