I want read my serial port but only when data comes(I want not polling).
This is how I do it.
Schnittstelle = new SerialPort(\"COM3\"
you need to subscribe to the DataReceived event before opening the port, then listen to that event when triggered.
private void OpenSerialPort()
{
try
{
m_serialPort.DataReceived += SerialPortDataReceived;
m_serialPort.Open();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message + ex.StackTrace);
}
}
private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
{
var serialPort = (SerialPort)sender;
var data = serialPort.ReadExisting();
ProcessData(data);
}
Serial, when there is data in the buffer, the data received event is triggered, this doesn't mean that you got all your data at once. You may have to wait for number of times to get all of your data; this is where you need to process the received data separate, perhaps keep them on cache somewhere, before you do the final processing.