private void OnDataReceived(object sender, SerialDataReceivedEventArgs e) { byte[] buffer = new byte[1024]; int b = 0; try { //循环接收数据 while (serialport.BytesToRead > 0) { b = serialport.Read(buffer, 0, buffer.Length); } //在这里对接收到的数据进行处理 // richTextBoxCallBack callback = delegate() { string str = Encoding.GetEncoding(setchar).GetString(buffer, 0, b);; this.richTextBox1.Text += str; }; richTextBox1.Invoke(callback); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); } }
public void SetText(string sInput, Color wordColor) { richTextBoxCallBack callback = delegate()//使用委托 { richTextBox1.SelectionColor = wordColor; richTextBox1.AppendText(sInput); richTextBox1.SelectionStart = richTextBox1.Text.Length; richTextBox1.ScrollToCaret(); }; richTextBox1.Invoke(callback); }