using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.IO.Ports;
namespace MyCETerminalChat
{
public partial class Form1 : Form
{
string name;
string message;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
// Create a new SerialPort object with default settings.
SerialPort sp = new SerialPort();
public Form1()
{
InitializeComponent();
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
cboPort.Items.Add(port);
}
}
private void cmdOpen_Click(object sender, EventArgs e)
{
try
{
sp = new SerialPort(cboPort.SelectedItem.ToString());
sp.BaudRate = int.Parse(cboBaud.SelectedItem.ToString());
// Set the read/write timeouts
sp.WriteTimeout = 500;
sp.ReadTimeout = 500;
sp.Parity = Parity.None;
sp.DataBits = 8;
sp.StopBits = StopBits.One;
sp.Handshake = Handshake.None;
sp.Open();
if (sp.IsOpen == true)
{
MessageBox.Show("Success");
cmdOpen.Enabled = false;
cmdClose.Enabled = true;
cmdSend.Enabled = true;
}
else
MessageBox.Show("Port no Connect");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void cmdSend_Click(object sender, EventArgs e)
{
string msg = txtSend.Text;
textBox1.Text += (msg);
sp.Write(msg);
txtSend.Text = "";
}
private void cmdClose_Click(object sender, EventArgs e)
{
sp.Close();
cmdOpen.Enabled = true;
cmdClose.Enabled = false;
cmdSend.Enabled = false;
}
delegate void ChangeTxtReciveTextDelegate(string newText);
private void ChangeTxtReciveText(string newText)
{
if (textBox1.InvokeRequired)
textBox1.Invoke(new ChangeTxtReciveTextDelegate(ChangeTxtReciveText), newText);
else
textBox1.Text = newText;
}
public void Read()
{
try
{
string message = sp.ReadExisting();
MessageBox.Show("Selected Item Text: " + message);
ChangeTxtReciveText(message);
}
catch (TimeoutException) { }
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace MyCETerminalChat
{
partial class Form1
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.MainMenu mainMenu1;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.textBox1 = new System.Windows.Forms.TextBox();
this.txtSend = new System.Windows.Forms.TextBox();
this.cmdSend = new System.Windows.Forms.Button();
this.cmdClose = new System.Windows.Forms.Button();
this.cmdOpen = new System.Windows.Forms.Button();
this.cboPort = new System.Windows.Forms.ComboBox();
this.cboBaud = new System.Windows.Forms.ComboBox();
this.Label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(15, 18);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 117);
this.textBox1.TabIndex = 0;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// txtSend
//
this.txtSend.Location = new System.Drawing.Point(15, 176);
this.txtSend.Name = "txtSend";
this.txtSend.Size = new System.Drawing.Size(100, 23);
this.txtSend.TabIndex = 1;
//
// cmdSend
//
this.cmdSend.Location = new System.Drawing.Point(134, 179);
this.cmdSend.Name = "cmdSend";
this.cmdSend.Size = new System.Drawing.Size(51, 20);
this.cmdSend.TabIndex = 2;
this.cmdSend.Text = "Send";
this.cmdSend.Click += new System.EventHandler(this.cmdSend_Click);
//
// cmdClose
//
this.cmdClose.Location = new System.Drawing.Point(15, 216);
this.cmdClose.Name = "cmdClose";
this.cmdClose.Size = new System.Drawing.Size(75, 20);
this.cmdClose.TabIndex = 3;
this.cmdClose.Text = "Close Port";
this.cmdClose.Click += new System.EventHandler(this.cmdClose_Click);
//
// cmdOpen
//
this.cmdOpen.Location = new System.Drawing.Point(121, 134);
this.cmdOpen.Name = "cmdOpen";
this.cmdOpen.Size = new System.Drawing.Size(74, 20);
this.cmdOpen.TabIndex = 4;
this.cmdOpen.Text = "Open Port";
this.cmdOpen.Click += new System.EventHandler(this.cmdOpen_Click);
//
// cboPort
//
this.cboPort.Location = new System.Drawing.Point(128, 38);
this.cboPort.Name = "cboPort";
this.cboPort.Size = new System.Drawing.Size(67, 23);
this.cboPort.TabIndex = 5;
//
// cboBaud
//
this.cboBaud.Items.Add("300");
this.cboBaud.Items.Add("600");
this.cboBaud.Items.Add("1200");
this.cboBaud.Items.Add("2400");
this.cboBaud.Items.Add("4800");
this.cboBaud.Items.Add("9600");
this.cboBaud.Items.Add("14400");
this.cboBaud.Items.Add("28800");
this.cboBaud.Items.Add("36000");
this.cboBaud.Items.Add("115000");
this.cboBaud.Location = new System.Drawing.Point(128, 94);
this.cboBaud.Name = "cboBaud";
this.cboBaud.Size = new System.Drawing.Size(67, 23);
this.cboBaud.TabIndex = 6;
this.cboBaud.SelectedIndexChanged += new System.EventHandler(this.cboBaud_SelectedIndexChanged);
//
// Label1
//
this.Label1.Location = new System.Drawing.Point(131, 18);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(50, 17);
this.Label1.Text = "Port";
//
// label2
//
this.label2.Location = new System.Drawing.Point(128, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(60, 19);
this.label2.Text = "Baud Rate";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(211, 254);
this.Controls.Add(this.label2);
this.Controls.Add(this.Label1);
this.Controls.Add(this.cboBaud);
this.Controls.Add(this.cboPort);
this.Controls.Add(this.cmdOpen);
this.Controls.Add(this.cmdClose);
this.Controls.Add(this.cmdSend);
this.Controls.Add(this.txtSend);
this.Controls.Add(this.textBox1);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox txtSend;
private System.Windows.Forms.Button cmdSend;
private System.Windows.Forms.Button cmdClose;
private System.Windows.Forms.Button cmdOpen;
private System.Windows.Forms.ComboBox cboPort;
private System.Windows.Forms.ComboBox cboBaud;
private System.Windows.Forms.Label Label1;
private System.Windows.Forms.Label label2;
}
}
////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace MyCETerminalChat
{
static class Program
{
///
/// The main entry point for the application.
///
[MTAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}