Serial port communication throwing TimeoutException
<p>Following up with <a href="https://stackoverflow.com/questions/39294081/serial-port-communication-on-the-same-windows-machine-not-working">Serial Port Communication solution</a> I implemented the below design. My code uses <code>com8</code> to communicate with a <a href="http://www.darkwood.biz/" rel="nofollow noreferrer">Serial Port Utility Application</a> that is listening on <code>com9</code> within the same machine, then sending back (manually I type a message and press a button)</p>
<p>In my main I do this:</p>
<pre><code>MyClass MyObj = new MyClass();
var message = MyObj.SendThenRecieveDataViaSerialPort(&quot;Test&quot;);
</code></pre>
<p>And then in my class I have this:</p>
<pre><code>private static SerialPort MainSerialPort { get; set; } = new SerialPort();
private static string _ReceivedMessage;
private Thread readThread = new Thread(() =&gt; ReadSerialPort(ref _ReceivedMessage));

public string SendThenRecieveDataViaSerialPort(string _Message)
{
    MainSerialPort = new SerialPort(&quot;com8&quot;, 9600);
    MainSerialPort.ReadTimeout = 5000;
    MainSerialPort.WriteTimeout = 5000;
    MainSerialPort.Open();
    readThread.Start(); // 1

    try
    { // 2
        MainSerialPort.WriteLine(_Message); // 3
        readThread.Join(); // 6 - Console pops and waits
    }
    catch (TimeoutException ex)
    {
        Console.WriteLine(&quot;Exception in SendThenreceive&quot;);
    }

    return _ReceivedMessage;
}

private static void ReadSerialPort(ref string _message)
{
    try
    { // 4
        _message= MainSerialPort.ReadLine(); // 5 
    }
    catch (TimeoutException ex)
    {
        // 7 - when time outs
    }
}
</code></pre>
<p>However, it is throwing an error at step 7 saying:</p>
<blockquote>
<p>{&quot;The operation has timed out.&quot;}</p>
<p>InnerException: null</p>
</blockquote>
<p>Could you tell me where I am going wrong? Please and thanks.</p>