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

📄 form1.cs

📁 MsComm 编程的一个例子
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace C2
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnSend;
		private System.Windows.Forms.TextBox textBox1;
		private AxMSCommLib.AxMSComm axMSComm1;
		private AxMSCommLib.AxMSComm axMSComm2;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.btnSend = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.axMSComm1 = new AxMSCommLib.AxMSComm();
			this.axMSComm2 = new AxMSCommLib.AxMSComm();
			((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.axMSComm2)).BeginInit();
			this.SuspendLayout();
			// 
			// btnSend
			// 
			this.btnSend.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.btnSend.BackColor = System.Drawing.SystemColors.Info;
			this.btnSend.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.btnSend.ForeColor = System.Drawing.SystemColors.Highlight;
			this.btnSend.Location = new System.Drawing.Point(248, 272);
			this.btnSend.Name = "btnSend";
			this.btnSend.Size = new System.Drawing.Size(64, 24);
			this.btnSend.TabIndex = 0;
			this.btnSend.Text = "发送";
			this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			// 
			// textBox1
			// 
			this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left)));
			this.textBox1.Location = new System.Drawing.Point(8, 8);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.ReadOnly = true;
			this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.textBox1.Size = new System.Drawing.Size(312, 256);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "接收区";
			// 
			// axMSComm1
			// 
			this.axMSComm1.Enabled = true;
			this.axMSComm1.Location = new System.Drawing.Point(64, 128);
			this.axMSComm1.Name = "axMSComm1";
			this.axMSComm1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMSComm1.OcxState")));
			this.axMSComm1.Size = new System.Drawing.Size(38, 38);
			this.axMSComm1.TabIndex = 4;
			this.axMSComm1.OnComm += new System.EventHandler(this.axMSComm1_OnComm);
			// 
			// axMSComm2
			// 
			this.axMSComm2.Enabled = true;
			this.axMSComm2.Location = new System.Drawing.Point(160, 128);
			this.axMSComm2.Name = "axMSComm2";
			this.axMSComm2.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMSComm2.OcxState")));
			this.axMSComm2.Size = new System.Drawing.Size(38, 38);
			this.axMSComm2.TabIndex = 5;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(328, 309);
			this.Controls.Add(this.axMSComm2);
			this.Controls.Add(this.axMSComm1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.btnSend);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.axMSComm2)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			try 
			{ 
				axMSComm1.PortOpen =true;
				axMSComm2.PortOpen =true;

				}
			catch 
			{
				MessageBox.Show ("串口操作失败");
			}



		}

		private void btnSend_Click(object sender, System.EventArgs e)
		{
			byte[] bytOut=new byte[1];
			bytOut[0]=255;
			axMSComm2.Output=bytOut;


			}

		private void axMSComm1_OnComm(object sender, System.EventArgs e)
		{
			string strIn="" ;
			byte[] bytIn;
			object objIn;
			int i;
	
			axMSComm1.InputMode=MSCommLib.InputModeConstants.comInputModeBinary ;
			objIn=axMSComm1.Input ;
			bytIn =(byte[])objIn;
			for (i=0;i<=(bytIn.Length-1);i++ )
			{
				strIn +="\r\n"+ " "+bytIn[i].ToString("X");
			}
			textBox1.Text+=strIn;

		}
	}
}

⌨️ 快捷键说明

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