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) { }
     }
 }
Beispiel #3
0
        /// <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);
                    }
                }
            }
        }