亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用arduino按端口顯示增量后如何清理標簽?

使用arduino按端口顯示增量后如何清理標簽?

C#
大話西游666 2023-08-20 15:47:31
我正在嘗試增加 Arduino 中的一個值并將其發送到端口,然后將其實時顯示在標簽中。即使我放置并延遲(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);            }        }    }}在變化中,我得到了所有的值,一個接一個,在屏幕上顯示其中一些。
查看完整描述

1 回答

?
郎朗坤

TA貢獻1921條經驗 獲得超9個贊

您正在處理程序中運行無限循環Afisare_Click,該循環與您的 UI 在同一線程中運行。這意味著 UI 將無法呈現控件更改。

Thread.Sleep代碼將上下文切換到其他線程,但不是 UI 線程。

您的方法應該是使用Timer。

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();

? ? ? ? }

? ? }

}


查看完整回答
反對 回復 2023-08-20
  • 1 回答
  • 0 關注
  • 143 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號