Ejemplo n.º 1
0
        ///-----------------------------------------------------------------
        /// <summary>
        /// 对    象:FrmSCI:窗体
        /// 事    件:Load
        /// 功    能:执行加载窗体程序,自动获得串口号
        ///          同时在标签LblSCI中显示串口相关信息
        /// 函数调用:无
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks></remarks>
        ///-----------------------------------------------------------------
        private void FrmSCI_Load(object sender, EventArgs e)
        {
            //初始化时,按钮显示"打开串口(Open SCI)"
            this.BtnSCISwitch.Text   = "打开串口(Open SCI)";
            this.CbSCIBaud.Enabled   = true;   //[波特率选择框]处于可用状态
            this.CbSCIComNum.Enabled = true;   //[串口选择框]处于可用状态
            this.BtnSCISend.Enabled  = false;  //发送按钮处于不可用状态
            //自动搜索串口,并将其加入到[串口选择框]中
            int i;

            string[] SCIPorts;
            SCIPorts = SCI.SCIGetPorts();

            this.CbSCIComNum.Items.Clear();//首先将现有的项清除掉
            for (i = 0; i < SCIPorts.Length; i++)
            {
                //向[串口选择框]中添加搜索到的串口号
                this.CbSCIComNum.Items.Add(SCIPorts[i]);
            }
            //设置各组合框的初始显示值
            if (SCIPorts.Length > 0)
            {
                this.BtnSCISwitch.Enabled        = true;
                this.CbSCIBaud.SelectedIndex     = 0;
                this.CbSCIComNum.SelectedIndex   = 0;
                this.CbSCISendType.SelectedIndex = 0;

                //设置初始的串口号与波特率
                PublicVar.g_SCIComNum   = this.CbSCIComNum.Text;
                PublicVar.g_SCIBaudRate = int.Parse(this.CbSCIBaud.Text);
                //显示当前串口信与状态信息
                this.LblSCI.Text = str + PublicVar.g_SCIComNum + "、" +
                                   PublicVar.g_SCIBaudRate + "\n" + msg;
                this.TSSLState.Text = "无操作,请先选择波特率与串口号,打开串口," +
                                      "然后发送数据";
            }
            else
            {
                this.TSSLState.Text       = "没有可用的串口,请检查!";
                this.BtnSCISwitch.Enabled = false;
            }
        }
Ejemplo n.º 2
0
        ///-----------------------------------------------------------------
        /// <summary>
        /// 对    象:BtnSCISwitch
        /// 事    件:Click
        /// 功    能:(1)当开关显示为打开串口(Open SCI),则单击时执行打开串口
        ///          操作,并在标签LblSCI中显示选择的串口号与波特率,并在
        ///          状态条文本TSSLState中显示当前操作
        ///          (2)当开关显示为关闭串口(Close SCI),则单击时执行关闭串口
        ///          操作,并在标签LblSCI中显示关闭的串口号与波特率,并在
        ///          状态条文本TSSLState中显示当前操作
        /// 函数调用:(1)SCIInit:串口初始化
        ///          (2)SCIClose:关闭串口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        ///-----------------------------------------------------------------
        private void BtnSCISwitch_Click(object sender, EventArgs e)
        {
            bool flag;//标记打开是否成功

            //根据按钮BtnSCISwitch显示内容执行打开或关闭串口操作
            if (this.BtnSCISwitch.Text.CompareTo("打开串口(Open SCI)") == 0)
            {
                //提示当前正在执行打开串口操作
                this.TSSLState.Text = "过程提示:正在打开串口...";
                //初始化SCI类对象sci
                sci = new SCI(PublicVar.g_SCIComNum, PublicVar.g_SCIBaudRate);
                if (sci.SCIOpen())//串口打开成功
                {
                    //设置接收中断处理事件
                    sci.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.SCIPort_DataReceived);
                    //设置每接收到1个字节中断1次
                    sci.SCIReceInt(1);

                    //显示打开串口相关信息
                    this.LblSCI.Text         = str + PublicVar.g_SCIComNum + "、" + PublicVar.g_SCIBaudRate + "\n" + msg;
                    this.BtnSCISwitch.Text   = "关闭串口(Close SCI)"; //修改按钮上文字
                    this.CbSCIComNum.Enabled = false;             //禁用[串口选择框]
                    this.CbSCIBaud.Enabled   = false;             //禁用[波特率选择框]
                    //状态上显示结果信息
                    this.TSSLState.Text = this.TSSLState.Text +
                                          "打开" + PublicVar.g_SCIComNum + "成功!" + "波特率选择:" + PublicVar.g_SCIBaudRate;
                    this.pictureBox1.Image  = SerialPort.Properties.Resources.Run;
                    this.BtnSCISend.Enabled = true;
                }
                else//串口打开失败
                {
                    this.TSSLState.Text = this.TSSLState.Text +
                                          "打开" + PublicVar.g_SCIComNum + "失败!";
                    this.pictureBox1.Image  = SerialPort.Properties.Resources.Run_static;
                    this.BtnSCISend.Enabled = false;
                }
            }
            else if (this.BtnSCISwitch.Text == "关闭串口(Close SCI)")
            {
                //设置接收中断处理事件
                sci.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(this.SCIPort_DataReceived);
                //提示当前操作
                this.TSSLState.Text = "过程提示:正在关闭串口...";
                //执行关闭串口操作,并用Flag返回结果
                flag = sci.SCIClose();
                if (flag == true)
                {
                    this.LblSCI.Text = str + PublicVar.g_SCIComNum
                                       + "、" + PublicVar.g_SCIBaudRate + "\n" + msg;
                    this.BtnSCISwitch.Text = "打开串口(Open SCI)";
                    //[串口选择框]处于可用状态
                    this.CbSCIComNum.Enabled = true;
                    //[波特率选择框]处于可用状态
                    this.CbSCIBaud.Enabled  = true;
                    this.TSSLState.Text    += "关闭" + PublicVar.g_SCIComNum + "成功!";
                    this.pictureBox1.Image  = SerialPort.Properties.Resources.Run_static;
                    this.BtnSCISend.Enabled = false;
                }
                else//串口关闭失败
                {
                    this.TSSLState.Text += "关闭" + PublicVar.g_SCIComNum + "失败!";
                }
            }
        }
