I'm having a problem with my windows forms timer, which is supposed call a method that reads data from serial using Serial.ReadExisting(). I often get multiple calls from the timer when my com port is attempting to send data over, which breaks a single string into multiple strings. This bothers me, as I put a timestamp in front of the returning string and append it to a multiline textbox. Is there any way for the timer to wait until the method has finished reading the incoming data without having to delay/slow down my timer?
What I have so far:
private void loop_Tick(object sender, EventArgs e)
{
AddToTextBox(Program.SerialReadLine());
}
And
public static string SerialReadLine()
{
string read = _serialPort.ReadExisting();
return read;
}
You can disable the timer, do the call and enable it again.
private void loop_Tick(object sender, EventArgs e)
{
loop.Enabled = false;
try
{
AddToTextBox(Program.SerialReadLine());
}
finally
{
loop.Enabled = true;
}
}