📄 terminalwin.cs
字号:
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*============================================================
**
** Class: TerminalWindow
**
** Purpose: In CSTerm application, TerminalWindow acts as the
** : main I/O form, allowing connections to be opened
** : or closed and lines/files to be read/written to the port
**
** Date: August 2002
**
===========================================================*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Net;
namespace System.IO.Ports
{
public class TerminalWindow : System.Windows.Forms.Form
{
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.RichTextBox richTextBox1;
internal SerialPort serialPort1;
internal bool usingModem = false;
private System.ComponentModel.Container components = null;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.RichTextBox richTextBox3;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
internal System.Windows.Forms.TextBox textBox1;
internal System.Windows.Forms.RadioButton radioButton1;
internal System.Windows.Forms.RadioButton radioButton2;
private int windowID;
private System.Text.ASCIIEncoding asciiEncoding;
public TerminalWindow(int windowID)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.Text = "Terminal Window " + windowID;
this.richTextBox1.Text = "";
this.richTextBox2.Text = "";
this.richTextBox3.Text = "";
this.richTextBox2.Enabled = false;
this.textBox1.Enabled = false;
this.radioButton1.Enabled = false;
this.radioButton2.Enabled = false;
this.statusBar1.Text = "Ready";
this.windowID = windowID;
this.serialPort1 = new SerialPort();
this.serialPort1.ReceivedEvent += new SerialEventHandler(gotIncomingText);
this.serialPort1.ErrorEvent += new SerialEventHandler(gotIncomingError);
this.serialPort1.ErrorEvent += new SerialEventHandler(gotIncomingPinChange);
asciiEncoding = new System.Text.ASCIIEncoding();
}
/// <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.statusBar1 = new System.Windows.Forms.StatusBar();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.richTextBox3 = new System.Windows.Forms.RichTextBox();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.SuspendLayout();
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 310);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(704, 24);
this.statusBar1.TabIndex = 0;
this.statusBar1.Text = "statusBar1";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(16, 16);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(520, 80);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "Sender Text";
//
// richTextBox2
//
this.richTextBox2.Location = new System.Drawing.Point(16, 208);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(416, 24);
this.richTextBox2.TabIndex = 3;
this.richTextBox2.Text = "Text Entry";
this.richTextBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.richTextBox2_KeyPress);
//
// button1
//
this.button1.Location = new System.Drawing.Point(552, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 32);
this.button1.TabIndex = 4;
this.button1.Text = "Change Properties";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(552, 56);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 32);
this.button2.TabIndex = 5;
this.button2.Text = "Open Connection";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// richTextBox3
//
this.richTextBox3.Location = new System.Drawing.Point(16, 112);
this.richTextBox3.Name = "richTextBox3";
this.richTextBox3.Size = new System.Drawing.Size(520, 80);
this.richTextBox3.TabIndex = 6;
this.richTextBox3.Text = "Received Text";
//
// button3
//
this.button3.Location = new System.Drawing.Point(552, 192);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(88, 32);
this.button3.TabIndex = 7;
this.button3.Text = "ReadFile";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(552, 232);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(88, 32);
this.button4.TabIndex = 8;
this.button4.Text = "SendFile";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(552, 96);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(112, 20);
this.textBox1.TabIndex = 9;
this.textBox1.Text = "Enter Phone Number";
//
// radioButton1
//
this.radioButton1.Checked = true;
this.radioButton1.Location = new System.Drawing.Point(552, 120);
this.radioButton1.Name = "radioButton1";
this.radioButton1.TabIndex = 10;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "Dial Mode";
//
// radioButton2
//
this.radioButton2.Location = new System.Drawing.Point(552, 144);
this.radioButton2.Name = "radioButton2";
this.radioButton2.TabIndex = 11;
this.radioButton2.Text = "Answer Mode";
//
// TerminalWindow
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(704, 334);
this.Controls.Add(this.radioButton2);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.richTextBox3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.richTextBox2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.statusBar1);
this.Name = "TerminalWindow";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
private void sendLine(string line)
{
serialPort1.Write(line);
}
private void button1_Click(object sender, System.EventArgs e)
{
SettingsWindow sw = new SettingsWindow(this, windowID);
this.Enabled = false;
sw.Show();
}
private void button2_Click(object sender, System.EventArgs e)
{
if (serialPort1.IsOpen == false) {
try {
serialPort1.Open();
richTextBox2.Enabled = true;
button2.Text = "Close Connection";
if (usingModem == true) {
if (radioButton1.Checked == true) CallNumber();
else AnswerCall();
}
this.richTextBox1.AppendText("SYSTEM: Connection opened\n");
} catch (IOException exception) {
MessageBox.Show("Invalid parameter set in Open(): " + exception.Message);
}
} else {
if (usingModem == true) Hangup();
serialPort1.Close();
button2.Text = "Open Connection";
richTextBox2.Enabled = false;
this.richTextBox1.AppendText("SYSTEM: Connection closed\n");
}
}
private void Hangup() {
bool ret = serialPort1.DtrEnable;
byte [] hangUpCommand = asciiEncoding.GetBytes("ATH0\r\n");
serialPort1.Write(hangUpCommand, 0, 4);
serialPort1.DtrEnable = true;
serialPort1.DtrEnable = false;
serialPort1.DtrEnable = ret;
}
private void CallNumber() {
string callText = "ATDT" + textBox1.Text + "\r\n";
byte[] callCommand = asciiEncoding.GetBytes(callText);
serialPort1.Write(callCommand, 0, callCommand.Length);
}
private void AnswerCall() {
string answerText = "ATE1S0=1\r\n";
byte[] answerCommand = asciiEncoding.GetBytes(answerText);
serialPort1.Write(answerCommand, 0, answerCommand.Length);
}
private void gotIncomingText(object sender, SerialEventArgs e)
{
string receivedText = serialPort1.ReadAvailable();
this.richTextBox3.AppendText(receivedText);
}
private void gotIncomingError(object sender, SerialEventArgs e)
{
serialPort1.DiscardInBuffer();
serialPort1.DiscardOutBuffer();
MessageBox.Show("A communications error has occurred.");
richTextBox1.AppendText("\nSYSTEM: -------------- Comm Error: " + (int) e.EventType + " ---------------\n");
richTextBox3.AppendText("\nSYSTEM: -------------- Comm Error: " + (int) e.EventType + " ---------------\n");
richTextBox2.Text = "";
}
private void gotIncomingPinChange(object sender, SerialEventArgs e)
{
if ((e.EventType & SerialEvents.Ring) != SerialEvents.None)
richTextBox1.AppendText("SYSTEM: Ring Detected\n");
if ((e.EventType & SerialEvents.CDChanged) != SerialEvents.None)
if (serialPort1.CDHolding == true) richTextBox1.AppendText("SYSTEM: CD High \n");
else richTextBox1.AppendText("SYSTEM: CD Low\n");
if ((e.EventType & SerialEvents.CtsChanged) != SerialEvents.None)
if (serialPort1.CtsHolding == true) richTextBox1.AppendText("SYSTEM: CTS High \n");
else richTextBox1.AppendText("SYSTEM: CTS Low\n");
if ((e.EventType & SerialEvents.DsrChanged) != SerialEvents.None)
if (serialPort1.DsrHolding == true) richTextBox1.AppendText("SYSTEM: DSR High \n");
else richTextBox1.AppendText("SYSTEM: DSR Low\n");
}
private void richTextBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (Equals(e.KeyChar, Environment.NewLine[0]))
{
if (serialPort1.IsOpen == false) {
this.richTextBox1.AppendText("SYSTEM: Cannot send - no connection open!\n");
} else {
// should be encoding-dependent, converted in
// serialPort1.Write to whatever is appropriate.
sendLine(richTextBox2.Text);
this.richTextBox1.AppendText(richTextBox2.Text);
}
this.richTextBox2.Text = "";
}
}
// receive file
private void button3_Click(object sender, System.EventArgs e)
{
if (serialPort1.IsOpen == false) return;
Stream myStream ;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
int currentTimeout;
byte [] byteBuffer = new byte[1000];
int numJustRead;
int total = 0;
if((myStream = saveFileDialog1.OpenFile()) != null)
{
// protocol - wait 20 sec. for any data to begin
// then, read until 500 ms. elapse without any more data as a singal
// for End of Stream
int oldTimeout = serialPort1.ReadTimeout;
serialPort1.ReadTimeout = 20000;
currentTimeout = 20000;
serialPort1.DiscardInBuffer();
// turn off receive events
serialPort1.EventFilter = SerialEvents.None;
while ((numJustRead = serialPort1.Read(byteBuffer, 0, 1000)) != 0) {
if (currentTimeout == 20000) serialPort1.ReadTimeout = 500;
myStream.Write(byteBuffer, 0, numJustRead);
total+= numJustRead;
}
myStream.Close();
MessageBox.Show("File saved. Size: " + total + " bytes.");
serialPort1.EventFilter = SerialEvents.All;
serialPort1.ReadTimeout = oldTimeout;
}
}
}
// send file
private void button4_Click(object sender, System.EventArgs e)
{
if (serialPort1.IsOpen == false) return;
// use open file dialog
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
byte [] byteBuffer = new byte[1000];
int numJustRead;
serialPort1.DiscardOutBuffer();
if((myStream = openFileDialog1.OpenFile())!= null)
{
// use stream-transfer paradigm to throw all bytes to output
while ((numJustRead = myStream.Read(byteBuffer, 0, 1000)) != 0) {
serialPort1.Write(byteBuffer, 0, numJustRead);
}
myStream.Close();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -