public MainWindow() { InitializeComponent(); this.DataContext = this; pltLeft.DataContext = plotLeft; plotLeft.AddDataPointToSeries(0, new DataPoint(0, 1)); pltRight.DataContext = plotRight; lData = new ConcurrentQueue <string>(); rData = new ConcurrentQueue <string>(); cts = new CancellationTokenSource(); xTime = new Stopwatch(); xTime.Start(); leftPort = SerialConnect("COM45"); if (leftPort.BytesToRead > 0) { string received = leftPort.ReadExisting(); } Task.Factory.StartNew(() => AddLeftData(lData, cts.Token)); leftPort.DataReceived += new SerialDataReceivedEventHandler(LeftDataReceivedHandler); rightPort = SerialConnect("COM47"); if (rightPort.BytesToRead > 0) { string received = rightPort.ReadExisting(); } Task.Factory.StartNew(() => AddRightData(rData, cts.Token)); rightPort.DataReceived += new SerialDataReceivedEventHandler(RightDataReceivedHandler); }
public void AddRightData(ConcurrentQueue <string> qData, CancellationToken token) { string local; while (true) { //token.ThrowIfCancellationRequested(); if (token.IsCancellationRequested) { break; } while (qData.TryDequeue(out local)) { String[] data = local.Split('#'); foreach (string point in data) { try { DataPoint dp = new DataPoint(xTime.ElapsedMilliseconds, Convert.ToDouble(point)); plotRight.AddDataPointToSeries(0, dp); } catch (Exception ex) { } } } Thread.Sleep(1); } }
public void AddData(ConcurrentQueue <string> qData, CancellationToken token, PlotViewModel plot) { string local; while (true) { if (token.IsCancellationRequested) { break; } while (qData.TryDequeue(out local)) { String[] data = local.Split(' '); string point = data[1]; try { DataPoint dp = new DataPoint(xTime.ElapsedMilliseconds, Convert.ToDouble(point)); plot.AddDataPointToSeries(0, dp); } catch (Exception ex) { } } } }