}
private void RefreshSerialPortsBtn_Click(object sender, EventArgs e) {
foreach (string PortName in SerialPort.GetPortNames()) {
PortNamesCbB.Items.Add(PortName); } }
private void StopDisplayBtn_Click(object sender, EventArgs e) {
if (StopDisplayBtn.Text == \停止显示\ {
StopDisplayBtn.Text = \恢复显示\ }
else {
StopDisplayBtn.Text = \停止显示\ } }
private void AutoSendChk_CheckedChanged(object sender, EventArgs e) {
if (AutoSendChk.Checked)
{
timer1.Interval = Convert.ToInt32(AutoTxTime.Text); timer1.Enabled = true;
AutoTxTime.Enabled = false; } else {
timer1.Enabled = false;
AutoTxTime.Enabled = true; } }
private void timer1_Tick(object sender, EventArgs e) {
SendData(serialPort1); } } }
TestSerialPort1.Designer.cs文件内容: namespace TestSerialPort1 {
partial class TestSerialPort1 {
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
///
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose(); }
base.Dispose(disposing); }
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。 ///
private void InitializeComponent() {
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources System.ComponentModel.ComponentResourceManager(typeof(TestSerialPort1)); this.SendBtn = new System.Windows.Forms.Button(); this.SendBox = new System.Windows.Forms.TextBox(); this.ReceiveBox = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label();
this.PortNamesCbB = new System.Windows.Forms.ComboBox(); this.OpenSerialPortBtn = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.label6 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label();
=
new
this.label4 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label();
this.StopBitsCbB = new System.Windows.Forms.ComboBox(); this.DataBitsCbB = new System.Windows.Forms.ComboBox();
this.ParityCbB = new System.Windows.Forms.ComboBox(); this.BaudrateCbB = new System.Windows.Forms.ComboBox(); this.ClrRxBoxBtn = new System.Windows.Forms.Button();
this.HexDisplayChk = new System.Windows.Forms.CheckBox(); this.AutoSendChk = new System.Windows.Forms.CheckBox(); this.HexSendChk = new System.Windows.Forms.CheckBox(); this.AutoTxTime = new System.Windows.Forms.TextBox(); this.label7 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox(); this.TxCount = new System.Windows.Forms.TextBox(); this.RxCount = new System.Windows.Forms.TextBox(); this.ClrBtn = new System.Windows.Forms.Button(); this.label8 = new System.Windows.Forms.Label(); this.label9 = new System.Windows.Forms.Label();
this.RefreshSerialPortsBtn = new System.Windows.Forms.Button(); this.StopDisplayBtn = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.SuspendLayout();
//
// SendBtn
//
this.SendBtn.Location = new System.Drawing.Point(446, 314); this.SendBtn.Name = \
this.SendBtn.Size = new System.Drawing.Size(75, 23); this.SendBtn.TabIndex = 0;
this.SendBtn.Text = \发送\
this.SendBtn.UseVisualStyleBackColor = true;
this.SendBtn.Click += new System.EventHandler(this.SendBtn_Click); // // SendBox
//
this.SendBox.BackColor = System.Drawing.SystemColors.Window; this.SendBox.Location = new System.Drawing.Point(167, 281); this.SendBox.Multiline = true;
this.SendBox.Name = \
this.SendBox.Size = new System.Drawing.Size(356, 28); this.SendBox.TabIndex = 2;
// // ReceiveBox //
this.ReceiveBox.BackColor = System.Drawing.SystemColors.Info;
this.ReceiveBox.Location = new System.Drawing.Point(167, 27);
this.ReceiveBox.Multiline = true;
this.ReceiveBox.Name = \
this.ReceiveBox.ReadOnly = true;
this.ReceiveBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.ReceiveBox.Size = new System.Drawing.Size(356, 246); this.ReceiveBox.TabIndex = 3; // // label1 //
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(165, 12); this.label1.Name = \
this.label1.Size = new System.Drawing.Size(53, 12); this.label1.TabIndex = 4; this.label1.Text = \接收区:\ // // PortNamesCbB //
this.PortNamesCbB.FormattingEnabled = true;
this.PortNamesCbB.Location = new System.Drawing.Point(72, 20);
this.PortNamesCbB.Name = \
this.PortNamesCbB.Size = new System.Drawing.Size(71, 20); this.PortNamesCbB.TabIndex = 6; this.PortNamesCbB.Text = \
this.PortNamesCbB.SelectedIndexChanged
+= System.EventHandler(this.PortNamesCbB_SelectedIndexChanged); //
// OpenSerialPortBtn //
this.OpenSerialPortBtn.Location = new System.Drawing.Point(12, 180); this.OpenSerialPortBtn.Name = \
this.OpenSerialPortBtn.Size = new System.Drawing.Size(70, 23); this.OpenSerialPortBtn.TabIndex = 7;
this.OpenSerialPortBtn.Text = \打开串口\
this.OpenSerialPortBtn.UseVisualStyleBackColor = true; this.OpenSerialPortBtn.Click += System.EventHandler(this.OpenSerialPortBtn_Click); //
// groupBox1
new
new
//
this.groupBox1.Controls.Add(this.label6); this.groupBox1.Controls.Add(this.label5); this.groupBox1.Controls.Add(this.label4); this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.StopBitsCbB); this.groupBox1.Controls.Add(this.DataBitsCbB); this.groupBox1.Controls.Add(this.ParityCbB); this.groupBox1.Controls.Add(this.BaudrateCbB); this.groupBox1.Controls.Add(this.PortNamesCbB);
this.groupBox1.ImeMode = System.Windows.Forms.ImeMode.NoControl; this.groupBox1.Location = new System.Drawing.Point(7, 12); this.groupBox1.Name = \
this.groupBox1.Size = new System.Drawing.Size(152, 167); this.groupBox1.TabIndex = 8; this.groupBox1.TabStop = false; this.groupBox1.Text = \参数设置\ // // label6 //
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(11, 144); this.label6.Name = \
this.label6.Size = new System.Drawing.Size(53, 12);
this.label6.TabIndex = 15; this.label6.Text = \停止位:\ //
// label5 //
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(11, 112); this.label5.Name = \
this.label5.Size = new System.Drawing.Size(53, 12); this.label5.TabIndex = 14; this.label5.Text = \数据位:\ // // label4 //
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(11, 82); this.label4.Name = \
this.label4.Size = new System.Drawing.Size(53, 12); this.label4.TabIndex = 13;