/// <summary> /// Import a CSV file based on a <see cref="StreamReader"/> /// </summary> /// <param name="_stream">The stream to import</param> public void ImportStream(TextReader _stream) { m_columnNames = null; m_data = null; if (ColumnNameSpaceReplacement == null) { ColumnNameSpaceReplacement = ""; } var header = _stream.ReadLine(); // Header must be the first line in the stream if (header != null) { var str = new StringBuilder(header); OnHeaderRead?.Invoke(str); ProcessHeader(str); string line; while ((line = _stream.ReadLine()) != null) { str.Length = 0; str.Append(line); OnLineRead?.Invoke(str); ProcessLine(str); } } }
public void Start(string comPort, int baudRate, int batchSize) { DateTime startTime = DateTime.Now; _batchSize = batchSize; if (_port == null) { _port = new SerialPort(comPort, baudRate); _port.DataReceived += (sender, e) => { //string line = _port.ReadExisting().Trim(); string line = _port.ReadLine().Trim(); if (line.StartsWith("R[")) { double r = 0, ir = 0, g = 0, x = 0, y = 0, z = 0; //R[972] IR[709] G[211] //-?[0-9]\d*(\.\d+)? Match match = Regex.Match(line, @"^R\[([0-9]+)\] IR\[([0-9]+)\] G\[([0-9]+)\] X\[(-?[0-9]\d*\.\d+?)\] Y\[(-?[0-9]\d*\.\d+?)\] Z\[(-?[0-9]\d*\.\d+?)\]"); if (match.Groups.Count == 7) { r = double.Parse(match.Groups[1].Value); ir = double.Parse(match.Groups[2].Value); g = double.Parse(match.Groups[3].Value); x = double.Parse(match.Groups[4].Value); y = double.Parse(match.Groups[5].Value); z = double.Parse(match.Groups[6].Value); double mag = Math.Sqrt(x * x + y * y + z * z) - 9.28; rs.Add(r); irs.Add(ir); gs.Add(g); mags.Add(mag); DateTime now = DateTime.Now; double timeStamp = (now - startTime).TotalSeconds; if ((now - _lastSend).TotalMilliseconds >= 50) { double rAverage = rs.Average(); double irAverage = irs.Average(); double gAverage = gs.Average(); double mAverage = mags.Average(); OnLineRead?.Invoke(rAverage, irAverage, gAverage, mAverage, timeStamp); irs.Clear(); rs.Clear(); gs.Clear(); mags.Clear(); } MeasureModel rModel = new MeasureModel { Time = timeStamp, Value = r, }; MeasureModel irModel = new MeasureModel { Time = timeStamp, Value = ir, }; MeasureModel gModel = new MeasureModel { Time = timeStamp, Value = g, }; MeasureModel mModel = new MeasureModel { Time = timeStamp, Value = mag, }; rBatch.Add(rModel); irBatch.Add(irModel); gBatch.Add(gModel); mBatch.Add(mModel); OnEveryLine?.Invoke(rModel, irModel, gModel, mModel, now); if (irBatch.Count >= _batchSize) { OnBatchCompleted?.Invoke(rBatch, irBatch, gBatch, mBatch); rBatch.Clear(); irBatch.Clear(); gBatch.Clear(); mBatch.Clear(); } _lastSend = now; } } }; _port.Open(); } }