如何在文本框中显示从串行端口接收的数据,而不会在 Visual Studio C# 中文本消失? [英] How to display data received from serial port in a textbox without the text disappearing in Visual Studio C#?

查看:81
本文介绍了如何在文本框中显示从串行端口接收的数据,而不会在 Visual Studio C# 中文本消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在尝试用 Visual C# 开发一个简单的应用程序,它从串行端口获取数据并将其显示在文本框中(以监控温度).我正在成功获取并显示数据,使用 DataReceived 事件更新全局字符串变量和计时器来更新我的文本框上的文本字段,如下所示:

So, I'm trying to develop a simple application in visual C# which gets data from serial port and displays it in a textbox (to monitor temperature). I'm acquiring and displaying the data successfully, using the DataReceived event to update a global string variable and a timer to update the text field on my text box, as shown:

    private void port_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
            globalVar.updateTemp = port.ReadLine(); //This is my global string
        }
        catch (IOException)
        {
        }
        catch (InvalidOperationException)
        {
        }
        catch (TimeoutException)
        {
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        tempDisplayBox.Text = globalVar.updateTemp; //This is my textbox updating
    }

我遇到的唯一问题是文本框中显示的值不断闪烁,难以阅读.我的计时器设置为每 10 毫秒触发一次(应该足够快,对吧?).有没有办法让它更稳定?我意识到这可能是一个新手问题,但公平地说,我是一个新手 :) 任何帮助表示赞赏!谢谢!

The only issue I have is that the value shown in the textbox keeps flashing, making it hard to read. My timer is set to trigger every 10 ms (which should be fast enough, right?). Is there any way to make it more stable? I realize this may be a newb question, but to be fair I am a newb :) Any help is appreciated! Thanks!

推荐答案

非常感谢您的解答!我找到了解决此问题的方法:而不是通过重写 TextBox.Text 属性来替换我的文本框的内容 - 正如 HenningNT 暗示的那样,刷新控件并导致闪烁 - 我现在使用的是 TextBox.AppendText 方法.但是,由于我一次只想显示一行数据,因此我在多行模式下使用文本框和 Environment.NewLine 在附加文本之前跳转到新行.至于更新方法,我已经重新使用计时器,因为由于某种原因,当我关闭表单时,invoke 方法会使我的应用程序崩溃.此外,启用双缓冲对我没有多大好处,虽然我想我做错了......它仍然有点闪烁,但现在好多了:)我知道这并不是一个完美的解决方案(更多的是一种解决方法),所以我会继续寻找它.如果我找到它,我一定会在这里更新它;) 我的代码:

Thank you so much for the answers! I found a way to work around this issue: Instead of replacing the contents of my textbox by rewriting the TextBox.Text property - which, as HenningNT implied, refreshes the control and causes the flickering - I'm now using the TextBox.AppendText method. Though, as I want to display only one line of data at a time, I use the textbox in multiline mode and the Environment.NewLine to jump to a new line before appending the text. As for the method of updating, I've gone back to using the timer because with the invoke method was crashing my application when I close the form, for some reason. Also, enabling double buffering didn't do me much good, although I guess I was doing it wrong... It still flickers a bit, but it's much better now :) I know this is not really a perfect solution (much more of a workaround), so I'll keep looking for it. If I find it, I'll be sure to update it here ;) My code:

    private void timer1_Tick(object sender, EventArgs e) //Timer to update textbox
    {
        if (tempDisplayBox.Text != globalVar.updateTemp) //Only update if temperature is different
        {
            try
            {
                tempDisplayBox.AppendText(Environment.NewLine);
                tempDisplayBox.AppendText(globalVar.updateTemp);
            }
            catch (NullReferenceException)
            { 
            }
        }
    }

这篇关于如何在文本框中显示从串行端口接收的数据,而不会在 Visual Studio C# 中文本消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