How to synchronize SerialPort operations?
<p>I have an application that communicates with a microcontroller via serial port. I want to check the controller's status periodically in a Background Worker, and also allow the user to interact with the controller asynchronously (via user interface), by sending commands and receiving responses.</p>

<p>The UI and Background Worker use the SerialCommunication static class:</p>

<pre><code>static class SerialCommunication
{
    static SerialPort serialPort;
    static int readWriteTimeout = 1000; // [ms]
    static int waitForTransmissionTimeout = 2; // [s]
    static string rxData = "", endOfString = "" + char.MinValue;
    static bool WaitingForSerialData = false; // Set in SerialWrite(), cleared in SerialRead()

    [...]
    public static string SerialRead()
    {
        try
        {
            rxData = serialPort.ReadTo(endOfString);
        }
        catch (TimeoutException)
        {
            WaitingForSerialData = false;
            throw new Exception(Properties.Resources.serial_read_timeout);
        }
        WaitingForSerialData = false;
        return rxData;
    }

    public static void SerialWrite(string text)
    {
        DateTime start = DateTime.Now;
        while (WaitingForSerialData) // Avoids the situation in which a command executed on a thread receives the response for the command executed from a different thread
        {
            if (!WaitingForSerialData)
            {
                try
                {
                    WaitingForSerialData = true; // All commands wait for confirmation/data, so it is normal to set this boolean value for every serial transmission
                    serialPort.Write(text);
                }
                catch (TimeoutException)
                {
                    throw new Exception(Properties.Resources.serial_write_timeout);
                }
            }
            else
            {
                System.Threading.Thread.Sleep(100);
                if ((DateTime.Now - start).Seconds &gt;= waitForTransmissionTimeout)
                {
                    throw new Exception("Timeout");
                }
            }
        }
    }
}
</code></pre>

<p>The serial port is initialized at application startup. <code>SerialWrite</code> and <code>SerialRead</code> are called in the UI or Background Worker consecutively.</p>

<p>I want to avoid situations in which a command receives the response from another command executed in a different thread. Currently, I've implemented waiting in SerialWrite for the command to be received (for SerialRead to finish) before sending the command, but I'm afraid that this can block the UI (for up to <code>waitForTransmissionTimeout</code> seconds), if SerialWrite is executed from there.</p>

<p><strong>So what is the best way to synchronize the SerialPort operations?</strong></p>