title: Proper way using timeouts with serial port and ReadAsync
body: <p>I have used serial port with its original methods like BytesToRead, Read and Write before. As it is said that these are unreliable and I had a lot busy waiting I am trying to switch to its asynchronous methods.</p>
  
  <p>I need to read from serial port with 2 different time outs. First timeout is between two messages (2000 ms here) and second is between 2 chars (10 ms here). Therefore I change ReadTimeout for each ReadAsync call like this:</p>
  
  <pre><code>    public int Read(byte[] buffer, int offset, int count, bool isFirstChar)
      {
          Stopwatch stopWatch = new Stopwatch();
          stopWatch.Start();
  
          try
          {
              m_serialPort.ReadTimeout = isFirstChar ? 2000 : 10;
              Task&lt;int&gt; task = m_serialPort.BaseStream.ReadAsync(buffer, offset, count);
              task.Wait();
              return task.Result;
          } catch (AggregateException err)
          {
              stopWatch.Stop();
              int cnt = 0;
              foreach (Exception e in err.InnerExceptions)
              {
                  if (e is TimeoutException)
                  {
                      Console.WriteLine(string.Format("Exception {0}: {1} Timeout: {2} ms", ++cnt, e.Message, stopWatch.ElapsedMilliseconds));
                  }
              }
  
              return -1;
          }
      }
  </code></pre>
  
  <p>Output when an AggregateException occurs and no bytes are received:</p>
  
  <blockquote>
    <p>Exception 1: The operation has timed-out. Timeout: 2014 ms<br>
    Exception 1: The operation has timed-out. Timeout: 2014 ms<br>
    Exception 1: The operation has timed-out. Timeout: 2014 ms<br>
    Exception 1: The operation has timed-out. Timeout: 2014 ms<br>
    Exception 1: The operation has timed-out. Timeout: 2014 ms<br>
    Exception 1: The operation has timed-out. Timeout: 3584 ms<br>
    Exception 1: The operation has timed-out. Timeout: 2014 ms<br>
    Exception 1: The operation has timed-out. Timeout: 2017 ms<br>
    Exception 1: The operation has timed-out. Timeout: 2012 ms<br>
    Exception 1: The operation has timed-out. Timeout: 2011 ms<br>
    Exception 1: The operation has timed-out. Timeout: 2016 ms<br>
    Exception 1: The operation has timed-out. Timeout: 2012 ms<br>
    Exception 1: The operation has timed-out. Timeout: 2011 ms<br>
    Exception 1: The operation has timed-out. Timeout: 2013 ms<br>
    Exception 1: The operation has timed-out. Timeout: 2013 ms  </p>
  </blockquote>
  
  <p>My questions are now: </p>
  
  <ul>
  <li><strong>Is this a proper way using ReadAsync method with .Wait()?</strong></li>
  <li><strong>Is it ok to change ReadTimeout everytime before the ReadAsync call?</strong></li>
  <li><strong>Should I use CancellationToken as parameter in ReadAsync instead, and if yes whats the best way of doing that?</strong></li>
  </ul>