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

📄 frmcomm.cs

📁 C#写的串口通信程序例子
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace SerialComm
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>

	public class frmComm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button buttonOpen;
		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.ComboBox comboRaud;
		private System.Windows.Forms.ComboBox comboData;
		private System.Windows.Forms.ComboBox comboCheck;
		private System.Windows.Forms.ComboBox comboStop;
		private System.Windows.Forms.Button buttonExit;
		private System.Windows.Forms.ComboBox comboComm;
		private AxMSCommLib.AxMSComm axMSComm1;
		private System.Windows.Forms.Button buttonSend;
		private System.Windows.Forms.TextBox txtReceive;
		private System.Windows.Forms.TextBox txtSend;
	
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmComm()
		{
			//
			// 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(frmComm));
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.buttonOpen = new System.Windows.Forms.Button();
			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.comboRaud = new System.Windows.Forms.ComboBox();
			this.comboData = new System.Windows.Forms.ComboBox();
			this.comboCheck = new System.Windows.Forms.ComboBox();
			this.comboStop = new System.Windows.Forms.ComboBox();
			this.comboComm = new System.Windows.Forms.ComboBox();
			this.txtReceive = new System.Windows.Forms.TextBox();
			this.txtSend = new System.Windows.Forms.TextBox();
			this.buttonExit = new System.Windows.Forms.Button();
			this.axMSComm1 = new AxMSCommLib.AxMSComm();
			this.buttonSend = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).BeginInit();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.buttonOpen);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.label4);
			this.groupBox1.Controls.Add(this.label5);
			this.groupBox1.Controls.Add(this.comboRaud);
			this.groupBox1.Controls.Add(this.comboData);
			this.groupBox1.Controls.Add(this.comboCheck);
			this.groupBox1.Controls.Add(this.comboStop);
			this.groupBox1.Controls.Add(this.comboComm);
			this.groupBox1.Location = new System.Drawing.Point(0, 0);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(144, 176);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "串口配置";
			// 
			// buttonOpen
			// 
			this.buttonOpen.Location = new System.Drawing.Point(8, 144);
			this.buttonOpen.Name = "buttonOpen";
			this.buttonOpen.Size = new System.Drawing.Size(128, 23);
			this.buttonOpen.TabIndex = 1;
			this.buttonOpen.Text = "打开串口";
			this.buttonOpen.Click += new System.EventHandler(this.buttonOpen_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "串口号";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 48);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(48, 16);
			this.label2.TabIndex = 0;
			this.label2.Text = "波特率";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 72);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(48, 16);
			this.label3.TabIndex = 0;
			this.label3.Text = "数据位";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 96);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(48, 16);
			this.label4.TabIndex = 0;
			this.label4.Text = "校验位";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(8, 120);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(48, 16);
			this.label5.TabIndex = 0;
			this.label5.Text = "停止位";
			// 
			// comboRaud
			// 
			this.comboRaud.Items.AddRange(new object[] {
														   "CBR_4800",
														   "CBR_9600",
														   "CBR_14400"});
			this.comboRaud.Location = new System.Drawing.Point(56, 44);
			this.comboRaud.Name = "comboRaud";
			this.comboRaud.Size = new System.Drawing.Size(80, 20);
			this.comboRaud.TabIndex = 2;
			this.comboRaud.Text = "CBR_9600";
			// 
			// comboData
			// 
			this.comboData.Items.AddRange(new object[] {
														   "8",
														   "7"});
			this.comboData.Location = new System.Drawing.Point(56, 68);
			this.comboData.Name = "comboData";
			this.comboData.Size = new System.Drawing.Size(80, 20);
			this.comboData.TabIndex = 2;
			this.comboData.Text = "8";
			// 
			// comboCheck
			// 
			this.comboCheck.Items.AddRange(new object[] {
															"n",
															"1"});
			this.comboCheck.Location = new System.Drawing.Point(56, 92);
			this.comboCheck.Name = "comboCheck";
			this.comboCheck.Size = new System.Drawing.Size(80, 20);
			this.comboCheck.TabIndex = 2;
			this.comboCheck.Text = "n";
			// 
			// comboStop
			// 
			this.comboStop.Items.AddRange(new object[] {
														   "1",
														   "1.5",
														   "2"});
			this.comboStop.Location = new System.Drawing.Point(56, 116);
			this.comboStop.Name = "comboStop";
			this.comboStop.Size = new System.Drawing.Size(80, 20);
			this.comboStop.TabIndex = 2;
			this.comboStop.Text = "1";
			// 
			// comboComm
			// 
			this.comboComm.Items.AddRange(new object[] {
														   "COM1",
														   "COM2"});
			this.comboComm.Location = new System.Drawing.Point(56, 20);
			this.comboComm.Name = "comboComm";
			this.comboComm.Size = new System.Drawing.Size(80, 20);
			this.comboComm.TabIndex = 2;
			this.comboComm.Text = "COM1";
			// 
			// txtReceive
			// 
			this.txtReceive.Location = new System.Drawing.Point(152, 8);
			this.txtReceive.Multiline = true;
			this.txtReceive.Name = "txtReceive";
			this.txtReceive.Size = new System.Drawing.Size(352, 168);
			this.txtReceive.TabIndex = 1;
			this.txtReceive.Text = "";
			//this.txtReceive.TextChanged += new System.EventHandler(this.txtReceive_TextChanged);
			// 
			// txtSend
			// 
			this.txtSend.Location = new System.Drawing.Point(152, 184);
			this.txtSend.Multiline = true;
			this.txtSend.Name = "txtSend";
			this.txtSend.Size = new System.Drawing.Size(352, 56);
			this.txtSend.TabIndex = 2;
			this.txtSend.Text = "";
			// 
			// buttonExit
			// 
			this.buttonExit.Location = new System.Drawing.Point(8, 216);
			this.buttonExit.Name = "buttonExit";
			this.buttonExit.Size = new System.Drawing.Size(128, 23);
			this.buttonExit.TabIndex = 1;
			this.buttonExit.Text = "退出";
			this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
			// 
			// axMSComm1
			// 
			this.axMSComm1.Enabled = true;
			this.axMSComm1.Location = new System.Drawing.Point(376, 24);
			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 = 3;
			this.axMSComm1.OnComm += new System.EventHandler(this.axMSComm1_OnComm);
			// 
			// buttonSend
			// 
			this.buttonSend.Enabled = false;
			this.buttonSend.Location = new System.Drawing.Point(8, 184);
			this.buttonSend.Name = "buttonSend";
			this.buttonSend.Size = new System.Drawing.Size(128, 23);
			this.buttonSend.TabIndex = 1;
			this.buttonSend.Text = "发送";
			this.buttonSend.Click += new System.EventHandler(this.buttonSend_Click);
			// 
			// frmComm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(512, 245);
			this.Controls.Add(this.axMSComm1);
			this.Controls.Add(this.txtSend);
			this.Controls.Add(this.txtReceive);
			this.Controls.Add(this.buttonExit);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.buttonSend);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Name = "frmComm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "串口通信";
			this.Load += new System.EventHandler(this.frmComm_Load);
			this.groupBox1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmComm());
		}
		
		private void frmComm_Load(object sender, System.EventArgs e)
		{
		
		}

		private void buttonExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void buttonOpen_Click(object sender, System.EventArgs e)
		{
			if(buttonOpen.Text=="打开串口")
			{
				buttonSend.Enabled=true;
				string setting="";
				switch(comboComm.SelectedIndex)
				{
					case 0:
						axMSComm1.CommPort=1;
						break;
					case 1:
						axMSComm1.CommPort=2;
						break;
					default:
						axMSComm1.CommPort=1;
						break;
				}
				switch(comboRaud.SelectedIndex)
				{
					case 0:
						setting+="4800,";
						break;
					case 2:
						setting+="14400,";
						break;
					default:
						setting+="9600,";
						break;
				}
				switch(comboCheck.SelectedIndex)
				{
					case 1:
						setting+="1,";
						break;
					default:
						setting+="n,";
						break;

				}
				switch(comboData.SelectedIndex)
				{
					case 1:
						setting+="7,";
						break;
					default:
						setting+="8,";
						break;
				}
				switch(comboStop.SelectedIndex)
				{
					case 1:
						setting+="1.5";
						break;
					case 2:
						setting+="2";
						break;
					default:
						setting+="1";
						break;
				}
				axMSComm1.Settings=setting;
				axMSComm1.InBufferSize=1024;
				axMSComm1.OutBufferSize=512;
				axMSComm1.InputMode=0;
				axMSComm1.RThreshold=1;
				axMSComm1.InputLen=0;
				try
				{
					axMSComm1.PortOpen=true;
				}
				catch(Exception ex)
				{
					MessageBox.Show(this,"打开串口失败");
					return;
				}
				buttonOpen.Text="关闭串口";
				comboComm.Enabled=false;
				comboData.Enabled=false;
				comboStop.Enabled=false;
				comboCheck.Enabled=false;
				comboRaud.Enabled=false;
            }
			else
			{
				comboComm.Enabled=true;
				comboData.Enabled=true;
				comboStop.Enabled=true;
				comboCheck.Enabled=true;
				comboRaud.Enabled=true;
				buttonOpen.Text="打开串口";
				axMSComm1.PortOpen=false;
				buttonSend.Enabled=false;
				txtReceive.Text=null;
				
			}
			
		}

		private void buttonSend_Click(object sender, System.EventArgs e)
		{
			//string output=txtSend.Text;
			byte []output=new byte[12];
			for(int i=0;i<10;i++)
			{
				output[i]=(byte)(0x01+(byte)i);
			}
			try
			{
				axMSComm1.Output=output;
				txtSend.Text=null;
			}
			catch(Exception ex)
			{
				MessageBox.Show(this,"发送错误");
			}
		}
		private void axMSComm1_OnComm(object sender,EventArgs e)
		{
			int k;
			object input;
			if(axMSComm1.CommEvent==2)
			{
				k=axMSComm1.InBufferCount;
				if(k>0)
				{
					input=axMSComm1.Input;
					txtReceive.Text+=(string)input;

				}
			}
		}

		
	}
}

⌨️ 快捷键说明

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