⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 settingswindow.cs

📁 SerialPort 串口通讯用
💻 CS
字号:
// ==++==
// 
//   Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// ==--==
/*============================================================
**
** Class:  SettingsWindow
**
** Purpose: In CSTerm application, SettingsWindow allows the user
**		  : to modify port settings: baudRate, portName, connection type, 
**		  : parity, encoding, dataBits, stopBits, and handshaking.
**		  : It is not guaranteed that ANY combination of settings
**		  : will be allowed by your serial driver.
**		  
** Date:  August 2002
**
===========================================================*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace System.IO.Ports
{
	/// <summary>
	/// Summary description for Form3.
	/// </summary>
	public class SettingsWindow : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		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.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private TerminalWindow parentWindow;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.ListBox listBox2;
		private System.Windows.Forms.ListBox listBox3;
		private System.Windows.Forms.ListBox listBox4;
		private System.Windows.Forms.ListBox listBox5;
		private System.Windows.Forms.ListBox listBox6;
		private System.Windows.Forms.Button button2;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		
		public SettingsWindow(TerminalWindow parentWindow, int windowID)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			//this.Text = "Terminal Window " + windowID;
			//this.richTextBox1.Text = this.richTextBox2.Text = "";
			//this.statusBar1.Text = "Ready";
			//this.windowID = windowID;
			this.parentWindow = parentWindow;
			this.Text = "Settings Window for Connection " + windowID;
			listBox1.Items.Add("Null Modem");
			listBox1.Items.Add("Modem");
			
			listBox2.Items.Add(Parity.None);
			listBox2.Items.Add(Parity.Even);
			listBox2.Items.Add(Parity.Odd);
			listBox2.Items.Add(Parity.Mark);
			listBox2.Items.Add(Parity.Space);
			
			listBox3.Items.Add("ASCII");
			listBox3.Items.Add("Unicode");
			listBox3.Items.Add("UTF7");
			listBox3.Items.Add("UTF8");
			
			listBox4.Items.Add(8);
			listBox4.Items.Add(7);
			listBox4.Items.Add(6);
			listBox4.Items.Add(5);
			
			listBox5.Items.Add(StopBits.One);
			listBox5.Items.Add(StopBits.OnePointFive);
			listBox5.Items.Add(StopBits.Two);
			
			listBox6.Items.Add(Handshake.None);
			listBox6.Items.Add(Handshake.RequestToSend);
			listBox6.Items.Add(Handshake.XOnXOff);
			listBox6.Items.Add(Handshake.RequestToSendXOnXOff);
		
			listBox1.SelectedIndex = 0;
			listBox2.SelectedIndex = 0;
			listBox3.SelectedIndex = 0;
			listBox4.SelectedIndex = 0;
			listBox5.SelectedIndex = 0;
			listBox6.SelectedIndex = 0;
			
			if (this.parentWindow.serialPort1.IsOpen) {
				textBox2.Enabled = false;
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.listBox2 = new System.Windows.Forms.ListBox();
			this.listBox3 = new System.Windows.Forms.ListBox();
			this.listBox4 = new System.Windows.Forms.ListBox();
			this.listBox5 = new System.Windows.Forms.ListBox();
			this.listBox6 = new System.Windows.Forms.ListBox();
			this.button2 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(96, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Baud Rate";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(128, 24);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(96, 16);
			this.label2.TabIndex = 1;
			this.label2.Text = "Port Name";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(256, 24);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(96, 16);
			this.label3.TabIndex = 2;
			this.label3.Text = "Connect Type";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(376, 24);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(96, 16);
			this.label4.TabIndex = 3;
			this.label4.Text = "Parity";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(16, 144);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(96, 16);
			this.label5.TabIndex = 4;
			this.label5.Text = "Encoding";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(128, 144);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(96, 16);
			this.label6.TabIndex = 5;
			this.label6.Text = "DataBits";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(256, 144);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(96, 16);
			this.label7.TabIndex = 6;
			this.label7.Text = "StopBits";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(376, 144);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(96, 16);
			this.label8.TabIndex = 7;
			this.label8.Text = "Handshake";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(448, 232);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(72, 24);
			this.button1.TabIndex = 8;
			this.button1.Text = "OK";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(16, 48);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(80, 20);
			this.textBox1.TabIndex = 9;
			this.textBox1.Text = "38400";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(128, 48);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(88, 20);
			this.textBox2.TabIndex = 10;
			this.textBox2.Text = "COM1";
			// 
			// listBox1
			// 
			this.listBox1.Location = new System.Drawing.Point(264, 48);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(80, 30);
			this.listBox1.TabIndex = 11;
			// 
			// listBox2
			// 
			this.listBox2.Location = new System.Drawing.Point(376, 48);
			this.listBox2.Name = "listBox2";
			this.listBox2.Size = new System.Drawing.Size(80, 56);
			this.listBox2.TabIndex = 12;
			// 
			// listBox3
			// 
			this.listBox3.Location = new System.Drawing.Point(16, 168);
			this.listBox3.Name = "listBox3";
			this.listBox3.Size = new System.Drawing.Size(80, 56);
			this.listBox3.TabIndex = 13;
			// 
			// listBox4
			// 
			this.listBox4.Location = new System.Drawing.Point(128, 168);
			this.listBox4.Name = "listBox4";
			this.listBox4.Size = new System.Drawing.Size(80, 56);
			this.listBox4.TabIndex = 14;
			// 
			// listBox5
			// 
			this.listBox5.Location = new System.Drawing.Point(256, 168);
			this.listBox5.Name = "listBox5";
			this.listBox5.Size = new System.Drawing.Size(80, 56);
			this.listBox5.TabIndex = 15;
			// 
			// listBox6
			// 
			this.listBox6.Location = new System.Drawing.Point(376, 168);
			this.listBox6.Name = "listBox6";
			this.listBox6.Size = new System.Drawing.Size(80, 56);
			this.listBox6.TabIndex = 16;
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(544, 232);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(72, 24);
			this.button2.TabIndex = 17;
			this.button2.Text = "Cancel";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// SettingsWindow
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(672, 266);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.listBox6);
			this.Controls.Add(this.listBox5);
			this.Controls.Add(this.listBox4);
			this.Controls.Add(this.listBox3);
			this.Controls.Add(this.listBox2);
			this.Controls.Add(this.listBox1);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.label8);
			this.Controls.Add(this.label7);
			this.Controls.Add(this.label6);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Name = "SettingsWindow";
			this.Text = "Form3";
			this.ResumeLayout(false);

		}
		#endregion

		private void button1_Click(object sender, System.EventArgs e)
		{
			int baudRate;
			string portName;
			bool nullModem;
			int dataBits;
			StopBits stopBits;
			Handshake handshake;
			
			parentWindow.serialPort1.BaudRate = System.Convert.ToInt32(textBox1.Text);
			if (textBox2.Enabled == true) {
				parentWindow.serialPort1.PortName = textBox2.Text;
			}
			
			if (listBox1.SelectedItem.Equals("Null Modem")) {
				parentWindow.serialPort1.DtrEnable = false;
				parentWindow.serialPort1.RtsEnable = false;
				parentWindow.usingModem = false;
				parentWindow.textBox1.Enabled = false;
				parentWindow.radioButton1.Enabled = false;
				parentWindow.radioButton2.Enabled = false;
			} else {
				parentWindow.serialPort1.DtrEnable = false;	// change to true
				parentWindow.serialPort1.RtsEnable = false; // change to true
				parentWindow.usingModem = true;
				parentWindow.textBox1.Enabled = true;
				parentWindow.radioButton1.Enabled = true;
				parentWindow.radioButton2.Enabled = true;
				// need to do modem connection stuff here too!
			}
			
			if (listBox3.SelectedItem.Equals("ASCII")) 
				parentWindow.serialPort1.Encoding = new System.Text.ASCIIEncoding();
	
			else if (listBox3.SelectedItem.Equals("Unicode"))
				parentWindow.serialPort1.Encoding = new System.Text.UnicodeEncoding();
			else if (listBox3.SelectedItem.Equals("UTF7"))
				parentWindow.serialPort1.Encoding = new System.Text.UTF7Encoding();
			else 
				parentWindow.serialPort1.Encoding = new System.Text.UTF8Encoding();
				
			parentWindow.serialPort1.Parity = (Parity) listBox2.SelectedItem;
			parentWindow.serialPort1.DataBits = (int) listBox4.SelectedItem;
			parentWindow.serialPort1.StopBits = (StopBits) listBox5.SelectedItem;
			parentWindow.serialPort1.Handshake = (Handshake) listBox6.SelectedItem;
			
			parentWindow.Enabled = true;
			this.Close();
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			parentWindow.Enabled = true;
			this.Close();			
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -