public static void Port_DataReceived(object sender, SerialDataReceivedEventArgs e) { while (Port.IsOpen && Port.BytesToRead > 0) { int i = Port.ReadByte(); if (i != '\r') { if (i != '\n') { Line[Index++] = (char)i; } } else { string line = new string(Line, 0, Index); foreach (GRBL.LineReceivedHandler d in OnLineReceived.GetInvocationList()) { d.Invoke(line); } Index = 0; } } }
private void ReadDataFromSerialPort() { lock (buffer) { int amount = serialPort.Read(staticBuffer, 0, MAXIMUM_BUFFER_SIZE); // var bytesReceived = serialPort.Read(rxBuffer, 0, e.Count); //Console.WriteLine($"Data amount: {amount}"); if (amount > 0) { for (var index = 0; index < amount; index++) { buffer += (char)staticBuffer[index]; } } var eolMarkerPosition = buffer.IndexOf(LINE_END); while (eolMarkerPosition >= 0) { var line = buffer.Substring(0, eolMarkerPosition); buffer = buffer.Substring(eolMarkerPosition + 2); eolMarkerPosition = buffer.IndexOf(LINE_END); // Console.WriteLine($"Line: {line}"); OnLineReceived?.Invoke(this, line); } } }
private void ReadLine() { while (_connected) { try { _lastLine = serial.ReadLine(); OnLineReceived?.Invoke(); } catch (TimeoutException) { } } }
/// <summary> /// Process the data from the serial port. /// </summary> private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e) { Console.WriteLine("SerialTextFile: SerialPortDataReceived"); if (e.EventType == SerialDataType.Chars) { lock (buffer) { int amount = ((SerialPort)sender).Read(staticBuffer, 0, MAXIMUM_BUFFER_SIZE); Console.WriteLine($"Data amount: {amount}"); if (amount > 0) { for (var index = 0; index < amount; index++) { buffer += (char)staticBuffer[index]; } } var eolMarkerPosition = buffer.IndexOf(LINE_END); Console.WriteLine($"eol: {eolMarkerPosition}"); Console.WriteLine($"Buffer: {buffer}"); while (eolMarkerPosition >= 0) { var line = buffer.Substring(0, eolMarkerPosition); buffer = buffer.Substring(eolMarkerPosition + 2); eolMarkerPosition = buffer.IndexOf(LINE_END); Console.WriteLine($"Line: {line}"); if (OnLineReceived != null) { Console.WriteLine("fire OnLineReceived"); } else { Console.WriteLine("null sir"); } OnLineReceived?.Invoke(this, line); } } } }