/// <summary> /// Event handler for receiving serial data /// </summary> /// <param name="sender">sender</param> /// <param name="e">event parameters</param> private void DataReceived(object sender, SerialDataReceivedEventArgs e) { // Read while there is data to read while (this.serialPort.BytesToRead > 0) { try { // We are reading line by line OregonData data = this.DecodeData(this.serialPort.ReadLine()); if (OregonDataRead != null && data != null) { OregonDataRead.Invoke(data); } } catch { } } }
/// <summary> /// Decode data from the arduino /// The data are as follow : /// I:XX;C:XX;T:xx.xx;B:xxx /// </summary> /// <param name="raw">raw string data</param> /// <returns>object data</returns> private OregonData DecodeData(string raw) { OregonData data = new OregonData(); data.Date = DateTime.Now; bool isOk = true; string[] rawSplit = raw.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (string set in rawSplit) { string[] setData = set.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); if (setData.Length == 2) { switch (setData[0]) { case "I": data.SensorId = setData[1]; break; case "C": int channel = 0; if (int.TryParse(setData[1], out channel)) { data.Channel = channel; } else { isOk = false; } break; case "T": decimal temp = 0; if (decimal.TryParse(setData[1], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out temp)) { data.Temperature = temp; } else { isOk = false; } break; case "B": decimal battery = 0; if (decimal.TryParse(setData[1], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out battery)) { data.BatteryLevel = battery; } break; } } } if (!isOk || !data.IsValid()) { return(null); } return(data); }