C#串口监听的实现实例操作

来源:岁月联盟 编辑:zhu 时间:2009-08-26

C#串口监听的实现在 Visual Stdio 2005中,对于串口操作Framework提供了一个很好的类接口-SerialPort,在这当中,串口数据的读取与写入有较大的不同。C#串口监听的实现由于串口不知道数据何时到达,因此有两种方法可以实现C#串口监听之串口数据的读取。1.用线程实时读串口2.用事件触发方式实现。但由于线程实时读串口的效率不是十分高效,因此比较好的方法是事件触发的方式。在SerialPort类中有DataReceived事件,当串口的读缓存有数据到达时则触发DataReceived事件,其中SerialPort.ReceivedBytesThreshold属性决定了当串口读缓存中数据多少个时才触发DataReceived事件,默认为1。

此外,SerialPort.DataReceived事件运行比较特殊,其运行在辅线程,不能与主线程中的显示数据控件直接进行数据传输,必须用间接的方式实现。

C#串口监听实现一、创建WIndow项目,设计界面

C#串口监听界面 

C#串口监听实现二、编写实现代码

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.IO.Ports;  
  9. //using Microsoft.VisualBasic.Devices;  
  10.  
  11. //C#串口监听  
  12. namespace Demo  
  13. ...{  
  14. public partial class Form1 : Form  
  15. ...{  
  16. public Form1()  
  17. ...{  
  18. InitializeComponent();  
  19. }  
  20.  
  21. private SerialPort Sp = new SerialPort();  
  22. public delegate void HandleInterfaceUpdataDelegate(string text);  
  23. private HandleInterfaceUpdataDelegate interfaceUpdataHandle;  
  24.  
  25. private void Form1_Load(object sender, EventArgs e)  
  26. ...{  
  27. tbID.Focus();  
  28. BtPause.Enabled = false;  
  29. }  
  30.  
  31. private void UpdateTextBox(string text)  
  32. ...{  
  33. tbData.Text = text;  
  34. }  
  35.  
  36. public void Sp_DataReceived(object sender,  
  37. System.IO.Ports.SerialDataReceivedEventArgs e)  
  38. ...{  
  39. byte[] readBuffer = new byte[Sp.ReadBufferSize];  
  40. Sp.Read(readBuffer, 0, readBuffer.Length);  
  41. this.Invoke(interfaceUpdataHandle,  
  42.  new string[] ...{ Encoding.UTF8.GetString(readBuffer) });  
  43. }  
  44.  
  45. private void Form1_FormClosing(  
  46. object sender, FormClosingEventArgs e)  
  47. ...{  
  48. Sp.Close();  
  49. }  
  50.  
  51. private void btENT_Click(object sender, EventArgs e)  
  52. ...{  
  53. if ((tbID.Text.Trim() != "") &&   
  54. (cmRate.Text != ""))  
  55. ...{  
  56. interfaceUpdataHandle =   
  57. new HandleInterfaceUpdataDelegate(UpdateTextBox);  
  58. //C#串口监听之实例化委托对象  
  59. Sp.PortName = tbID.Text.Trim();  
  60. serialPort1.BaudRate =   
  61. Convert.ToInt32(cmRate.Text.Trim());  
  62. Sp.Parity = Parity.None;  
  63. Sp.StopBits = StopBits.One;  
  64. Sp.DataReceived +=   
  65. new SerialDataReceivedEventHandler(Sp_DataReceived);  
  66. Sp.ReceivedBytesThreshold = 1;  
  67. try 
  68. ...{  
  69. Sp.Open();  
  70. tbID.ReadOnly = true;  
  71. BtPause.Enabled = true;  
  72. btENT.Enabled = false;  
  73. }  
  74. catch 
  75. ...{  
  76. MessageBox.Show(  
  77. "端口" + tbID.Text.Trim() + "打开失败!");  
  78. }  
  79. }//C#串口监听  
  80. else 
  81. ...{  
  82. MessageBox.Show("请输入正确的端口号和波特率!");  
  83. tbID.Focus();  
  84. }  
  85. }  
  86.  
  87. private void BtPause_Click(  
  88. object sender, EventArgs e)  
  89. ...{  
  90. Sp.Close();  
  91. tbID.ReadOnly = false;  
  92. btENT.Enabled = true;  
  93. BtPause.Enabled = false;  
  94. }  
  95. }//C#串口监听  
  96. }  

C#串口监听具体的实现操作就向你介绍到这里,希望对你了解和学习C#串口监听的实现有所帮助。