1

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

笔记:

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

  2. 该程序以前可以运行,我今天刚刚添加了高速设置。所以对于 xbee 设备,我必须以 9600 速度开始,然后更改为我需要的任何速度,即 38400。但是,一旦我将其设置为 38400,无需重启 xbee。下次连接设备时,速度保持@38400。这就是我添加尝试与另一个波特率连接的 if(initial_fail) 块的原因。

  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
            }
    

这是 GUI 的一部分,如下所示: 在此处输入图像描述

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

4

1 回答 1

0

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

于 2012-08-12T03:03:34.683 回答