Ejemplo n.º 3
0
        //========================以下是本窗体的各个事件响应函数==========================

        //------------------------单击“检测PC节点”按钮----------------------------------
        private void btnCheckPcNode_Click(object sender, EventArgs e)
        {
            //int ret;            //返回值
            //string com = "";    //串口信息
            string sTemp = "";

            //byte[] recv = null;//保存串口接收信息
            //byte[] shake = { (byte)10, (byte)'s', (byte)'h', (byte)'a', (byte)'k', (byte)'e' }; //与终端握手帧数据

            byte[] userShake = { (byte)11, (byte)'a', (byte)'u', (byte)'a', (byte)'r', (byte)'t', (byte)'?' }; //与终端握手帧数据
            //byte[] send = { (byte)'S' };

            string[] SCIPorts;
            SCIPorts = SCI.SCIGetPorts();

            this.Refresh();                     //刷新显示

            //if (emuart._Uartport != null) emuart._Uartport.Close();
            //Thread.Sleep(10);   //等待串口关闭
            //emuart = EMUART.getInstance();    //每次“重新连接”重新实例化
            //ret = emuart.findDevice(out com, 115200);  //寻找emuart设备
            //this.lbl_PC_Node_State.Text = com;       //右上角显示区

            //if (ret == 1) goto btn_uartcheck_Click_EXIT1;
            ////(4.2)找到串口,没有找到UE,退出函数
            //else if (ret == 2) goto btn_uartcheck_Click_EXIT2;
            //emuart.bufferClear();   //清除接收数组缓冲区
            //emuart.send(shake, out recv, 200, 3); //获得设备的信息在recv中
            //if (recv == null) goto btn_uartcheck_Click_EXIT3;  //没有接收到设备返回消息

            //if (recv.Length == Marshal.SizeOf(typeof(shakeData)))
            //{
            //    //byte数组转结构体
            //    shakedata = (shakeData)bytesToStruct(recv, typeof(shakeData));
            //    //获取握手帧数据
            //    uecomType = Encoding.Default.GetString(shakedata.uecomType).Replace("\0", "");
            //    mcuType = Encoding.Default.GetString(shakedata.mcuType).Replace("\0", "");

            //    //设置设备信息
            //    sTemp = com + ":" + uecomType + " " + mcuType;  //设备信息
            //    //状态提示
            //    this.lbl_PC_Node_State.Text = sTemp;     //右上角显示区
            //    this.btnPnFlashErase.Enabled = true;
            //    this.btnPnFlashRead.Enabled = true;
            //    this.btnPnFlashReadAddr.Enabled = true;
            //    this.btnPnFlashWrite.Enabled = true;

            //}
            //else if (recv.Length == Marshal.SizeOf(typeof(newshakeData)))
            //{
            int i;

            //byte数组转结构体
            //newshakedata = (newshakeData)bytesToStruct(recv, typeof(newshakeData));
            ////获取握手帧数据
            //uecomType = Encoding.Default.GetString(newshakedata.uecomType).Replace("\0", "");
            //mcuType = Encoding.Default.GetString(newshakedata.mcuType).Replace("\0", "");
            //设置设备信息
            //sTemp = com + ":" + uecomType + " " + mcuType;  //设备信息
            if (sci != null && sci.IsOpen)
            {
                sci.Close();
            }

            //发送全局变量_SendByteArray中的数据,并返回结果
            for (i = 0; i < SCIPorts.Length; i++)
            {
                //if (SCIPorts[i] == com) continue;

                sci = new SCI(SCIPorts[i], 115200);
                if (sci.SCIOpen())
                {
                    //sci.SCISendData(ref send);           //发送与Uart_User串口握手的数据信息
                    sci.SCISendFrameData(ref userShake);

                    Thread.Sleep(500);
                    if (sci.SCIReceiveData(ref recvData))
                    {
                        //MessageBox.Show(System.Text.Encoding.Default.GetString(recvData));

                        //if (recvData.Length == 0 || !System.Text.Encoding.Default.GetString(recvData).Contains("Yes,I am an emuart!!"))
                        //{
                        //    this.tb_statue.Text = "未找到终端设备";
                        //    //this.lbl_PC_Node_State.Text = "未找到终端设备";

                        //    MessageBox.Show("检查UART_User(KL36:UART0)串口有无接好");

                        //    this.btnPnFlashErase.Enabled = false;
                        //    this.btnPnFlashRead.Enabled = false;
                        //    this.btnPnFlashReadAddr.Enabled = false;
                        //    this.btnPnFlashWrite.Enabled = false;
                        //    sci.Close();
                        //    continue;
                        //}
                        if (System.Text.Encoding.Default.GetString(recvData).Contains("Yes,I am an emuart!!"))       //记录UART_User串口的Com号
                        {
                            string retInfo = System.Text.Encoding.Default.GetString(recvData);
                            this.tb_statue.Text = "找到终端设备";
                            //this.lbl_PC_Node_State.Text = "连接终端在" + PublicVar.g_SCIComNum;
                            PublicVar.g_SCIComNum = SCIPorts[i];

                            this.btnPnFlashErase.Enabled    = true;
                            this.btnPnFlashRead.Enabled     = true;
                            this.btnPnFlashReadAddr.Enabled = true;
                            this.btnPnFlashWrite.Enabled    = true;
                            //sTemp = retInfo.Substring(retInfo.IndexOf('.') + 1, retInfo.Length - retInfo.IndexOf('.') - 1);
                            //sTemp = retInfo.Substring(retInfo.IndexOf('!') + 2, retInfo.Length - retInfo.IndexOf('!') - 2);
                            sTemp = retInfo.Substring(retInfo.IndexOf('!') + 2, 19);
                            //sTemp = retInfo.Substring(retInfo.IndexOf('.') + 1, retInfo.Length - retInfo.IndexOf('.') - 1);
                            this.lbl_PC_Node_State.Text = SCIPorts[i] + ":" + sTemp;     //右上角显示区

                            //this.lbl_PC_Node_State.Text = sTemp;     //右上角显示区
                            break;                                                                  //找到后UART_User串口后,跳出循环
                        }
                    }
                }
            }
            if (i == SCIPorts.Length)
            {
                this.tb_statue.Text = "未找到终端设备";
                MessageBox.Show("检查UART_User(KL36:UART0)串口有无接好");
                this.btnPnFlashErase.Enabled    = false;
                this.btnPnFlashRead.Enabled     = false;
                this.btnPnFlashReadAddr.Enabled = false;
                this.btnPnFlashWrite.Enabled    = false;
                sci.Close();
            }
            //状态提示

            //}
            //else goto btn_uartcheck_Click_EXIT3;  //接收到错误返回
            ////(5)退出区
            ////(5.1)退出函数
            //btn_uartcheck_Click_EXIT:
            //    btnCheckPcNode.Enabled = true;
            //    return;

            ////(5.2)不存在可用串口
            //btn_uartcheck_Click_EXIT1:
            //    btnCheckPcNode.Text = "运行状态:当前不存在可用串口";  //右上角提示区
            //    this.btnPnFlashErase.Enabled = false;
            //    this.btnPnFlashRead.Enabled = false;
            //    this.btnPnFlashReadAddr.Enabled = false;
            //    this.btnPnFlashWrite.Enabled = false;
            //    goto btn_uartcheck_Click_EXIT;

            ////(5.3)存在串口,但不存在emuar设备
            //btn_uartcheck_Click_EXIT2:
            //    emuart.terminate(115200); //发送数据给终端设备,让终端设备清空其数据缓冲区
            //    btnCheckPcNode.Text = "重新连接";  //连接按键文本
            //    lbl_PC_Node_State.Text = "运行状态:已连接串口" + com + "但未找到设备";   //右上角提示区
            //    this.btnPnFlashErase.Enabled = false;
            //    this.btnPnFlashRead.Enabled = false;
            //    this.btnPnFlashReadAddr.Enabled = false;
            //    this.btnPnFlashWrite.Enabled = false;
            //    goto btn_uartcheck_Click_EXIT;

            //(5.4)没有正确接收到设备返回消息
            //btn_uartcheck_Click_EXIT3:
            //    emuart.terminate(115200); //发送数据给终端设备,让终端设备清空其数据缓冲区
            //    btnCheckPcNode.Text = "重新连接";   //连接按钮文本
            //    lbl_PC_Node_State.Text = "找到GEC在" + com + "但握手失败,请再次单击[重新连接]按钮";   //右上角提示区
            //    this.btnPnFlashErase.Enabled = false;
            //    this.btnPnFlashRead.Enabled = false;
            //    this.btnPnFlashReadAddr.Enabled = false;
            //    this.btnPnFlashWrite.Enabled = false;
            //    goto btn_uartcheck_Click_EXIT;
        }