コード例 #1
0
 private void addNowLine(object sender, EventArgs e)
 {
     if (ShowRecTime.Checked)
     {
         RecBuff.AppendText("\n");
     }
 }
コード例 #2
0
        private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            //计数
            int num = sp.BytesToRead;

            RxCounter += num;
            string ShowBuff = "";

            //接收数据
            byte[] recBuff = new byte[num];
            sp.Read(recBuff, 0, num);
            sb.Clear();


            //转换十六进制
            if (RecHEX.Checked)
            {
                //ShowBuff = recBuff.ToString("X2");
                foreach (int b in recBuff)
                {
                    ShowBuff += b.ToString("X2") + " ";
                }
            }
            else
            {
                ShowBuff = Encoding.ASCII.GetString(recBuff);
            }

            //显示接收时间
            if (ShowRecTime.Checked)
            {
                current_time = DateTime.Now;
                ShowBuff     = current_time.ToString("[HH:mm:ss.fff]") + " \n" + ShowBuff + "\n";
            }

            //自动转行
            if (addNewlineReceved.Checked)
            {
                ShowBuff += "\n";
            }


            try
            {
                this.Invoke((EventHandler)(delegate
                {
                    RxCountShow.Text = string.Format("RX: {0} Bytes", RxCounter); //更新接收字节数
                    RecBuff.AppendText(ShowBuff);                                 //更新显示内容
                }
                                           )
                            );

                // RecBuff.AppendText(sp.ReadExisting());
            } catch (Exception ex)
            {
                System.Media.SystemSounds.Beep.Play();
                MessageBox.Show(ex.Message);
                Message.Text = ex.Message;
            }
        }