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 
 { 
     public 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; 
         /// <summary> 
         /// 必需的设计器变量。 
         /// </summary> 
         private System.ComponentModel.IContainer components = null; 
         /// <summary> 
         /// 清理所有正在使用的资源。 
         /// </summary> 
         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 
         protected override void Dispose(bool disposing) 
         { 
             if (disposing && (components != null)) 
             { 
                 components.Dispose(); 
             } 
             base.Dispose(disposing); 
         } 
         #region Windows 窗体设计器生成的代码 
         /// <summary> 
         /// 设计器支持所需的方法 - 不要 
         /// 使用代码编辑器修改此方法的内容。 
         /// </summary> 
         private void 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(); 
             // 
             // button1 
             // 
             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.button1_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, 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.Size(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.AutoScaleDimensions = 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.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"; 
             serialPort1.BaudRate = 19200; 
             serialPort1.Parity = Parity.Odd; 
             serialPort1.StopBits = StopBits.Two; 
             //上述步骤可以用在实例化时调用SerialPort类的重载构造函数 
             //SerialPort serialPort = new SerialPort("COM1", 19200, Parity.Odd, StopBits.Two); 
             //打开串口(打开串口后不能修改端口名,波特率等参数,修改参数要在串口关闭后修改) 
             serialPort1.Open(); 
             //发送数据 
             SendStringData(serialPort1); 
             //也可用字节的形式发送数据 
             //SendBytesData(serialPort1); 
              
             //开启接收数据线程 
             ReceiveData(serialPort1); 
              
         } 
          
         //发送字符串数据 
         private void SendStringData(SerialPort serialPort) 
         { 
             serialPort.Write(txtSend.Text); 
         } 
         /// <summary> 
         /// 开启接收数据线程 
         /// </summary> 
         private void ReceiveData(SerialPort serialPort) 
         { 
             //同步阻塞接收数据线程 
             Thread threadReceive=new Thread(new ParameterizedThreadStart(SynReceiveData)); 
             threadReceive.Start(serialPort); 
              
              //也可用异步接收数据线程 
             //Thread threadReceiveSub = new Thread(new ParameterizedThreadStart(AsyReceiveData)); 
             //threadReceiveSub.Start(serialPort); 
              
         } 
         //发送二进制数据 
         private void SendBytesData(SerialPort serialPort) 
         { 
             byte[] bytesSend=System.Text.Encoding.Default.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.ToByte(serialPort.ReadByte()); 
                 int bytesRead=serialPort.BytesToRead ;                
                 byte[] bytesData=new byte[bytesRead+1]; 
                 bytesData[0] = 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) 
             { 
                 MessageBox.Show(e.Message); 
                 //处理错误 
             } 
             serialPort.Close(); 
         } 
     } 
  
     static class Program 
     { 
         /// <summary> 
         /// 应用程序的主入口点。 
         /// </summary> 
         [STAThread] 
         static void Main() 
         { 
             Application.EnableVisualStyles(); 
             Application.SetCompatibleTextRenderingDefault(false); 
             Application.Run(new frmTESTSerialPort()); 
         } 
     } 
 }

时间: 2024-11-06 16:50:13

SerialPort的相关文章

[C#] SerialPort用法

如今各种高速接口层出不穷,如USB2.0.USB3.0.USB3.1以及Thunderbold等等,但是简单低速串行接口依然在嵌入式软硬件开发中作为debug信息输出所采用. 本文将介绍如何在PC端用C# .Net 来开发串口应用程序. 从Microsoft .Net 2.0版本以后,就默认提供了System.IO.Ports.SerialPort类,用户可以非常简单地编写少量代码就完成串口的信息收发程序. 1. 串口硬件信号定义 2. 串口端口号搜索 3. 串口属性参数设置 4. 串口发送信息

SerialPort串口多个窗体访问

串口在每个窗体打开关闭容易出现访问被拒绝情况,这个时候只能重启电脑重置解决. 所以比较好的方式是使用母窗体打开串口,然后子窗体调用,不会出现上诉问题. 调用方法: 1.母窗体 //打开SerialPort即可 private void button2_Click(object sender, EventArgs e) { serialPort1.BaudRate = 9600; serialPort1.Parity = System.IO.Ports.Parity.None; serialPor

如何通过SerialPort读取和写入设备COM端口数据

SerialPort类用于控制串行端口文件资源.提供同步 I/O 和事件驱动的 I/O.对管脚和中断状态的访问以及对串行驱动程序属性的访问.另外,SerialPort的功能可以包装在内部 Stream 对象中,可通过 BaseStream 属性访问,并且可以传递给包装或使用流的类. 下面本文将如何通过实现COM端口配置.SerialPort调用配置打开端口.对设备端口进行读取操作. 1.        实现COM端口配置 COM端口主要配置有:COM端口名称.波特率.数据位数.停止位.奇偶校验及

vc 使用了SerialPort类的串口通信软件分析

实现串口通信,使用的类文件是SerialPort.cpp.在项目中使用mscomm控件的时候,串口连续传递若干数据后,会出现卡死的情况,关闭串口再打开,继续读取的话可以正常通信. 为了解决这个问题,想到就用SerialPort串口类来实现会好吧.当然,完全用windows的api函数来实现也可以,太麻烦吧,我也没用过.用微软的一些控件编程虽然容易了,但是也不熟悉底层. 软件主界面为: 点了自动发送单选框后: 点开始发送按钮: 一个界面就知道很好实现. 借这个例子,重点来梳理一下串口类的使用. 1

(c#2.0)serialPort串口通讯

原文:(c#2.0)serialPort串口通讯 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Configuration; usi

【笔记】SerialPort类的实现

class CSerialPort { public: // contruction and destruction CSerialPort(); virtual ~CSerialPort(); // port initialisation BOOL InitPort(CWnd* pPortOwner, UINT portnr = 1, UINT baud = 19200, char parity = 'N', UINT databits = 8, UINT stopsbits = 1, DWO

串行通讯之.NET SerialPort异步写数据

目 录 第1章 说明    2 1 为什么需要异步写数据?    2 2 异步写数据的代码    2 3 源代码    4 第1章 说明 1 为什么需要异步写数据? 如下图所示,以波特率300打开一个串口. 图1 单击"同步发送"按钮,则数据未发送完之前写数据的函数不会返回.波特率300,每秒大概能发送25个字符,发送500个字符就需要20秒.这20秒之内,整个程序将处于假死状态. 单击"异步发送"按钮,就不会出现假死状态. 2 异步写数据的代码 异步写数据的代码如

MFC串口的编程 mscomm控件与SerialPort类

MFC制作上位机,首先需要了解的是串口的编程,一般有两种方法,一个是使用ActiveX控件,例如mscomm串口控件,还有一个是用SerialPort类或者一些其他的串口类,这两个的区别是使用SerialPort类不需要注册控件,在其他没有安装控件的电脑上也能够用. 一·使用mscomm串口控件 使用mscomm串口控件的方法网上一大堆,大致说一些方法和一些需要注意的地方.如果是使用VC6.0在WIN7上来编写就会有个问题会通常说添加控件的方法为选中项目à“工程”à“添加到工程”à“Compon

Java SerialPort SDK

SerialPort SDK is a professional java serial port SDK,provides simple communication interface to connect to any serial port device. You can use this program for implementing, debuging serial protocol. You cansend and capture data. You can change comm