c#结合串口通信类实现串口通信源代码_c#应用
2008-02-23 05:43:44来源:互联网 阅读 ()
mycom是串口通信类,在本blog前一篇中有完整代码。
下面是串口通讯测试程式的源代码,将mycom类放在此项目中
能够实现简单的串口通信,希望读者能通过这个程式对串口通信过程有一个初步的了解:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace BusApp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.TextBox textBox8;
private System.Windows.Forms.Label label7;
public int iPort=1; //1,2,3,4
public int iRate=9600; //1200,2400,4800,9600
public byte bSize=8; //8 bits
public byte bParity=0; // 0-4=no,odd,even,mark,space
public byte bStopBits=1; // 0,1,2 = 1, 1.5, 2
public int iTimeout=1000;
public mycom mycom1=new mycom();
public byte[] recb;
private System.Windows.Forms.TextBox msg;
private System.Windows.Forms.TextBox t_port;
private System.Windows.Forms.TextBox t_rate;
private System.Windows.Forms.TextBox t_bytesize;
private System.Windows.Forms.TextBox t_stopbyte;
private System.Windows.Forms.TextBox t_parity;
private System.Windows.Forms.TextBox t_send;
private System.Windows.Forms.Button button5; //readTimeOut
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 清理任何正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.msg = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.t_send = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.t_port = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.t_rate = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.t_bytesize = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.t_stopbyte = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.t_parity = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.textBox8 = new System.Windows.Forms.TextBox();
this.label7 = new System.Windows.Forms.Label();
this.button5 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// msg
//
this.msg.ForeColor = System.Drawing.Color.Green;
this.msg.Location = new System.Drawing.Point(0, 0);
this.msg.Multiline = true;
this.msg.Name = "msg";
this.msg.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.msg.Size = new System.Drawing.Size(512, 264);
this.msg.TabIndex = 0;
this.msg.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.TabIndex = 1;
this.label1.Text = "串口号:";
//
// label2
//
this.label2.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(64)), ((System.Byte)(0)));
this.label2.Location = new System.Drawing.Point(8, 280);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 16);
this.label2.TabIndex = 1;
this.label2.Text = "配置数据包:";
//
// t_send
//
this.t_send.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.t_send.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(0)));
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇: 如何获取当前操作系统的软件版本_c#应用
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash