private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { string dataIn = serialPort.ReadLine(); string asciiConvertedBytes = string.Empty; asciiConvertedBytes = dataIn.Replace("\r", "").Replace("\n", ""); if (asciiConvertedBytes.Length == 52) //if data is valid { byte[] bytes = new byte[asciiConvertedBytes.Length / 4]; for (int i = 0; i < 13; ++i) //split each 4 bit nibble into array { bytes[i] = Convert.ToByte(asciiConvertedBytes.Substring(4 * i, 4).Reverse().ToString(), 2); } string diameterStringBuilder = string.Empty; for (int i = 5; i <= 10; i++) //diamter resides in array positions 5 to 10 { diameterStringBuilder += bytes[i].ToString(); } try //if anything fails, skip it and wait for the next serial event { //bytes[11] is the decmial position from right diameterStringBuilder = diameterStringBuilder.Insert(diameterStringBuilder.Length - bytes[11], "."); double diameter = 0; if (Double.TryParse(diameterStringBuilder, NumberStyles.Float, CultureInfo.InvariantCulture, out diameter)) //if it can convert to double, do it { string formatString = "0."; for (int i = 0; i < bytes[11]; i++) //format the string for number of decimal places { formatString += "0"; } DiameterChanged?.Invoke(diameter.ToString(formatString), null); } } catch (Exception oe) { MessageBox.Show(oe.Message); } } }
private void SerialService_DiameterChanged(object sender, EventArgs e) { //ActualDiameter = sender.ToString(); FilamentServiceVariables["ActualDiameter"] = sender.ToString(); if (captureStarted) { ZedGraphPlotModel.GetPlots().Select(x => { x.AddDataPoint(FilamentServiceVariables["ActualDiameter"]); return(x); }).ToList(); } DiameterChanged?.Invoke(sender, e); if (captureStarted) { UpdateHighsAndLows(); } }
private void RunSimulation() { Task.Factory.StartNew(() => { while (true) { double diameter = GetRandomNumber(1.7000, 1.8000, 4); string formatString = "0."; for (int i = 0; i < 4; i++) { formatString += "0"; } DiameterChanged?.Invoke(diameter.ToString(formatString), null); Thread.Sleep(50); } }); }