[英]Visual Studio Serial input from Arduino
這是代碼的更新版本,這與您所做的所有編輯相加嗎? 我認為它基本上是准確的,但是如果您已經使用過Visual Studio,您也可以嘗試使用它。
namespace Lexis
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
serialPort1.Open();
string lastLine = string.Empty;
Task.Run(() =>
{
while (true)
{
string tailValue = lastLine;
lastLine = serialPort1.ReadLine();
string line = lastLine;
label1.BeginInvoke(new Action
(() =>
{
label1.Text = string.IsNullOrEmpty(line) || string.Equals(tailValue, line)
? label1.Text
: $"{line}{Environment.NewLine}{label1.Text}";
}
));
Task.Delay(1000).Wait();
}
});
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
這可能不是您的黃金解決方案,但我希望這能給您帶來幫助。
public Form1()
{
InitializeComponent();
serialPort1.Open();
string currentLine = string.Empty;
object mylock = new object();
Task.Run(() =>
{
while (true)
{
lock (mylock)
{
string previousLine = currentLine;
currentLine = serialPort1.ReadLine();
string line = currentLine;
label1.BeginInvoke(new Action
(() =>
{
label1.Text = string.IsNullOrEmpty(line) || string.Equals(previousLine, line)
? label1.Text
: $"{line}{Environment.NewLine}{label1.Text}";
}
));
Task.Delay(TimeSpan.FromSeconds(1)).Wait();
}
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.