简体   繁体   English

来自Arduino的Visual Studio串行输入

[英]Visual Studio Serial input from Arduino

this is the updated version of the code does this add up with all the edits you made? 这是代码的更新版本,这与您所做的所有编辑相加吗? I think it's mostly accurate but you can also try with visual studio, if you already did is it working on yours? 我认为它基本上是准确的,但是如果您已经使用过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)
            {

            }
        }
    }

This may not be your golden solution but I hope this gives you a lead. 这可能不是您的黄金解决方案,但我希望这能给您带来帮助。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM