使用SerialPort控件用类及线程实现串口通信_第1页
使用SerialPort控件用类及线程实现串口通信_第2页
使用SerialPort控件用类及线程实现串口通信_第3页
使用SerialPort控件用类及线程实现串口通信_第4页
使用SerialPort控件用类及线程实现串口通信_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C#使用SerialPort控件用类及线程实现串口通信C# 使用SerialPort 源码实例编辑源码复制到剪贴板打印using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO.Ports; using System.Threading; namespace TestSerialPort publ

2、ic partial class frmTESTSerialPort : Form public frmTESTSerialPort() InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; private Button button1; private TextBox txtSend; private TextBox txtReceive; private Label label1; private Label label2; / / 必需的设计器变量。 / private System.Compone

3、ntModel.IContainer components = null; / / 清理所有正在使用的资源。 / / 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) if (disposing & (components != null) components.Dispose(); base.Dispose(disposing); #region Windows 窗体设计器生成的代码 / / 设计器支持所需的方法 - 不要 / 使用代码编辑器修改此方法的内容。 / private void

4、InitializeComponent() this.button1 = new System.Windows.Forms.Button(); this.txtSend = new System.Windows.Forms.TextBox(); this.txtReceive = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.SuspendLayout(); / / b

5、utton1 / this.button1.Location = new System.Drawing.Point(440, 379); this.button1.Name = button1; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = 发送; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.bu

6、tton1_Click); / / txtSend / this.txtSend.Location = new System.Drawing.Point(59, 12); this.txtSend.Multiline = true; this.txtSend.Name = txtSend; this.txtSend.Size = new System.Drawing.Size(456, 164); this.txtSend.TabIndex = 2; / / txtReceive / this.txtReceive.Location = new System.Drawing.Point(59,

7、 200); this.txtReceive.Multiline = true; this.txtReceive.Name = txtReceive; this.txtReceive.Size = new System.Drawing.Size(456, 164); this.txtReceive.TabIndex = 2; / / label1 / this.label1.Location = new System.Drawing.Point(13, 15); this.label1.Name = label1; this.label1.Size = new System.Drawing.S

8、ize(41, 12); this.label1.TabIndex = 0; this.label1.Text = 发送; / / label2 / this.label2.Location = new System.Drawing.Point(13, 213); this.label2.Name = label2; this.label2.Size = new System.Drawing.Size(41, 12); this.label2.TabIndex = 0; this.label2.Text = 接收; / / frmTESTSerialPort / this.AutoScaleD

9、imensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(546, 434); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.txtReceive); this.Controls.Add(this.txtSend); this.

10、Controls.Add(this.button1); this.Name = frmTESTSerialPort; this.Text = 串口试验; this.ResumeLayout(false); this.PerformLayout(); #endregion private void button1_Click(object sender, EventArgs e) /实例化串口对象(默认:COMM1,9600,e,8,1) SerialPort serialPort1 = new SerialPort(); /更改参数 serialPort1.PortName = COM1; s

11、erialPort1.BaudRate = 19200; serialPort1.Parity = Parity.Odd; serialPort1.StopBits = StopBits.Two; /上述步骤可以用在实例化时调用SerialPort类的重载构造函数 /SerialPort serialPort = new SerialPort(COM1, 19200, Parity.Odd, StopBits.Two); /打开串口(打开串口后不能修改端口名,波特率等参数,修改参数要在串口关闭后修改) serialPort1.Open(); /发送数据 SendStringData(seria

12、lPort1); /也可用字节的形式发送数据 /SendBytesData(serialPort1); /开启接收数据线程 ReceiveData(serialPort1); /发送字符串数据 private void SendStringData(SerialPort serialPort) serialPort.Write(txtSend.Text); / / 开启接收数据线程 / private void ReceiveData(SerialPort serialPort) /同步阻塞接收数据线程 Thread threadReceive=new Thread(new Parameter

13、izedThreadStart(SynReceiveData); threadReceive.Start(serialPort); /也可用异步接收数据线程 /Thread threadReceiveSub = new Thread(new ParameterizedThreadStart(AsyReceiveData); /threadReceiveSub.Start(serialPort); /发送二进制数据 private void SendBytesData(SerialPort serialPort) byte bytesSend=System.Text.Encoding.Defau

14、lt.GetBytes(txtSend.Text ); serialPort.Write(bytesSend, 0, bytesSend.Length); /同步阻塞读取 private void SynReceiveData(object serialPortobj) SerialPort serialPort = (SerialPort)serialPortobj; System.Threading.Thread.Sleep(0); serialPort.ReadTimeout = 1000; try /阻塞到读取数据或超时(这里为2秒) byte firstByte=Convert.To

15、Byte(serialPort.ReadByte(); int bytesRead=serialPort.BytesToRead ; byte bytesData=new bytebytesRead+1; bytesData0 = firstByte; for (int i = 1; i =bytesRead; i+) bytesData = Convert.ToByte( serialPort.ReadByte(); txtReceive.Text = System.Text.Encoding.Default.GetString(bytesData); catch(Exception e) MessageBox.Show(e.Message); /处理超时错误 serialPort.Close(); /异步读取 private void AsyReceiveData(object serialPortobj) SerialPort serialPort = (SerialPort)serialPortobj; System.Threading.Thread.Sleep(500); try txtReceive.Text = serialPort.ReadExisting(); catch (Exception e)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论