我有3个课程:
传感器:从串行端口连续读取数据并进行解析。解析数据后,将其添加到blockingCollection
DataProcessing:从blockingCollecion获取数据并进行处理。
DisplayDataForm:显示处理后的数据
如何确定DataProcessing连续运行但没有阻塞,并且何时有新数据到达并以表格形式显示对其进行处理?
class Sensor
{
public BlockingCollection<double[]> _bufferTimeSeriesData;
public void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
byte[] buffer = new byte[_serialPort1.BytesToRead];
lock (buffer)
{
_serialPort1.Read(buffer, 0, buffer.Length);
double[] samplesAtTimeT;
for (int i = 0; i < buffer.Length; i++)
{
samplesAtTimeT = _BinaryStreamConverter.interpretBinaryStream(buffer[i]);
if (samplesAtTimeT != null)
_bufferTimeSeriesData.Add(samplesAtTimeT); //This is the BlockingCollection
}
_bufferTimeSeriesData.CompleteAdding();
}
}
}
class DataProcessing
{
public void ProcessData()
{
Task.Run(() =>
{
while (Globals.Sensor._bufferTimeSeriesData.IsCompleted)
{
double[] samplesAtTimeT = Globals.Sensor._bufferTimeSeriesData.Take();
//Process data
// writes data to file (is it correct to do it here?)
// Send processed data to form to plot in a chart. HOW??
}
}
}
}
class DisplayForm:Form
{
public void DisplayDataInForm(double[] samples)
{
chartCh1.Series["Signal"].Points.Add(samples[1]);
chartCh2.Series["Signal"].Points.Add(samples[2]);
while (chartCh1.Series["Signal"].Points.Count > 1000)
{
chartCh1.Series["Signal"].Points.Remove(chartCh1.Series["Signal"].Points.First());
chartCh2.Series["Signal"].Points.Remove(chartCh2.Series["Signal"].Points.First());
}
}
}