简体   繁体   English

C#Winforms,从numericupdown存储值

[英]C# winforms, storing value from numericupdown

I have a serialport dataRecived event with 2 methods inside it. 我有一个带有2个方法的serialport dataRecived事件。 The LogFile is loging data and drawSetpoint is drawing a graph. LogFile正在记录数据,而drawSetpoint正在绘制图形。

public void serialPort1_DataRecived  (object sender, SerialDataReceivedEventArgs e)
        {

            DateTime time = DateTime.Now;
            string Rdata = serialPort1.ReadLine();
            LogFile(Rdata, (int)numericSetpoint.Value);
            drawSetpoint(time,numericSetpoint.Value.ToString());


        }

Both methods take the second argument from a numericUpDown control which looks like this 这两种方法都采用一个来自numericUpDown控件的第二个参数,如下所示

public void numericSetpoint_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) 
            {

                if (serialPort1.IsOpen) 
                {
                    int setpoint = (int)numericSetpoint.Value;
                    //send to serial port .....                    
                }

The problem is both methods in my data recived event read digits as I type them. 问题是当我键入它们时,我的数据接收事件读取数字中的这两种方法。 For example, if I type 150, LogFile will show 1,15,150 and the draw function will start drawing 1,15,150. 例如,如果我键入150,LogFile将显示1,15,150,而draw函数将开始绘制1,15,150。 I would like both functions to take the value from the numericSetpoint control after the enter key is pressed so i get the whole value. 我希望这两个函数都可以在按下回车键之后从numericSetpoint控件中获取值,以便获得整个值。 How could I do that ? 我该怎么办?

You are using the KeyPress event. 您正在使用KeyPress事件。 Instead consider using the ValueChanged event which only fires when enter is pressed or the user leaves the control https://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.valuechanged%28v=vs.110%29.aspx 而是考虑使用ValueChanged事件,该事件仅在按下Enter键或用户离开控件时才会触发, 网址为https://msdn.microsoft.com/zh-cn/library/system.windows.forms.numericupdown.valuechanged%28v=vs.110 %29.aspx

It sounds like your data rx event is firing while your typing. 听起来您的数据rx事件在您键入时正在触发。 I would try filling a buffer in your data rx event and hold off on any logging or plotting until you've sent your data. 我会尝试在您的data rx事件中填充一个缓冲区,并保留所有日志记录或绘图,直到您发送数据为止。 There are bunch of "safe" ways to do this but the core logic is as follows: 有很多“安全”的方法可以做到这一点,但核心逻辑如下:

byte[] buffer = new byte[MAX_BUFFER];
private volatile bool _ready = false;
private Object _lock = new Object();

public void serialPort1_DataRecived  (object sender, SerialDataReceivedEventArgs e)
{

    DateTime time = DateTime.Now;

    // Either read as bytes or convert string to bytes, add to your buffer
    // string Rdata = serialPort1.ReadLine();

    lock(_lock )
    {
        if(_ready)
        {
            _ready = false;
            var myData = your buffer as string
            // clear buffer
            LogFile(myData, (int)numericSetpoint.Value);
            drawSetpoint(time,numericSetpoint.Value.ToString());
        }
    }

}

...

public void numericSetpoint_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == 13) 
    {

        if (serialPort1.IsOpen) 
        {
            int setpoint = (int)numericSetpoint.Value;
            //send to serial port .....     
            lock(_lock ) { _ready = true; }               
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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