时间:2015-1-13来源:不详作者:佚名
收藏    我要投稿

本实例演示定义委托,并利用委托把来自串口接收到的数据显示在文本框中!熟悉委托的定义和串行数据收发的简单功能!

 

 


 

项目代码:

 


using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { ///  /// QQ 458978 无名 c#开发技术 欢迎和我交流探讨 ///  public partial class Form1 : Form { ///  /// 定义委托 ///  ///public delegate void ShowString(string a); ///  /// 字符显示在文本框 ///  ///public void ShowTxt(string a) { this.textBox1.AppendText(DateTime.Now.ToString() + | + a + ); if (textBox1.TextLength > 2000) { textBox1.Clear(); } } ///  /// 定义委托并初始化 ///  ShowString AA; ///  /// 接收字符串存储 ///  string ReadStr = ; public Form1() { InitializeComponent(); serialPort1.Open(); AA = new ShowString(ShowTxt);//初始化委托 } //串口收到数据并回发 private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { ReadStr = serialPort1.ReadExisting(); byte[] ReadBuffer; ReadBuffer= System.Text.ASCIIEncoding.ASCII.GetBytes(ReadStr); this.Invoke(AA, ReadStr); serialPort1.Write(ReadBuffer, 0, ReadBuffer.Length); } } } 





 

运行效果图片:

 

 

 



转载请注明原文网址:http://www.helimiaopu.com/hjpz/hjpz/27.html
------分隔线----------------------------