c# com口通讯(1)- 界面代码

时间:2022-08-30 23:19:06

效果:

c# com口通讯(1)- 界面代码

代码:

 

        private void InitializeComponent()
{
this.comboPortName = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.comboBaudrate = new System.Windows.Forms.ComboBox();
this.buttonOpenClose = new System.Windows.Forms.Button();
this.labelGetCount = new System.Windows.Forms.Label();
this.labelSendCount = new System.Windows.Forms.Label();
this.buttonReset = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.txGet = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.btnCrc = new System.Windows.Forms.Button();
this.DropCRC = new System.Windows.Forms.ComboBox();
this.label4 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.buttonSend = new System.Windows.Forms.Button();
this.checkBoxHexSend = new System.Windows.Forms.CheckBox();
this.txSend = new System.Windows.Forms.TextBox();
this.checkBoxNewlineSend = new System.Windows.Forms.CheckBox();
this.checkBoxHexView = new System.Windows.Forms.CheckBox();
this.checkBoxNewlineGet = new System.Windows.Forms.CheckBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// comboPortName
//
this.comboPortName.BackColor = System.Drawing.Color.White;
this.comboPortName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboPortName.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.comboPortName.FormattingEnabled = true;
this.comboPortName.Location = new System.Drawing.Point(78, 11);
this.comboPortName.Name = "comboPortName";
this.comboPortName.Size = new System.Drawing.Size(121, 20);
this.comboPortName.TabIndex = 4;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(13, 15);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(53, 12);
this.label1.TabIndex = 2;
this.label1.Text = "串口名称";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(205, 15);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 12);
this.label2.TabIndex = 3;
this.label2.Text = "波特率";
//
// comboBaudrate
//
this.comboBaudrate.BackColor = System.Drawing.Color.White;
this.comboBaudrate.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBaudrate.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.comboBaudrate.FormattingEnabled = true;
this.comboBaudrate.Items.AddRange(new object[] {
"2400",
"4800",
"9600",
"19200",
"38400",
"57600",
"115200"});
this.comboBaudrate.Location = new System.Drawing.Point(264, 11);
this.comboBaudrate.Name = "comboBaudrate";
this.comboBaudrate.Size = new System.Drawing.Size(121, 20);
this.comboBaudrate.TabIndex = 5;
//
// buttonOpenClose
//
this.buttonOpenClose.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.buttonOpenClose.Font = new System.Drawing.Font("宋体", 9F);
this.buttonOpenClose.ForeColor = System.Drawing.Color.Black;
this.buttonOpenClose.Location = new System.Drawing.Point(391, 10);
this.buttonOpenClose.Name = "buttonOpenClose";
this.buttonOpenClose.Size = new System.Drawing.Size(75, 23);
this.buttonOpenClose.TabIndex = 0;
this.buttonOpenClose.Text = "打开串口";
this.buttonOpenClose.UseVisualStyleBackColor = true;
this.buttonOpenClose.Click += new System.EventHandler(this.buttonOpenClose_Click);
//
// labelGetCount
//
this.labelGetCount.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.labelGetCount.AutoSize = true;
this.labelGetCount.Font = new System.Drawing.Font("宋体", 9F);
this.labelGetCount.Location = new System.Drawing.Point(433, 0);
this.labelGetCount.Name = "labelGetCount";
this.labelGetCount.Size = new System.Drawing.Size(107, 12);
this.labelGetCount.TabIndex = 9;
this.labelGetCount.Text = "累计接收字节数::0";
//
// labelSendCount
//
this.labelSendCount.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.labelSendCount.AutoSize = true;
this.labelSendCount.Font = new System.Drawing.Font("宋体", 9F);
this.labelSendCount.Location = new System.Drawing.Point(433, 0);
this.labelSendCount.Name = "labelSendCount";
this.labelSendCount.Size = new System.Drawing.Size(101, 12);
this.labelSendCount.TabIndex = 13;
this.labelSendCount.Text = "累加发送字节数:0";
//
// buttonReset
//
this.buttonReset.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonReset.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.buttonReset.Font = new System.Drawing.Font("宋体", 9F);
this.buttonReset.ForeColor = System.Drawing.Color.Black;
this.buttonReset.Location = new System.Drawing.Point(472, 10);
this.buttonReset.Name = "buttonReset";
this.buttonReset.Size = new System.Drawing.Size(89, 23);
this.buttonReset.TabIndex = 1;
this.buttonReset.Text = "清空发送记录";
this.buttonReset.UseVisualStyleBackColor = true;
this.buttonReset.Click += new System.EventHandler(this.buttonReset_Click);
//
// groupBox1
//
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox1.Controls.Add(this.txGet);
this.groupBox1.Controls.Add(this.labelGetCount);
this.groupBox1.Location = new System.Drawing.Point(15, 37);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(546, 313);
this.groupBox1.TabIndex = 6;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "接受数据:";
//
// txGet
//
this.txGet.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txGet.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txGet.Location = new System.Drawing.Point(7, 20);
this.txGet.Multiline = true;
this.txGet.Name = "txGet";
this.txGet.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txGet.Size = new System.Drawing.Size(533, 265);
this.txGet.TabIndex = 0;
//
// groupBox2
//
this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox2.Controls.Add(this.btnCrc);
this.groupBox2.Controls.Add(this.DropCRC);
this.groupBox2.Controls.Add(this.label4);
this.groupBox2.Controls.Add(this.label3);
this.groupBox2.Controls.Add(this.buttonSend);
this.groupBox2.Controls.Add(this.checkBoxHexSend);
this.groupBox2.Controls.Add(this.labelSendCount);
this.groupBox2.Controls.Add(this.txSend);
this.groupBox2.Controls.Add(this.checkBoxNewlineSend);
this.groupBox2.Location = new System.Drawing.Point(15, 341);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(546, 103);
this.groupBox2.TabIndex = 10;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "数据发送: ";
//
// btnCrc
//
this.btnCrc.Location = new System.Drawing.Point(78, 80);
this.btnCrc.Name = "btnCrc";
this.btnCrc.Size = new System.Drawing.Size(75, 23);
this.btnCrc.TabIndex = 17;
this.btnCrc.Text = "校验";
this.btnCrc.UseVisualStyleBackColor = true;
this.btnCrc.Click += new System.EventHandler(this.btnCrc_Click);
//
// DropCRC
//
this.DropCRC.FormattingEnabled = true;
this.DropCRC.Items.AddRange(new object[] {
"16 CRC ,低字节在前"});
this.DropCRC.Location = new System.Drawing.Point(78, 49);
this.DropCRC.Name = "DropCRC";
this.DropCRC.Size = new System.Drawing.Size(442, 20);
this.DropCRC.TabIndex = 16;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(31, 53);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(41, 12);
this.label4.TabIndex = 15;
this.label4.Text = "校验:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(19, 24);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(53, 12);
this.label3.TabIndex = 14;
this.label3.Text = "发送区:";
//
// buttonSend
//
this.buttonSend.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.buttonSend.Font = new System.Drawing.Font("宋体", 9F);
this.buttonSend.ForeColor = System.Drawing.Color.Black;
this.buttonSend.Location = new System.Drawing.Point(163, 80);
this.buttonSend.Name = "buttonSend";
this.buttonSend.Size = new System.Drawing.Size(75, 23);
this.buttonSend.TabIndex = 0;
this.buttonSend.Text = "发送";
this.buttonSend.UseVisualStyleBackColor = true;
this.buttonSend.Click += new System.EventHandler(this.buttonSend_Click);
//
// checkBoxHexSend
//
this.checkBoxHexSend.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.checkBoxHexSend.AutoSize = true;
this.checkBoxHexSend.Checked = true;
this.checkBoxHexSend.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBoxHexSend.Location = new System.Drawing.Point(78, 0);
this.checkBoxHexSend.Name = "checkBoxHexSend";
this.checkBoxHexSend.Size = new System.Drawing.Size(60, 16);
this.checkBoxHexSend.TabIndex = 11;
this.checkBoxHexSend.Text = "16进制";
this.checkBoxHexSend.UseVisualStyleBackColor = true;
//
// txSend
//
this.txSend.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txSend.Location = new System.Drawing.Point(78, 22);
this.txSend.Name = "txSend";
this.txSend.Size = new System.Drawing.Size(442, 21);
this.txSend.TabIndex = 1;
//
// checkBoxNewlineSend
//
this.checkBoxNewlineSend.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.checkBoxNewlineSend.AutoSize = true;
this.checkBoxNewlineSend.Location = new System.Drawing.Point(154, 0);
this.checkBoxNewlineSend.Name = "checkBoxNewlineSend";
this.checkBoxNewlineSend.Size = new System.Drawing.Size(48, 16);
this.checkBoxNewlineSend.TabIndex = 12;
this.checkBoxNewlineSend.Text = "换行";
this.checkBoxNewlineSend.UseVisualStyleBackColor = true;
//
// checkBoxHexView
//
this.checkBoxHexView.AutoSize = true;
this.checkBoxHexView.Checked = true;
this.checkBoxHexView.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBoxHexView.Location = new System.Drawing.Point(109, 36);
this.checkBoxHexView.Name = "checkBoxHexView";
this.checkBoxHexView.Size = new System.Drawing.Size(60, 16);
this.checkBoxHexView.TabIndex = 7;
this.checkBoxHexView.Text = "16进制";
this.checkBoxHexView.UseVisualStyleBackColor = true;
//
// checkBoxNewlineGet
//
this.checkBoxNewlineGet.AutoSize = true;
this.checkBoxNewlineGet.Location = new System.Drawing.Point(181, 36);
this.checkBoxNewlineGet.Name = "checkBoxNewlineGet";
this.checkBoxNewlineGet.Size = new System.Drawing.Size(72, 16);
this.checkBoxNewlineGet.TabIndex = 8;
this.checkBoxNewlineGet.Text = "自动换行";
this.checkBoxNewlineGet.UseVisualStyleBackColor = true;
this.checkBoxNewlineGet.CheckedChanged += new System.EventHandler(this.checkBoxNewlineGet_CheckedChanged);
//
// SerialportSampleForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(573, 456);
this.Controls.Add(this.checkBoxNewlineGet);
this.Controls.Add(this.checkBoxHexView);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.buttonReset);
this.Controls.Add(this.buttonOpenClose);
this.Controls.Add(this.comboBaudrate);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.comboPortName);
this.Name = "SerialportSampleForm";
this.Text = "SerialPort 串口通讯";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.ComboBox comboPortName;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox comboBaudrate;
private System.Windows.Forms.Button buttonOpenClose;
private System.Windows.Forms.Button buttonReset;
private System.Windows.Forms.Label labelSendCount;
private System.Windows.Forms.Label labelGetCount;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox txGet;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.CheckBox checkBoxNewlineSend;
private System.Windows.Forms.CheckBox checkBoxHexSend;
private System.Windows.Forms.Button buttonSend;
private System.Windows.Forms.TextBox txSend;
private System.Windows.Forms.CheckBox checkBoxHexView;
private System.Windows.Forms.CheckBox checkBoxNewlineGet;
private System.Windows.Forms.ComboBox DropCRC;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnCrc;


 

更改电脑端口?

"设备管理器"->"通讯端口"->右键->"属性"->"端口设置"->"高级"->"COM端口号"

c# com口通讯(1)- 界面代码

 

 

推荐博客:

http://bbs.csdn.net/topics/340118401

http://blog.csdn.net/wuyazhe/article/category/695097

http://www.cnblogs.com/Sue_/category/293326.html