private void CNC_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (serialPort.BytesToRead > 0)
{
RxBuffer += serialPort.ReadExisting().Replace("\r", "");
if (RxBuffer.Contains("\n"))
{
string[] lines = RxBuffer.Split(new char[] { '\n' });
int iLines = lines.Length;
if (RxBuffer.EndsWith("\n"))
{
RxBuffer = string.Empty;
}
else if (iLines >= 1)
{
RxBuffer = lines[iLines - 1];
iLines--;
}
else
{
RxBuffer = string.Empty;
}
for (int i = 0; i < iLines; i++)
{
string line = lines[i];
while (line.IndexOf(" ") >= 0)
line = line.Replace(" ", " ");
line = line.Trim();
if (!string.IsNullOrEmpty(line))
{
if (line.StartsWith("[", StringComparison.InvariantCultureIgnoreCase)
|| line.StartsWith("tinyg", StringComparison.InvariantCultureIgnoreCase))
{
while (Responses.Count >= 1000)
for (int j = 0; j < 100; j++)
Responses.Dequeue();
Responses.Enqueue(line);
}
else if (line.StartsWith("{", StringComparison.InvariantCultureIgnoreCase))
{
// JSON response string
}
else
GPlotAppend(line);
LogAppend(line);
}
}
}
}
}