Example #1
0
        /// <summary>
        /// 从设备中读取命令
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="timeout"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public override int ReadCmdFromDevice(ref string cmd, int timeout = 200, RichTextBox msg = null)
        {
            //---获取开始时间标签
            DateTime nowTime = DateTime.Now;
            int      _return = -1;

            if ((this.defaultSerialPort != null) && (this.defaultSerialPort.IsOpen))
            {
                CGenFuncDelay.GenFuncDelayms(timeout);
                if (this.defaultSerialPort.BytesToRead > 0)
                {
                    cmd     = this.defaultSerialPort.ReadExisting();
                    _return = 0;
                }
                else
                {
                    this.defaultSerialMsg = "未收到响应数据!";
                }
            }
            else
            {
                this.defaultSerialMsg = "端口:" + this.mCOMMName + "初始化异常!";
            }
            if ((msg != null) && (_return != 0))
            {
                CRichTextBoxPlus.AppendTextInfoTopWithDataTime(msg, this.defaultSerialMsg, Color.Red);
            }
            this.defaultSerialTimeout = timeout;
            //---结束时间
            this.defaultSerialUsedTime = DateTime.Now - nowTime;
            return(_return);
        }
        /// <summary>
        /// 将外进程嵌入到当前程序
        /// </summary>
        /// <param name="process"></param>
        private bool EmbeddedProcess(Process process)
        {
            //---是否嵌入成功标志,用作返回值
            bool isEmbedSuccess = false;
            //---外进程句柄
            IntPtr processHwnd = process.MainWindowHandle;
            //---容器句柄
            IntPtr panelHwnd = this.Handle;

            if (processHwnd != (IntPtr)0 && panelHwnd != (IntPtr)0)
            {
                //---把本窗口句柄与目标窗口句柄关联起来
                int setTime = 0;
                while (!isEmbedSuccess && setTime < 10)
                {
                    isEmbedSuccess = (CWinAPIWindows.SetParent(processHwnd, panelHwnd) != 0);
                    CGenFuncDelay.GenFuncDelayms(150);
                    //Thread.Sleep(100);
                    setTime++;
                }
                //---设置初始尺寸和位置
                CWinAPIWindows.MoveWindow(this.defaultProcess.MainWindowHandle, 0, 0, this.Width, this.Height, true);
                // Remove border and whatnot
                //---移除边框和右上角的最大,最小和关闭功能
                CWinAPIWindows.SetWindowLong(new HandleRef(this, this.defaultProcess.MainWindowHandle), CWinAPIWindows.GWL_STYLE, CWinAPIWindows.WS_VISIBLE);
            }

            if (isEmbedSuccess)
            {
                this.defaultEmbededWindowHandle = this.defaultProcess.MainWindowHandle;
            }

            return(isEmbedSuccess);
        }
        /// <summary>
        /// 功能码的变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (sender == null)
            {
                return;
            }
            CheckedListBox clb = (CheckedListBox)sender;

            //---这里是为防止双击操作,如果没有,快速双击会导致状态解析错误
            CGenFuncDelay.GenFuncDelayms(150);
            //---检测变化状态
            switch (clb.Name)
            {
            case "cCheckedListBoxEx_FuncMaskStep1":
                if ((this.defaultCMcuFunc != null) && (this.defaultCMcuFunc.mMcuInfoParam != null))
                {
                    this.defaultCMcuFunc.mMcuInfoParam.mChipFuncMask1 = this.defaultCMcuFunc.mMcuInfoParam.GetFuncMaskCheckedListBox(clb);
                }
                break;

            case "cCheckedListBoxEx_FuncMaskStep2":
                if ((this.defaultCMcuFunc != null) && (this.defaultCMcuFunc.mMcuInfoParam != null))
                {
                    this.defaultCMcuFunc.mMcuInfoParam.mChipFuncMask2 = this.defaultCMcuFunc.mMcuInfoParam.GetFuncMaskCheckedListBox(clb);
                }
                break;

            default:
                break;
            }
        }
        /// <summary>
        /// CheckedListBox_SelectedIndexChanged变化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //---检验当前任务空闲
            if ((sender == null))
            {
                return;
            }
            CheckedListBox clb   = (CheckedListBox)sender;
            int            index = clb.SelectedIndex;

            for (int i = 0; i < clb.Items.Count; i++)
            {
                if (index != i)
                {
                    clb.SetItemCheckState(i, CheckState.Unchecked);
                }
                else
                {
                    clb.SetItemCheckState(i, CheckState.Checked);
                    if (clb.Items[i].ToString() == "串口通讯")
                    {
                        this.defaulCCommType = CCOMM_TYPE.COMM_SERIAL;
                    }
                    else if (clb.Items[i].ToString() == "USB通讯")
                    {
                        this.defaulCCommType = CCOMM_TYPE.COMM_USB;
                    }
                }
            }
            //---这里是为防止双击操作,如果没有,快速双击会导致状态解析错误
            CGenFuncDelay.GenFuncDelayms(150);
        }
        /// <summary>
        /// CheckedListBox_SelectedIndexChanged变化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //---检验当前任务空闲
            if ((sender == null) || (this.defaultTaskIdle == true))
            {
                return;
            }
            CheckedListBox clb = (CheckedListBox)sender;

            //---这里是为防止双击操作,如果没有,快速双击会导致状态解析错误
            CGenFuncDelay.GenFuncDelayms(150);
            //clb.Enabled = false;
            //---设置输入焦点
            //clb.Focus();
            if (clb.SelectedItem.ToString() == "NC")
            {
                clb.SetItemCheckState(clb.SelectedIndex, CheckState.Checked);
                clb.SetItemCheckState(clb.SelectedIndex, CheckState.Indeterminate);
            }
            else
            {
                //---后台线程执行函数
                Thread t = new Thread
                               (delegate()
                {
                    if (clb.InvokeRequired)
                    {
                        clb.BeginInvoke((EventHandler)
                                        (delegate
                        {
                            this.UI_CheckedListBox_SelectedIndexChanged(clb);
                        }));
                    }
                    else
                    {
                        this.UI_CheckedListBox_SelectedIndexChanged(clb);
                    }
                }
                               );
                //---校验线程有效
                if (t != null)
                {
                    //---后台任务
                    t.IsBackground = true;
                    //---启动任务
                    t.Start();
                }
                //this.UI_CheckedListBox_SelectedIndexChanged(clb);
            }
            //clb.Enabled = true;
        }
        /// <summary>
        /// 嵌入进程
        /// </summary>
        /// <param name="processPath"></param>
        /// <returns></returns>
        public bool EmbeddedProcess(string processPath)
        {
            bool isStartAndEmbedSuccess = false;

            this.EventDone.Reset();

            //---启动进程
            this.defaultProcess = this.StartProcess(processPath);

            if (this.defaultProcess == null)
            {
                return(false);
            }

            //---等待新进程完成它的初始化并等待用户输入
            this.defaultProcess.WaitForInputIdle();

            //---确保可获取到句柄
            Thread thread = new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    if (this.defaultProcess.MainWindowHandle != (IntPtr)0)
                    {
                        this.EventDone.Set();
                        break;
                    }
                    //---
                    CGenFuncDelay.GenFuncDelayms(20);
                    //Thread.Sleep(10);
                }
            }));

            thread.Start();

            //---嵌入进程
            if (this.EventDone.WaitOne(10000))
            {
                isStartAndEmbedSuccess = this.EmbeddedProcess(defaultProcess);
                if (!isStartAndEmbedSuccess)
                {
                    this.KillProcess(this.defaultProcess);
                }
            }
            return(isStartAndEmbedSuccess);
        }