C#结合串口通信类实现串口通信源代码Word下载.docx
《C#结合串口通信类实现串口通信源代码Word下载.docx》由会员分享,可在线阅读,更多相关《C#结合串口通信类实现串口通信源代码Word下载.docx(10页珍藏版)》请在冰豆网上搜索。
privateSystem.Windows.Forms.Buttonbutton4;
privateSystem.Windows.Forms.TextBoxtextBox8;
privateSystem.Windows.Forms.Labellabel7;
publicintiPort=1;
//1,2,3,4
publicintiRate=9600;
//1200,2400,4800,9600
publicbytebSize=8;
//8bits
publicbytebParity=0;
//0-4=no,odd,even,mark,space
publicbytebStopBits=1;
//0,1,2=1,1.5,2
publicintiTimeout=1000;
publicmycommycom1=newmycom();
publicbyte[]recb;
privateSystem.Windows.Forms.TextBoxmsg;
privateSystem.Windows.Forms.TextBoxt_port;
privateSystem.Windows.Forms.TextBoxt_rate;
privateSystem.Windows.Forms.TextBoxt_bytesize;
privateSystem.Windows.Forms.TextBoxt_stopbyte;
privateSystem.Windows.Forms.TextBoxt_parity;
privateSystem.Windows.Forms.TextBoxt_send;
privateSystem.Windows.Forms.Buttonbutton5;
//readTimeOut
///必需的设计器变量。
privateSystem.ComponentModel.Containercomponents=null;
publicForm1()
InitializeComponent();
}
///清理所有正在使用的资源。
protectedoverridevoidDispose(booldisposing)
if(disposing)
if(components!
=null)
components.Dispose();
base.Dispose(disposing);
#regionWindows窗体设计器生成的代码
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
privatevoidInitializeComponent()
this.msg=newSystem.Windows.Forms.TextBox();
this.label1=newSystem.Windows.Forms.Label();
this.label2=newSystem.Windows.Forms.Label();
this.t_send=newSystem.Windows.Forms.TextBox();
this.button1=newSystem.Windows.Forms.Button();
this.groupBox1=newSystem.Windows.Forms.GroupBox();
this.button2=newSystem.Windows.Forms.Button();
this.t_port=newSystem.Windows.Forms.TextBox();
this.label3=newSystem.Windows.Forms.Label();
this.t_rate=newSystem.Windows.Forms.TextBox();
this.label4=newSystem.Windows.Forms.Label();
this.t_bytesize=newSystem.Windows.Forms.TextBox();
this.label5=newSystem.Windows.Forms.Label();
this.t_stopbyte=newSystem.Windows.Forms.TextBox();
this.label6=newSystem.Windows.Forms.Label();
this.t_parity=newSystem.Windows.Forms.TextBox();
this.button3=newSystem.Windows.Forms.Button();
this.button4=newSystem.Windows.Forms.Button();
this.textBox8=newSystem.Windows.Forms.TextBox();
this.label7=newSystem.Windows.Forms.Label();
this.button5=newSystem.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
//msg
this.msg.ForeColor=System.Drawing.Color.Green;
this.msg.Location=newSystem.Drawing.Point(0,0);
this.msg.Multiline=true;
this.msg.Name="
msg"
;
this.msg.ScrollBars=System.Windows.Forms.ScrollBars.Vertical;
this.msg.Size=newSystem.Drawing.Size(512,264);
this.msg.TabIndex=0;
this.msg.Text="
"
//label1
this.label1.Location=newSystem.Drawing.Point(16,24);
this.label1.Name="
label1"
this.label1.Size=newSystem.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=newSystem.Drawing.Point(8,280);
this.label2.Name="
label2"
this.label2.Size=newSystem.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)));
this.t_send.Location=newSystem.Drawing.Point(80,272);
this.t_send.Name="
t_send"
this.t_send.Size=newSystem.Drawing.Size(344,21);
this.t_send.TabIndex=2;
this.t_send.Text="
//button1
this.button1.Location=newSystem.Drawing.Point(432,272);
this.button1.Name="
button1"
this.button1.Size=newSystem.Drawing.Size(40,23);
this.button1.TabIndex=3;
this.button1.Text="
发送"
this.button1.Click+=newSystem.EventHandler(this.button1_Click);
//groupBox1
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.t_port);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.t_rate);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.t_bytesize);
this.groupBox1.Controls.Add(this.label5);
this.groupBox1.Controls.Add(this.t_stopbyte);
this.groupBox1.Controls.Add(this.label6);
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=newSystem.Drawing.Point(8,304);
this.groupBox1.Name="
groupBox1"
this.groupBox1.Size=newSystem.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=newSystem.Drawing.Point(80,184);
this.button2.Name="
button2"
this.button2.TabIndex=3;
this.button2.Text="
应用设置"
this.button2.Click+=newSystem.EventHandler(this.button2_Click);
//t_port
this.t_port.BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;
this.t_port.Location=newSystem.Drawing.Point(80,16);
this.t_port.Name="
t_port"
this.t_port.Size=newSystem.Drawing.Size(80,21);
this.t_port.TabIndex=2;
this.t_port.Text="
1"
//label3
this.label3.Location=newSystem.Drawing.Point(16,58);
this.label3.Name="
label3"
this.label3.Size=newSystem.Drawing.Size(56,16);
this.label3.TabIndex=1;
this.label3.Text="
波特率:
//t_rate
this.t_rate.BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;
this.t_rate.Location=newSystem.Drawing.Point(80,50);
this.t_rate.Name="
t_rate"
this.t_rate.Size=newSystem.Drawing.Size(80,21);
this.t_rate.TabIndex=2;
this.t_rate.Text="
9600"
//label4
this.label4.Location=newSystem.Drawing.Point(16,92);
this.label4.Name="
label4"
this.label4.Size=newSystem.Drawing.Size(56,16);
this.label4.TabIndex=1;
this.label4.Text="
数据位:
//t_bytesize
this.t_bytesize.BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;
this.t_bytesize.Location=newSystem.Drawing.Point(80,84);
this.t_bytesize.Name="
t_bytesize"
this.t_bytesize.Size=newSystem.Drawing.Size(80,21);
this.t_bytesize.TabIndex=2;
this.t_bytesize.Text="
8"
//label5
this.label5.Location=newSystem.Drawing.Point(16,126);
this.label5.Name="
label5"
this.label5.Size=newSystem.Drawing.Size(56,16);
this.label5.TabIndex=1;
this.label5.Text="
停止位:
//t_stopbyte
this.t_stopbyte.BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;
this.t_stopbyte.Location=newSystem.Drawing.Point(80,118);
this.t_stopbyte.Name="
t_stopbyte"
this.t_stopbyte.Size=newSystem.Drawing.Size(80,21);
this.t_stopbyte.TabIndex=2;
this.t_stopbyte.Text="
//label6
this.label6.Location=newSystem.Drawing.Point(16,160);
this.label6.Name="
label6"
this.label6.Size=newSystem.Drawing.Size(56,16);
this.label6.TabIndex=1;
this.label6.Text="
校验位:
//t_parity
this.t_parity.BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;
this.t_parity.Enabled=false;
this.t_parity.Location=newSystem.Drawing.Point(80,152);
this.t_parity.Name="
t_parity"
this.t_parity.Size=newSystem.Drawing.Size(80,21);
this.t_parity.TabIndex=2;
this.t_parity.Text="
0"
//button3
this.button3.Location=newSystem.Drawing.Point(472,272);
this.button3.Name="
button3"
this.button3.Size=newSystem.Drawing.Size(40,23);
this.button3.TabIndex=3;
this.button3.Text="
清空"
this.button3.Click+=newSystem.EventHandler(this.button3_Click);
//button4
this.button4.FlatStyle=System.Windows.Forms.FlatStyle.Popup;
this.button4.Location=newSystem.Drawing.Point(432,312);
this.button4.Name="
button4"
this.button4.Size=newSystem.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=newSystem.Drawing.Point(288,312);
this.textBox8.Name="
textBox8"
this.textBox8.Size=newSystem.Drawing.Size(136,21);
this.textBox8.TabIndex=7;
this.textBox8.Text="
//label7
this.label7.Location=newSystem.Drawing.Point(200,320);
this.label7.Name="
label7"
this.label7.Size=newSystem.Drawing.Size(100,16);
this.label7.TabIndex=8;
this.label7.Text="
设置本机地址:
//button5
this.button5.FlatStyle=System.Windows.Forms.FlatStyle.Popup;
this.button5.Location=newSystem.Drawing.Point(440,504);
this.button5.Name="
button5"
this.button5.Size=newSystem.Drawing.Size(64,23);
this.button5.TabIndex=9;
this.button5.Text="
关闭串口"
this.button5.Click+=newSystem.EventHandler(this.button5_Click);
//Form1
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.ClientSize=newSystem.Drawing.Size(512,533);
this.Controls.Add(this.button5);
this.Controls.Add(this.textBox8);
this.Controls.Add(this.label7);
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(this.label2);
this.Controls.Add(this.button3);
this.Name="
Form1"
this.T