为什么只在抛出异常后才更新表单

时间:2012-08-12 02:10:42

标签: c# serial-port xbee

我正在开发一个winform程序,主要涉及Xbee和PC之间的COM端口通信。请允许我先告诉你问题:

注意:

  1. 不要担心委托函数,它只是检查返回的字节

  2. 该程序之前有效,我今天刚刚添加了高速设置。所以 对于xbee设备,我必须以9600速度开始,然后更改为 无论我需要什么,这是38400.然而,一旦我把它设置为38400, 没有电源循环xbee。当我下次连接设备时 ,速度保持在@ 38400。这就是我添加的原因 if(initial_fail)阻止尝试连接另一个baud_rate。

  3. 请按照我用数字标记的评论

        try
        {
            portBuffer.Clear();
            myComPort.Write(myxbee.myxbee_cmd.cmd_mode, 0, myxbee.myxbee_cmd.cmd_mode.Length);   //RETURN OK <CR> 
    
            IAsyncResult res = d.BeginInvoke(null, null);
            if (res.IsCompleted == false)
            {
                res.AsyncWaitHandle.WaitOne(2000, false);
                if (res.IsCompleted == false)
                    initial_fail = 1; //1. start from here, once there is a timeout, set the initial_fail to 1, Time-out because the baudrate is not sync
                 if (d.EndInvoke(res) == false)
                   throw new Exception("Failing to enter the cmd mode");
            }
        }
        catch (ApplicationException e)
        {
            MessageBox.Show(e.Message);
            return false;
        }
        catch (Exception e)
        {
            MessageBox.Show("Error at step 1: {0}", e.Message);
            return false;
        }
        //2. here is the new code I added today
        if (initial_fail == 1)
        {
            myComPort.BaudRate = 38400; //3. Here I changed the speed and gui text
            cmbBaudRate.Text = "38400"; //PROBLEM: when Im doing step by step debug, seems like these two lines doesnt get executed. Winform GUI remain unchanged
            try
            {
    
                portBuffer.Clear();
                myxbee.command_buffer.Clear();
                dummy = "05";
                dummy_byte = Encoding.ASCII.GetBytes(dummy);
                myxbee.command_buffer.AddRange(myxbee.myxbee_cmd.data_rate); 
                myxbee.command_buffer.AddRange(dummy_byte);
                myxbee.command_buffer.Add(myxbee.myxbee_cmd.line_feed);
                myComPort.Write(myxbee.command_buffer.ToArray(), 0, myxbee.command_buffer.ToArray().Length);
                IAsyncResult res1 = d.BeginInvoke(null, null);
                if (res1.IsCompleted == false)
                {
                    res1.AsyncWaitHandle.WaitOne(1000, false);
                    if (res1.IsCompleted == false)
                        throw new ApplicationException("Timeout"); 
                        //4. since the winform hasnt shown any bd_rate change, the program will throw a time-out here again 
                }
                if (d.EndInvoke(res1) == false)
                    throw new Exception("Fail in setting data rate to 38400");
                chkHighSpeed.Checked = true;
                high_speed_set = 1;
                MessageBox.Show("Xbee high speed");
            }
            catch (ApplicationException e)
            {
                MessageBox.Show(e.Message);
                return false;
                //5. BUT AFTER THIS TIMEOUT message, the winform's GUI will be updated to 38400, and rerun the whole test will pass
            }
    
  4. 这是GUI的一部分,如下所示: enter image description here

    所以我的问题是为什么只有在超时异常之后,才会更新波特率?

1 个答案:

答案 0 :(得分:0)

问题来自于你在与ui相同的线程上进行处理的事实。 您的com进程可以冻结SO UI。 您可以使用更新UI来解决问题的doevents方法,但这不是您的最佳方法。 使用后台工作程序在另一个线程上启动您的com进程。