public void comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int i;

            try
            {
                byte[] framebuffer = new byte[128];
                byte   frametype   = 0;
                int    n           = CVar.GetComm().BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
                if (n == 0)
                {
                    return;
                }

                byte[] buf = new byte[n]; //声明一个临时数组存储当前来的串口数据
                received_count += n;      //增加接收计数
                comm.Read(buf, 0, n);     //读取缓冲数据


                //Add receive data to que
                for (i = 0; i < n; i++)
                {
                    FifoClass.EnFifo(buf[i]);
                }


                if (GetFrame(ref framebuffer, ref frametype) > 0)
                {
                    st_CommParaSet commPara = new st_CommParaSet();
                    commPara = (st_CommParaSet)CommProtocol.BytesToStruct(framebuffer, commPara.GetType());

                    UploadData.hall_angle = commPara.hall_angle;
                    UploadData.zeroAngle  = commPara.hall_zero_angle;
                    UploadData.hall_x     = commPara.hall_x;
                    UploadData.hall_y     = commPara.hall_y;

                    UploadData.maxDegree  = commPara.maxDegree;
                    UploadData.maxCurrent = commPara.maxCurrent;
                    UploadData.Kp         = commPara.pid_p;
                    UploadData.Ki         = commPara.pid_i;
                    UploadData.Ke1        = commPara.pid_ke1;
                    UploadData.Ke2        = commPara.pid_ke2;


                    UploadData.reversse  = commPara.reverse;
                    UploadData.run2zero  = commPara.run2zero;
                    UploadData.traceMode = commPara.traceMode;
                }
            }
            finally
            {
            }
        }
        private void buttonOpenClose_Click(object sender, EventArgs e)
        {
            if (buttonOpenClose.Text == "关闭")
            {
                if (comm.IsOpen)
                {
                    comm.Close();
                }
                buttonOpenClose.Text = "打开";
                return;
            }
            //根据当前串口对象,来判断操作
            if (comm == null)
            {
                //关闭时点击,则设置好端口,波特率后打开
                comm.PortName        = comboPortName.Text;
                comm.BaudRate        = int.Parse(comboBaudrate.Text);
                comm.DataBits        = 8;
                comm.Parity          = Parity.None;
                comm.StopBits        = StopBits.One;
                comm.WriteBufferSize = 1024 * 1024 * 5;
                comm.ReadBufferSize  = 1024 * 1024 * 5;
                try
                {
                    comm.Open();
                    MessageBox.Show("串口已打开");
                }
                catch (Exception ex)
                {
                    //捕获到异常信息,创建一个新的comm对象,之前的不能用了。
                    comm = new SerialPort();
                    //现实异常信息给客户。
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                if (comm.IsOpen)
                {
                    while (CVar.GetCommBusy())
                    {
                        Application.DoEvents();
                    }
                    //打开时点击,则关闭串口
                    comm.Close();
                    MessageBox.Show("串口已关闭");
                }
                else
                {
                    //关闭时点击,则设置好端口,波特率后打开
                    comm.PortName        = comboPortName.Text;
                    comm.BaudRate        = int.Parse(comboBaudrate.Text);
                    comm.DataBits        = 8;
                    comm.Parity          = Parity.Even;
                    comm.StopBits        = StopBits.One;
                    comm.WriteBufferSize = 1024 * 1024 * 5;
                    comm.ReadBufferSize  = 1024 * 1024 * 5;
                    try
                    {
                        comm.Open();
                        MessageBox.Show("串口已打开");
                    }
                    catch (Exception ex)
                    {
                        //捕获到异常信息,创建一个新的comm对象,之前的不能用了。
                        comm = new SerialPort();
                        //现实异常信息给客户。
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            //设置按钮的状态
            buttonOpenClose.Text = comm.IsOpen ?  "关闭" : "打开";

            if (comm.IsOpen)
            {
                CommPara.portnum  = comm.PortName;
                CommPara.baudrate = comm.BaudRate;
                CommPara.ParaWrite();
                CVar.form1.button1.Text = CommPara.portnum.ToString() + "已打开";
                comm.DataReceived      += CVar.form1.comm_DataReceived;
            }
        }
 public static void SendCmd(byte Cmd)
 {
     MakeFrame(Cmd);
     CVar.GetComm().Write(FrameBuffer, 0, Marshal.SizeOf(commPara));
 }