You can try this approach instead using the events of the
SerialPort
class.
Then there is no need to work in a loop.
Add this method to your main class
public delegate void UpdateTextBoxDelegate(string text);
private void UpdateTextBox(string text)
{
if (textBox1.InvokeRequired)
{
textBox1.BeginInvoke(new UpdateTextBoxDelegate(UpdateTextBox), text);
}
else
{
textBox1.Text = text;
}
}
Add this event to your main class.
this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);
Inside the event you add the code to set the value in the text box.
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (e.EventType == System.IO.Ports.SerialData.Chars)
{
UpdateTextBox(serialPort1.ReadLine());
or
UpdateTextBox(serialPort1.ReadExisting());
}
}