Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Leitura_0001
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- try
- {
- Porta.Open();
- }
- catch { }
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- try
- {
- if (Porta.IsOpen) Porta.Close();
- }
- catch { }
- }
- private void Porta_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
- {
- BeginInvoke(new Action(() =>
- {
- try
- {
- int limite = 50;//Número máximo de pontos que o grafico pode ter.
- string dados = Porta.ReadLine();//Pegue o que tem no buffer até um '\n'.
- Text = dados;//Mostrar o valor na barra de titulo.
- int num = Convert.ToInt16(dados);//Converta a string para int.
- chart1.Series[0].Points.AddY(num);//Adiciona o valor lido como um novo Y;
- //Se o grafico tiver número de pontos maior que o limite...
- if (chart1.Series[0].Points.Count > limite)
- {
- chart1.Series[0].Points.RemoveAt(0);//Remova o primeiro ponto.
- chart1.Update();//Atualiza o grafico.
- }
- }
- catch { }
- }));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement