Arduinoで値をインクリメントしてポートに送信し、その後、ラベルにリアルタイムで表示しようとしています。
私が置いてdelay(200)とThread.sleep(200);
namespace Receiver
{
public partial class Form1 : Form
{
SerialPort port;
public Form1()
{
InitializeComponent();
this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
if (port == null)
{
port = new SerialPort("COM9", 9600);//Set your board COM
port.Open();
}
}
void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (port != null && port.IsOpen)
{
port.Close();
}
}
private void Afisare_Click(object sender, EventArgs e)
{
while (true)
{
string a = port.ReadExisting();
afisare.Text = a;
Thread.Sleep(200);
}
}
}
}
変化の中で、私はすべての値を次々と画面に表示しました。
Afisare_Click
UIと同じスレッドで実行されるハンドラーで無限ループを実行しています。これは、UIがコントロールの変更をレンダリングできないことを意味します。
Thread.Sleep
コードはコンテキストを他のスレッドに切り替えますが、UIスレッドは切り替えません。
あなたのアプローチはタイマーを使用する必要があります。
public partial class Form1 : Form
{
private Timer _timer;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
_timer = new Timer();
_timer.Interval = 200;
_timer.Tick += _timer_Tick;
_timer.Enabled = true;
_timer.Start();
}
private void _timer_Tick(object sender, EventArgs e)
{
// This function will be called every 200 ms.
// Read the information from port, update the UI.
string a = port.ReadExisting();
afisare.Text = a;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
_timer.Stop();
if (port != null && port.IsOpen)
{
port.Close();
}
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加