private void ButtonCapture_Click(object sender, EventArgs e) { _spManager.SendData(StopCmd); _spManager.SendData(StopCmd); _spManager.Dispose(); _spManager.Dispose(); _spManager.Dispose(); _spManager.StartListening(); TextBoxRawData.Clear(); sendCmd(); }
private void ButtonStart_Click(object sender, EventArgs e) { try { if (Connected == false) { _spManager.StartListening(); sendCmd(); ComboboxCOM.Enabled = false; LabelConnect.Text = "ĐÃ KẾT NỐI THIẾT BỊ"; ButtonStartStop.Text = "NGẮT"; Connected = true; ButtonStartStop.Focus(); ButtonCapture.Enabled = true; ButtonSave.Enabled = true; } else { _spManager.SendData(StopCmd); _spManager.SendData(StopCmd); _spManager.SendData(StopCmd); _spManager.StopListening(); _spManager.Dispose(); _spManager.Dispose(); _spManager.Dispose(); TextBoxRawData.Clear(); ComboboxCOM.Enabled = true; LabelConnect.Text = "CHƯA KẾT NỐI"; ButtonStartStop.Text = "KẾT NỐI"; ButtonCapture.Text = "LẤY MẪU"; Connected = false; ButtonCapture.Enabled = false; ButtonSave.Enabled = false; } ButtonCapture.Focus(); } catch (Exception ex) { MetroMessageBox.Show(this, "\r\n\r\nKhông kết nối được với cổng COM :\r\n" + ex.Message, "Thử lại", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private void DrawScope() { if (!Controls.Contains(Scope)) { Scope.Location = new Point(30, 180); Scope.Size = new Size(900, 470); Controls.Add(Scope); } PlotModel Model = new PlotModel { Title = "", DefaultFont = afont, DefaultFontSize = fontsize, LegendTitle = "", PlotType = PlotType.XY, TitleFont = afont, Culture = System.Globalization.CultureInfo.CurrentCulture, LegendSymbolLength = 24, Padding = new OxyThickness(0, 20, 30, 0) }; Model.InvalidatePlot(true); Scope.Model = Model; LinearAxis YAxis = new LinearAxis { Position = AxisPosition.Left, Minimum = -20, Maximum = 20, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.Dot, Title = "Điện áp (V)", MajorStep = 4, Font = afont, FontSize = fontsize, LabelFormatter = _formatter }; LinearAxis XAxis = new LinearAxis { Position = AxisPosition.Bottom, Minimum = 0, Maximum = 400, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.Dot, Title = "Thời gian (µs) ", MajorStep = 50, Font = afont, FontSize = fontsize, LabelFormatter = _time_formatter }; Scope.Model.Axes.Add(XAxis); Scope.Model.Axes.Add(YAxis); //Timer 32bit, 100MHz, 50 MIPS, 2*(10^-8) sec/step,2*(10^-8)*50 = 1 µs if (TimerValue > 0) { double TimeRange = TimerValue * Math.Pow(10, 6) * TimerStep; //Micro Second step = TimeRange / SampleCount; //Micro Second for (int dt = 0; dt < SampleCount; dt++) { try { ChannelAVoltage[dt] = CalculateVoltage(ChannelA[dt], CHAX, GainA, CHAOffset, ErrA); ChannelBVoltage[dt] = CalculateVoltage(ChannelB[dt], CHBX, GainB, CHBOffset, ErrB); } catch (Exception e) { TextBoxRawData.Text = e.Message + " Voltage\r\n"; } } double MaxA = ChannelAVoltage.Max(); double MinA = ChannelAVoltage.Min(); double MaxB = ChannelBVoltage.Max(); double MinB = ChannelBVoltage.Min(); double MidA = (MaxA + MinA) / 2; double MidB = (MaxB + MinB) / 2; if (ToggleA.Checked == true) { LabelMaxAV.Text = String.Format("{0:0.00}V", ChannelAVoltage.Max()); LabelMinAV.Text = String.Format("{0:0.00}V", ChannelAVoltage.Min()); } if (ToggleB.Checked == true) { LabelMaxBV.Text = String.Format("{0:0.00}V", ChannelBVoltage.Max()); LabelMinBV.Text = String.Format("{0:0.00}V", ChannelBVoltage.Min()); } double MaxY = Math.Max(MaxA, MaxB); if (MaxY == 0) { MaxY = 20; } MaxY = Math.Abs(Math.Min(MinA, MinB)) > MaxY?Math.Abs(Math.Min(MinA, MinB)) + Math.Abs(Math.Min(MinA, MinB)) : MaxY + MaxY; if (CheckBoxAUP.Checked || CheckBoxBUP.Checked) { MaxY = 3 * MaxY; } double MinY = -MaxY; CalculateFreqAndRMS(); double FmaxA = 0, FmaxB = 0; double Fmax = 0; double Fzmax = 0; if (ka == true) { double[] DemA = new double[inpA.Length]; for (int i = 0; i < inpA.Length; i++) { DemA[i] = Complex.Abs(inpA[i]) / 10; } FmaxA = DemA.Max(); } if (kb == true) { double[] DemB = new double[inpB.Length]; for (int i = 0; i < inpB.Length; i++) { DemB[i] = Complex.Abs(inpB[i]) / 10; } FmaxB = DemB.Max(); } if (FmaxA > FmaxB) { Fmax = FmaxA; } else { Fmax = FmaxB; } if (ka == true && kb == false) { Fzmax = FreqA; } else if (ka == false && kb == true) { Fzmax = FreqB; } else if (ka == true && kb == true) { if (FreqA > FreqB) { Fzmax = FreqA; } else { Fzmax = FreqB; } } if (Kaaaaa == true) { YAxis.Maximum = Fmax; YAxis.Minimum = 0; YAxis.MajorStep = Fmax / 5; YAxis.Title = "Biên Độ (V)"; XAxis.Minimum = 0; XAxis.Maximum = Fzmax; XAxis.MajorStep = Fzmax / 12; XAxis.Title = "Tầng Số (Hz)"; XAxis.LabelFormatter = _time_formatter1; } else { YAxis.Maximum = MaxY; YAxis.Minimum = MinY; YAxis.MajorStep = MaxY / 5; XAxis.Minimum = 0; XAxis.Maximum = TimeRange; XAxis.MajorStep = TimeRange / 12; } var CHA = new LineSeries { Color = OxyColors.Blue, MarkerType = MarkerType.None, MarkerSize = 1, MarkerStroke = OxyColors.White, MarkerFill = OxyColors.Black, MarkerStrokeThickness = 1, LineStyle = LineStyle.Solid, Smooth = false, StrokeThickness = 1, Title = "KÊNH A", TrackerFormatString = "{0}\n{1}: {2:0.00}\n{3}: {4:0.00}" }; var CHB = new LineSeries { Color = OxyColors.Red, MarkerType = MarkerType.None, MarkerSize = 1, MarkerStroke = OxyColors.White, MarkerFill = OxyColors.Teal, MarkerStrokeThickness = 1, LineStyle = LineStyle.Solid, Smooth = false, StrokeThickness = 1, Title = "KÊNH B", TrackerFormatString = "{0}\n{1}: {2:0.00}\n{3}: {4:0.00}" }; if (Kaaaaa == false) { for (int dt = 0; dt < SampleCount; dt++) { if ((MinA >= 0) && (CheckBoxGNDA.Checked == true)) { ChannelAVoltage[dt] = ChannelAVoltage[dt] - MinA; } if ((MinB >= 0) && (CheckBoxGNDB.Checked == true)) { ChannelBVoltage[dt] = ChannelBVoltage[dt] - MinB; } try { if (CheckBoxAUP.Checked) { CHA.Points.Add(new DataPoint(step * dt, ChannelAVoltage[dt] + 3 * MaxB)); CHB.Points.Add(new DataPoint(step * dt, ChannelBVoltage[dt])); } else if (CheckBoxBUP.Checked) { CHA.Points.Add(new DataPoint(step * dt, ChannelAVoltage[dt])); CHB.Points.Add(new DataPoint(step * dt, ChannelBVoltage[dt] + 3 * MaxA)); } else { CHA.Points.Add(new DataPoint(step * dt, ChannelAVoltage[dt])); CHB.Points.Add(new DataPoint(step * dt, ChannelBVoltage[dt])); } } catch (Exception e) { TextBoxRawData.AppendText(e.Message + "Points"); } } } else { if (ka == true) { for (int i = 0; i < inpA.Length; i++) { CHA.Points.Add(new DataPoint(FreqA * i / inpA.Length, Complex.Abs(inpA[i]) / 10)); } } if (kb == true) { for (int i = 0; i < inpB.Length; i++) { CHB.Points.Add(new DataPoint(FreqB * i / inpB.Length, Complex.Abs(inpB[i]) / 10)); } } } if (ToggleA.Checked == true) { Scope.Model.Series.Add(CHA); } if (ToggleB.Checked == true) { Scope.Model.Series.Add(CHB); } CalculateFreqAndRMS(); Scope.Update(); Scope.Refresh(); } }
void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e) { if (this.InvokeRequired) { // Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway. this.BeginInvoke(new EventHandler <SerialDataEventArgs>(_spManager_NewSerialDataRecieved), new object[] { sender, e }); return; } // This application is connected to a GPS sending ASCCI characters, so data is converted to text string str = Encoding.ASCII.GetString(e.Data); TextBoxRawData.AppendText(str); if (TextBoxRawData.TextLength == 4021) { string[] RawData = TextBoxRawData.Text.Split(','); TextBoxRawData.Clear(); _spManager.Dispose(); if (RawData.Length > SampleCount * 2) { for (int i = 0; i < RawData.Length; i++) { if (i < SampleCount) { UInt16 ADC; if (UInt16.TryParse(RawData[i], out ADC)) { ChannelA[i] = ADC; } } if ((i >= SampleCount) && (i < (RawData.Length - 2))) { UInt16 ADC; if (UInt16.TryParse(RawData[i], out ADC)) { ChannelB[i - SampleCount] = ADC; } } if (i == (RawData.Length - 2)) { if (UInt32.TryParse(RawData[i], out TimerValue)) { } } if (i == (RawData.Length - 1)) { if (UInt32.TryParse(RawData[i], out SampleRateValue)) { DrawScope(); if (RadioButtonRepeat.Checked) { _spManager.clearInBuffer(); _spManager.clearOutBuffer(); _spManager.Dispose(); _spManager.Dispose(); _spManager.Dispose(); _spManager.StartListening(); if ((ToggleA.Checked == true) || (ToggleB.Checked == true)) { sendCmd(); } } } } } } } }