当前位置:文档之家› 串口通讯测试程序的源代码

串口通讯测试程序的源代码

using System;using System.Drawing;using System.Collections;using ponentModel;using System.Windows.Forms;using System.Data;using System.Threading;namespace BusApp{/// <summary>/// Form1 的摘要说明。

/// </summary>public class Form1 : System.Windows.Forms.Form{private bel label1;private bel label2;private System.Windows.Forms.Button button1;private System.Windows.Forms.GroupBox groupBox1;private bel label3;private bel label4;private bel label5;private bel 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 bel label7;public int iPort=1; //1,2,3,4public int iRate=9600; //1200,2400,4800,9600public byte bSize=8; //8 bitspublic byte bParity=0; // 0-4=no,odd,even,mark,spacepublic byte bStopBits=1; // 0,1,2 = 1, 1.5, 2public 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 ponentModel.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();bel1 = new bel();bel2 = new bel();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();bel3 = new bel();this.t_rate = new System.Windows.Forms.TextBox();bel4 = new bel();this.t_bytesize = new System.Windows.Forms.TextBox();bel5 = new bel();this.t_stopbyte = new System.Windows.Forms.TextBox();bel6 = new bel();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();bel7 = new bel();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; = "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//bel1.Location = new System.Drawing.Point(16, 24); = "label1";bel1.Size = new System.Drawing.Size(56, 16);bel1.TabIndex = 1;bel1.Text = "串口号:";//// label2//bel2.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(64)), ((System.Byte)(0)));bel2.Location = new System.Drawing.Point(8, 280); = "label2";bel2.Size = new System.Drawing.Size(80, 16);bel2.TabIndex = 1;bel2.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)));this.t_send.Location = new System.Drawing.Point(80, 272);this.t_ = "t_send";this.t_send.Size = new System.Drawing.Size(344, 21);this.t_send.TabIndex = 2;this.t_send.Text = "";//// button1//this.button1.Location = new System.Drawing.Point(432, 272); = "button1";this.button1.Size = new System.Drawing.Size(40, 23);this.button1.TabIndex = 3;this.button1.Text = "发送";this.button1.Click += new System.EventHandler(this.button1_Click);//// groupBox1//this.groupBox1.Controls.Add(this.button2);this.groupBox1.Controls.Add(this.t_port);this.groupBox1.Controls.Add(bel1);this.groupBox1.Controls.Add(bel3);this.groupBox1.Controls.Add(this.t_rate);this.groupBox1.Controls.Add(bel4);this.groupBox1.Controls.Add(this.t_bytesize);this.groupBox1.Controls.Add(bel5);this.groupBox1.Controls.Add(this.t_stopbyte);this.groupBox1.Controls.Add(bel6);this.groupBox1.Controls.Add(this.t_parity);this.groupBox1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(64)), ((System.Byte)(0)));this.groupBox1.Location = new System.Drawing.Point(8, 304); = "groupBox1";this.groupBox1.Size = new System.Drawing.Size(176, 216);this.groupBox1.TabIndex = 4;this.groupBox1.TabStop = false;this.groupBox1.Text = "参数设置";//// button2//this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;this.button2.Location = new System.Drawing.Point(80, 184); = "button2";this.button2.TabIndex = 3;this.button2.Text = "应用设置";this.button2.Click += new System.EventHandler(this.button2_Click);//// t_port//this.t_port.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;this.t_port.Location = new System.Drawing.Point(80, 16);this.t_ = "t_port";this.t_port.Size = new System.Drawing.Size(80, 21);this.t_port.TabIndex = 2;this.t_port.Text = "1";//// label3//bel3.Location = new System.Drawing.Point(16, 58); = "label3";bel3.Size = new System.Drawing.Size(56, 16);bel3.TabIndex = 1;bel3.Text = "波特率:";//// t_rate//this.t_rate.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;this.t_rate.Location = new System.Drawing.Point(80, 50);this.t_ = "t_rate";this.t_rate.Size = new System.Drawing.Size(80, 21);this.t_rate.TabIndex = 2;this.t_rate.Text = "9600";//// label4//bel4.Location = new System.Drawing.Point(16, 92); = "label4";bel4.Size = new System.Drawing.Size(56, 16);bel4.TabIndex = 1;bel4.Text = "数据位:";//// t_bytesize//this.t_bytesize.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.t_bytesize.Location = new System.Drawing.Point(80, 84);this.t_ = "t_bytesize";this.t_bytesize.Size = new System.Drawing.Size(80, 21);this.t_bytesize.TabIndex = 2;this.t_bytesize.Text = "8";//// label5bel5.Location = new System.Drawing.Point(16, 126); = "label5";bel5.Size = new System.Drawing.Size(56, 16);bel5.TabIndex = 1;bel5.Text = "停止位:";//// t_stopbyte//this.t_stopbyte.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.t_stopbyte.Location = new System.Drawing.Point(80, 118);this.t_ = "t_stopbyte";this.t_stopbyte.Size = new System.Drawing.Size(80, 21);this.t_stopbyte.TabIndex = 2;this.t_stopbyte.Text = "1";//// label6//bel6.Location = new System.Drawing.Point(16, 160); = "label6";bel6.Size = new System.Drawing.Size(56, 16);bel6.TabIndex = 1;bel6.Text = "校验位:";//// t_parity//this.t_parity.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.t_parity.Enabled = false;this.t_parity.Location = new System.Drawing.Point(80, 152);this.t_ = "t_parity";this.t_parity.Size = new System.Drawing.Size(80, 21);this.t_parity.TabIndex = 2;this.t_parity.Text = "0";//// button3//this.button3.Location = new System.Drawing.Point(472, 272); = "button3";this.button3.Size = new System.Drawing.Size(40, 23);this.button3.TabIndex = 3;this.button3.Text = "清空";this.button3.Click += new System.EventHandler(this.button3_Click);//// button4this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Popup;this.button4.Location = new System.Drawing.Point(432, 312); = "button4";this.button4.Size = new System.Drawing.Size(72, 23);this.button4.TabIndex = 6;this.button4.Text = "初始化";//// textBox8//this.textBox8.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;this.textBox8.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(0)));this.textBox8.Location = new System.Drawing.Point(288, 312); = "textBox8";this.textBox8.Size = new System.Drawing.Size(136, 21);this.textBox8.TabIndex = 7;this.textBox8.Text = "";//// label7//bel7.Location = new System.Drawing.Point(200, 320); = "label7";bel7.Size = new System.Drawing.Size(100, 16);bel7.TabIndex = 8;bel7.Text = "设置本机地址:";//// button5//this.button5.FlatStyle = System.Windows.Forms.FlatStyle.Popup;this.button5.Location = new System.Drawing.Point(440, 504); = "button5";this.button5.Size = new System.Drawing.Size(64, 23);this.button5.TabIndex = 9;this.button5.Text = "关闭串口";this.button5.Click += new System.EventHandler(this.button5_Click);//// Form1//this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);this.ClientSize = new System.Drawing.Size(512, 533);this.Controls.Add(this.button5);this.Controls.Add(this.textBox8);this.Controls.Add(bel7);this.Controls.Add(this.button4);this.Controls.Add(this.groupBox1);this.Controls.Add(this.button1);this.Controls.Add(this.t_send);this.Controls.Add(this.msg);this.Controls.Add(bel2);this.Controls.Add(this.button3); = "Form1";this.Text = "串口通讯(小y设计)";this.Closing += new ponentModel.CancelEventHandler(this.Form1_Closing);this.Load += new System.EventHandler(this.Form1_Load);this.groupBox1.ResumeLayout(false);this.ResumeLayout(false);}#endregion/// <summary>/// 应用程序的主入口点。

相关主题