void sp1_DataReceived(object sender, SerialDataReceivedEventArgs e) { int err = 0; if (sp1.IsOpen) { try { protocol prot1 = new protocol(); Byte[] receiveData = new Byte[sp1.BytesToRead]; sp1.Read(receiveData, 0, receiveData.Length); sp1.DiscardInBuffer(); byte[] strRcv = new byte[receiveData.Length]; //string strRcv1 = Encoding.Default.GetString(receiveData); string strRcvHex = null; string strRcvChar = null; for (int i = 0; i < receiveData.Length; i++) { strRcvHex += " 0x"; strRcvHex += receiveData[i].ToString("X"); strRcvChar += (char)receiveData[i]; } err = prot1.RcvDeal(receiveData); if (err == 0) { //截取帧头 string str = null; str += "0x"; str += prot1.start1.ToString("X"); str += " 0x"; str += prot1.start2.ToString("X"); tbRStart.Text = str; //获得Mac str = null; str += "0X"; str += prot1.mac.ToString("X"); tbRAddr.Text = str; //获得cmd str = null; str += "0X"; str += prot1.cmd.ToString("X"); tbRCmd.Text = str; //获得cmd str = null; str += "0X"; str += prot1.datalength.ToString("X"); tbRValidDataLen.Text = str; //获得帧尾 str = null; str += "0x"; str += prot1.end1.ToString("X"); str += " 0x"; str += prot1.end2.ToString("X"); tbREnd.Text = str; str = null; str = prot1.vrms.ToString(); tbVrms.Text = str; str = null; str = prot1.irms.ToString(); tbIrms.Text = str; str = null; str = prot1.p.ToString(); tbP.Text = str; str = null; str = prot1.ep.ToString(); tbEp.Text = str; str = null; if (prot1.outputstatu == 1) { str = "开启"; } else { str = "关闭"; } tbOutput.Text = str; str = null; str += "0x"; str += prot1.cksum1.ToString("X"); tbRCkSum1.Text = str; str = null; str += "0x"; str += prot1.cksum2.ToString("X"); tbRCkSum2.Text = str; str = null; str += "0x"; str += prot1.MAX_DATALEN.ToString("X"); tbRMaxDataLen.Text = str; tsCmd.Text = "接收完成"; timerRepeat.Enabled = false; repeattimes = 0; } else { ; } txtRecvChar.Text += strRcvChar; txtRecvHex.Text += strRcvHex + "\r\n"; } catch (System.Exception ex) { MessageBox.Show(ex.Message, "出错提示"); } } }