Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2014
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Leitura_0001
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.             //chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Range;
  19.             //chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
  20.             //chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
  21.         }
  22.  
  23.         private void Form1_Load(object sender, EventArgs e)
  24.         {
  25.             try
  26.             {
  27.                 Porta.Open();
  28.             }
  29.             catch { }
  30.         }
  31.  
  32.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  33.         {
  34.             try
  35.             {
  36.                 if (Porta.IsOpen) Porta.Close();
  37.             }
  38.             catch { }
  39.         }
  40.  
  41.         private void Porta_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
  42.         {
  43.             BeginInvoke(new Action(() =>
  44.             {
  45.                 try
  46.                 {
  47.                     int limite = 50;//Número máximo de pontos que o grafico pode ter.
  48.                     string dados = Porta.ReadLine();//Pegue o que tem no buffer até um '\n'.
  49.                     Text = dados;//Mostrar o valor na barra de titulo.
  50.                     int num = Convert.ToInt16(dados);//Converta a string para int.
  51.                     chart1.Series[0].Points.AddY(num);//Adiciona o valor lido como um novo Y;
  52.  
  53.                     //Se o grafico tiver número de pontos maior que o limite...
  54.                     if (chart1.Series[0].Points.Count > limite)
  55.                     {
  56.                         chart1.Series[0].Points.RemoveAt(0);//Remova o primeiro ponto.
  57.                         chart1.Update();//Atualiza o grafico.
  58.  
  59.                     }
  60.                 }
  61.                 catch { }
  62.             }));
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement