Can I do a timer loop and timeout for serial port communication in C#?
<p>I'm more familiar with C language and recently I've been ask to do C# for serial communication.
Below is my code for receiving data from COM port:</p>

<pre><code>public void RxData()
{
    int i = 0;
    int Data;
    bool StartRx = false;
    int timer;

    while (true)
    {
        Data = sp.ReadByte();
        if (Data == 0x01)
        {
            StartRx = true;
        }
        if (StartRx == true)
        {
            RxBuffer[i++] = Data;
        }
        if (Data == 0x04)
        {
            RxChkSum = RxBuffer[i - 2];
            break;
        }
        timer++;
        if(timer&gt;100) 
        {
            timer = 0;
            break;
        }
    } 
}
</code></pre>

<p>Above is the way I receive data starting with 0x01 and ends with 0x04.</p>

<p>I'm incrementing a timer to count til 100 and quit the while loop in case I don't receive any data. Some sort like a timeout.</p>

<p>But seems like the timer don't work. When I don't receive any data, my program just stuck in the while loop forever.</p>

<p>I know this is the way we write in embedded c programming. But is this the right way to write in C#?</p>
