Witam.
Jak zrobić , aby program ciągle odczytywał dane i je odświeżał w Visual C# , oto kod który tylko przy starcie podaje wartość temperatury a potem nic. Zależy mi aby samoczynnie się odświeżał nie klikając na żaden button . Chciałem dać pętle while , ale program nie chce startować .. Jak to zrobić ? Oto kod który do tej pory mam (Chodzi o metodę txttemperatura() , Progrm odczytuje dane z Serial Monitor i je wyświetla w Label , ale tylko przy starcie a ma ciągle odświeżać samoczynnie ;p ) :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace Arduino
{
public partial class Form1 : Form
{
private SerialPort myport;
public Form1()
{
InitializeComponent();
init();
txttemperatura();
}
private void on_bttn_Click(object sender, EventArgs e)
{
myport.WriteLine("N");
on_bttn.Enabled = false;
off_btn.Enabled = true;
}
private void init()
{
try
{
myport = new SerialPort();
myport.BaudRate = 9600;
myport.PortName = "COM5";
myport.Open();
myport.DtrEnable = true;
}
catch (Exception)
{
MessageBox.Show("Error");
}
on_bttn.Enabled = true;
off_btn.Enabled = false;
}
private void off_btn_Click(object sender, EventArgs e)
{
myport.WriteLine("F");
on_bttn.Enabled = true;
off_btn.Enabled = false;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
if (myport.IsOpen)
myport.WriteLine(trackBar1.Value.ToString());
label4.Text = "Wartosc = " + trackBar1.Value.ToString();
}
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
if (myport.IsOpen)
myport.WriteLine(vScrollBar1.Value.ToString());
label4.Text = "Wartosc = " + vScrollBar1.Value.ToString();
}
private void txttemperatura()
{
if (myport.IsOpen)
{
string temperatura = myport.ReadLine();
txttemp.Text = temperatura + "°C";
}
}
}
}