protected void receivedData(string data)
{
if (NotifyData) {
SendMessage ("OnSerialData", data);
}
// Detect lines
if (NotifyLines || NotifyValues) {
// prepend pending buffer to received data and split by line
string [] lines = (BufferIn + data).Split ('\n');
// If last line is not empty, it means the line is not complete (new line did not arrive yet),
// We keep it in buffer for next data.
int nLines = lines.Length;
BufferIn = lines [nLines - 1];
// Loop until the penultimate line (don't use the last one: either it is empty or it has already been saved for later)
for (int iLine = 0; iLine < nLines - 1; iLine++) {
string line = lines [iLine];
//Debug.Log ("Received a line: " + line);
// skip first line
if (!FirstLineReceived) {
FirstLineReceived = true;
if (SkipFirstLine) {
if (EnableDebugInfos) {
Debug.Log("First line skipped: " + line);
}
continue;
}
}
// Buffer line
if (RememberLines > 0) {
linesIn.Add (line);
// trim lines buffer
int overflow = linesIn.Count - RememberLines;
if (overflow > 0) {
print ("Serial removing " + overflow + " lines from lines buffer. Either consume lines before they are lost or set RememberLines to 0.");
linesIn.RemoveRange (0, overflow);
}
}
// notify new line
if (NotifyLines) {
SendMessage ("OnSerialLine", line);
}
// Notify values
if (NotifyValues) {
string [] values = line.Split (ValuesSeparator);
SendMessage ("OnSerialValues", values);
}
}
}
}