Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2014
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         }
  19.  
  20.         private void Form1_Load(object sender, EventArgs e)
  21.         {
  22.             try
  23.             {
  24.                 Porta.Open();
  25.             }
  26.             catch { }
  27.         }
  28.  
  29.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  30.         {
  31.             try
  32.             {
  33.                 if (Porta.IsOpen) Porta.Close();
  34.             }
  35.             catch { }
  36.         }
  37.  
  38.         private void Porta_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
  39.         {
  40.             BeginInvoke(new Action(() =>
  41.             {
  42.                 try
  43.                 {
  44.                     int limite = 50;//Número máximo de pontos que o grafico pode ter.
  45.                     string dados = Porta.ReadLine();//Pegue o que tem no buffer até um '\n'.
  46.                     Text = dados;//Mostrar o valor na barra de titulo.
  47.                     int num = Convert.ToInt16(dados);//Converta a string para int.
  48.                     chart1.Series[0].Points.AddY(num);//Adiciona o valor lido como um novo Y;
  49.  
  50.                     //Se o grafico tiver número de pontos maior que o limite...
  51.                     if (chart1.Series[0].Points.Count > limite)
  52.                     {
  53.                         chart1.Series[0].Points.RemoveAt(0);//Remova o primeiro ponto.
  54.                         chart1.Update();//Atualiza o grafico.
  55.  
  56.                     }
  57.                 }
  58.                 catch { }
  59.             }));
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement