我正在从串行端口读取数据(数据以大块形式接收)并存储到缓冲区中。我想实时读取并处理它,而不会丢失数据。
计时器是实现它的最佳方法吗?如果计时器在前一个计时器完成之前过去了怎么办?等待吗?
private void Init()
{
timerData = new Systems.Timers.Timer;
timerData.Enabled = true;
timerData.Interval = 10;
timerData.Tick += new System.EventHandler(this.timerReadAndProcessData_Tick);
timerData.Start();
}
private void timerReadAndProcessData_Tick(object sender, EventArgs e)
{
while (_readAndProcesssDataFlag && Sensor.bufferHasSamples())
{
var data = Sensor.getFirstUnprocessedSample(); //buffer.dequeue() in serialPort class
var parsedData = parse(data);
for (int i=0; i<parsedData.Length; i++)
{
// Process samples here
// Write processed samples to file
}
}
}