MyException - 我的异常网
当前位置:我的异常网» C# » SerialPort串口通讯中DataReceived事件不能触发

SerialPort串口通讯中DataReceived事件不能触发(9)

www.MyException.Cn  网友分享于:2013-02-20  浏览:827次


            //初始化SerialPort对象   
            comm.NewLine = "\r\n";
            comm.RtsEnable = true;//根据实际情况吧。   

            //添加事件注册   
            comm.DataReceived += comm_DataReceived;
        }

        private void buttonOpenClose_Click(object sender, EventArgs e)
        {
            //根据当前串口对象,来判断操作   
            if (comm.IsOpen)
            {
                Closing = true;
                while (Listening) Application.DoEvents();
                //打开时点击,则关闭串口   
                comm.Close();
            }
            else
            {
                //关闭时点击,则设置好端口,波特率后打开   
                comm.PortName = comboPortName.Text;
                //comm.BaudRate = int.Parse(comboBaudrate.Text);
                comm.BaudRate = 9600;
                try
                {
                    comm.Open();
                }
                catch (Exception ex)
                {
                    //捕获到异常信息,创建一个新的comm对象,之前的不能用了。   
                    comm = new SerialPort();
                    //现实异常信息给客户。   
                    MessageBox.Show(ex.Message);
                }
            }
            //设置按钮的状态   

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有