📄 mainform.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using KYLai_MScomm;
namespace CSMA_EmuMaster
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private AxMSCommLib.AxMSComm MSComm1;
private System.Windows.Forms.Button btSend;
private System.Windows.Forms.TextBox txlSend;
private System.Windows.Forms.TextBox txlRx1;
private System.Windows.Forms.Button btSendByte;
private System.Windows.Forms.StatusBar stbStatus;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem mnuOpen;
private System.Windows.Forms.MenuItem mnuClose;
private System.Windows.Forms.MenuItem mnuSetting;
private System.Windows.Forms.GroupBox gbMSCom;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox t1;
private System.Windows.Forms.Button btSequence;
private System.Windows.Forms.TextBox txlSeq1;
private System.Windows.Forms.TextBox txlSeq2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <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()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
this.MSComm1 = new AxMSCommLib.AxMSComm();
this.txlSend = new System.Windows.Forms.TextBox();
this.btSend = new System.Windows.Forms.Button();
this.txlRx1 = new System.Windows.Forms.TextBox();
this.btSendByte = new System.Windows.Forms.Button();
this.stbStatus = new System.Windows.Forms.StatusBar();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.mnuOpen = new System.Windows.Forms.MenuItem();
this.mnuClose = new System.Windows.Forms.MenuItem();
this.mnuSetting = new System.Windows.Forms.MenuItem();
this.gbMSCom = new System.Windows.Forms.GroupBox();
this.t1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.btSequence = new System.Windows.Forms.Button();
this.txlSeq1 = new System.Windows.Forms.TextBox();
this.txlSeq2 = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.MSComm1)).BeginInit();
this.gbMSCom.SuspendLayout();
this.SuspendLayout();
//
// MSComm1
//
this.MSComm1.ContainingControl = this;
this.MSComm1.Enabled = true;
this.MSComm1.Location = new System.Drawing.Point(208, 24);
this.MSComm1.Name = "MSComm1";
this.MSComm1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("MSComm1.OcxState")));
this.MSComm1.Size = new System.Drawing.Size(38, 38);
this.MSComm1.TabIndex = 3;
this.MSComm1.OnComm += new System.EventHandler(this.MSComm1_OnComm);
//
// txlSend
//
this.txlSend.Location = new System.Drawing.Point(24, 144);
this.txlSend.Name = "txlSend";
this.txlSend.TabIndex = 4;
this.txlSend.Text = "1";
//
// btSend
//
this.btSend.Location = new System.Drawing.Point(128, 176);
this.btSend.Name = "btSend";
this.btSend.Size = new System.Drawing.Size(72, 23);
this.btSend.TabIndex = 5;
this.btSend.Text = "Send Ascii";
this.btSend.Click += new System.EventHandler(this.btSend_Click);
//
// txlRx1
//
this.txlRx1.Location = new System.Drawing.Point(24, 24);
this.txlRx1.Multiline = true;
this.txlRx1.Name = "txlRx1";
this.txlRx1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txlRx1.Size = new System.Drawing.Size(176, 112);
this.txlRx1.TabIndex = 6;
this.txlRx1.Text = "";
this.txlRx1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.txlRx1_MouseUp);
//
// btSendByte
//
this.btSendByte.Location = new System.Drawing.Point(128, 144);
this.btSendByte.Name = "btSendByte";
this.btSendByte.Size = new System.Drawing.Size(72, 23);
this.btSendByte.TabIndex = 7;
this.btSendByte.Text = "Send Byte";
this.btSendByte.Click += new System.EventHandler(this.btSendByte_Click);
//
// stbStatus
//
this.stbStatus.Location = new System.Drawing.Point(0, 305);
this.stbStatus.Name = "stbStatus";
this.stbStatus.Size = new System.Drawing.Size(352, 16);
this.stbStatus.TabIndex = 8;
this.stbStatus.Text = "statusBar1";
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuOpen,
this.mnuClose,
this.mnuSetting});
//
// mnuOpen
//
this.mnuOpen.Index = 0;
this.mnuOpen.Text = "Open";
this.mnuOpen.Click += new System.EventHandler(this.mnuOpen_Click);
//
// mnuClose
//
this.mnuClose.Index = 1;
this.mnuClose.Text = "Close";
this.mnuClose.Click += new System.EventHandler(this.mnuClose_Click);
//
// mnuSetting
//
this.mnuSetting.Index = 2;
this.mnuSetting.Text = "Setting";
this.mnuSetting.Click += new System.EventHandler(this.mnuSetting_Click);
//
// gbMSCom
//
this.gbMSCom.Controls.Add(this.txlSeq2);
this.gbMSCom.Controls.Add(this.txlSeq1);
this.gbMSCom.Controls.Add(this.btSequence);
this.gbMSCom.Controls.Add(this.t1);
this.gbMSCom.Controls.Add(this.button1);
this.gbMSCom.Controls.Add(this.txlRx1);
this.gbMSCom.Controls.Add(this.txlSend);
this.gbMSCom.Controls.Add(this.btSend);
this.gbMSCom.Controls.Add(this.btSendByte);
this.gbMSCom.Controls.Add(this.MSComm1);
this.gbMSCom.Location = new System.Drawing.Point(8, 8);
this.gbMSCom.Name = "gbMSCom";
this.gbMSCom.Size = new System.Drawing.Size(320, 248);
this.gbMSCom.TabIndex = 9;
this.gbMSCom.TabStop = false;
this.gbMSCom.Text = "gbMSCom";
//
// t1
//
this.t1.Location = new System.Drawing.Point(208, 152);
this.t1.Name = "t1";
this.t1.TabIndex = 9;
this.t1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(208, 176);
this.button1.Name = "button1";
this.button1.TabIndex = 8;
this.button1.Text = "test";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// btSequence
//
this.btSequence.Location = new System.Drawing.Point(128, 208);
this.btSequence.Name = "btSequence";
this.btSequence.TabIndex = 10;
this.btSequence.Text = "Sequence";
this.btSequence.Click += new System.EventHandler(this.btSequence_Click);
//
// txlSeq1
//
this.txlSeq1.Location = new System.Drawing.Point(24, 208);
this.txlSeq1.Name = "txlSeq1";
this.txlSeq1.Size = new System.Drawing.Size(40, 20);
this.txlSeq1.TabIndex = 11;
this.txlSeq1.Text = "0";
//
// txlSeq2
//
this.txlSeq2.Location = new System.Drawing.Point(72, 208);
this.txlSeq2.Name = "txlSeq2";
this.txlSeq2.Size = new System.Drawing.Size(40, 20);
this.txlSeq2.TabIndex = 12;
this.txlSeq2.Text = "255";
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(352, 321);
this.Controls.Add(this.gbMSCom);
this.Controls.Add(this.stbStatus);
this.Menu = this.mainMenu1;
this.Name = "MainForm";
this.Text = "MainForm";
this.Load += new System.EventHandler(this.MainForm_Load);
((System.ComponentModel.ISupportInitialize)(this.MSComm1)).EndInit();
this.gbMSCom.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
public MainForm (AxMSCommLib.AxMSComm objMSComm)
{
MSComm1 = objMSComm;
}
private void MSComm1_OnComm(object sender, System.EventArgs e)
{
if (MSComm1.InBufferCount >0)
{
// input binary
if (MSComm1.InputMode.ToString () == "comInputModeBinary")
{
byte []rxBuffer= (byte[]) MSComm1.Input ; // datas will be store in rxBuffer array
foreach (byte b in rxBuffer)
txlRx1.Text = "Rx: " + b.ToString () + "\r\n" + txlRx1.Text;
}
else
// input text
{
txlRx1.Text = "Rx: " + MSComm1.Input.ToString () + "\r\n" + txlRx1.Text;
MSComm1.InBufferCount =0;
}
if (txlRx1.Text .Length > 500)
txlRx1.Text = txlRx1.Text .Substring (0,10);
}
}
private void MainForm_Load(object sender, System.EventArgs e)
{
MSComm1 = ComPort.DefaultSetting (MSComm1); // load the default setting of com port
MSComm1.CommPort = 2;
MSComm1.Settings = "9600,n,8,1";
OnClose();
}
private void btSend_Click(object sender, System.EventArgs e)
{
try
{
MSComm1.Output = txlSend.Text ;
stbStatus.Text = "Send";
txlRx1.Text = "Tx: " + txlSend.Text + "\r\n" + txlRx1.Text;
}
catch
{
stbStatus.Text = "Data Send error";
}
}
private void btSendByte_Click(object sender, System.EventArgs e)
{
// send data by byte
try
{
byte [] txBuffer = { Convert.ToByte (txlSend.Text ) };
MSComm1.Output = txBuffer;
stbStatus.Text = "Send";
txlSend.Text = (int.Parse (txlSend.Text )+1).ToString ();
if (txlSend.Text =="256")
txlSend.Text ="0";
txlRx1.Text = "Tx: " + txBuffer[0].ToString () + "\r\n" + txlRx1.Text;
}
catch
{
stbStatus.Text = "Data Send error";
}
}
private void mnuOpen_Click(object sender, System.EventArgs e)
{
try
{
if (MSComm1.PortOpen)
MSComm1.PortOpen = false;
//MSComm1.CommPort = byte.Parse (nblistComPort.Value.ToString ()) ;
MSComm1.PortOpen = true;
stbStatus.Text = "Success !!";
OnOpen();
}
catch
{
stbStatus.Text ="fail to connect to the Com port...";
OnClose();
}
}
private void mnuClose_Click(object sender, System.EventArgs e)
{
if (MSComm1.PortOpen)
MSComm1.PortOpen = false;
stbStatus.Text = "Close";
OnClose();
}
private void mnuSetting_Click(object sender, System.EventArgs e)
{
ComPort f = new ComPort (MSComm1);
f.ShowDialog();
OnClose();
}
private void OnOpen()
{
mnuOpen.Enabled = false;
mnuClose.Enabled = true;
mnuSetting.Enabled = false;
gbMSCom.Enabled = true;
stbStatus.Text = "Success --> " + "Com: " + MSComm1.CommPort .ToString () + " " + MSComm1.Settings +" "+ MSComm1.InputMode ;
}
private void OnClose()
{
mnuOpen.Enabled = true;
mnuClose.Enabled = false;
mnuSetting.Enabled = true;
gbMSCom.Enabled = false;
stbStatus.Text = "Close --> " + "Com: " + MSComm1.CommPort .ToString () + " " + MSComm1.Settings +" "+ MSComm1.InputMode ;
}
private void button1_Click(object sender, System.EventArgs e)
{
t1.Text = MSComm1.InputMode .ToString ();
}
private void txlRx1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
txlRx1.Text = "";
}
private void btSequence_Click(object sender, System.EventArgs e)
{
// send data by byte
int m_startnum = int.Parse (txlSeq1.Text);
int m_endnum = int.Parse (txlSeq2.Text );
int i;
for (i=0;i<5000;i++);
for (i=m_startnum;i<=m_endnum;i++)
{
try
{
byte [] txBuffer = new byte [1] ;
txBuffer [0] =byte.Parse (i.ToString ());
MSComm1.Output = txBuffer;
stbStatus.Text = "Send";
txlSend.Text = i.ToString ();
//txlRx1.Text = "Tx: " + txBuffer[0].ToString () + "\r\n" + txlRx1.Text;
txlRx1.Text = "Tx: " + i.ToString () + "\r\n" + txlRx1.Text;
}
catch
{
stbStatus.Text = "Data Send error";
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -