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

📄 form1.cs

📁 单片机串行通信测试板程序
💻 CS
📖 第 1 页 / 共 2 页
字号:
			this.hex.TabIndex = 16;
			this.hex.Text = "HEX";
			// 
			// dec
			// 
			this.dec.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.dec.Location = new System.Drawing.Point(16, 24);
			this.dec.Name = "dec";
			this.dec.Size = new System.Drawing.Size(48, 16);
			this.dec.TabIndex = 15;
			this.dec.Text = "DEC";
			// 
			// Setup
			// 
			this.Setup.Cursor = System.Windows.Forms.Cursors.Hand;
			this.Setup.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.Setup.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.Setup.Image = ((System.Drawing.Bitmap)(resources.GetObject("Setup.Image")));
			this.Setup.Location = new System.Drawing.Point(280, 248);
			this.Setup.Name = "Setup";
			this.Setup.Size = new System.Drawing.Size(64, 40);
			this.Setup.TabIndex = 7;
			this.Setup.Text = "设置";
			this.Setup.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
			this.Setup.Click += new System.EventHandler(this.Setup_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(216, 328);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(232, 23);
			this.label1.TabIndex = 14;
			this.label1.Text = "苏州市职工科技大学实验中心";
			// 
			// axMSComm1
			// 
			this.axMSComm1.Enabled = true;
			this.axMSComm1.Location = new System.Drawing.Point(448, 304);
			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 = 15;
			this.axMSComm1.OnComm += new System.EventHandler(this.axMSComm1_OnComm);
			// 
			// button1
			// 
			this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.button1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.button1.Image = ((System.Drawing.Bitmap)(resources.GetObject("button1.Image")));
			this.button1.Location = new System.Drawing.Point(344, 248);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(64, 40);
			this.button1.TabIndex = 16;
			this.button1.Text = "终端";
			this.button1.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// pictureBox2
			// 
			this.pictureBox2.Image = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox2.Image")));
			this.pictureBox2.Location = new System.Drawing.Point(144, 288);
			this.pictureBox2.Name = "pictureBox2";
			this.pictureBox2.Size = new System.Drawing.Size(352, 40);
			this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureBox2.TabIndex = 17;
			this.pictureBox2.TabStop = false;
			// 
			// FormMain
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(8, 19);
			this.ClientSize = new System.Drawing.Size(498, 343);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.pictureBox2,
																		  this.button1,
																		  this.axMSComm1,
																		  this.label1,
																		  this.groupBox1,
																		  this.datasend,
																		  this.SendData,
																		  this.textBox3,
																		  this.Quit,
																		  this.Setup,
																		  this.ClrScr,
																		  this.pictureBox1,
																		  this.characterDisplay,
																		  this.textBox2,
																		  this.dataDisplay,
																		  this.textBox1});
			this.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Name = "FormMain";
			this.Text = "串行通信测试板";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.groupBox1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

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

		private void Form1_Load(object sender, System.EventArgs e)
		{
			this.textBox2.Text="00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F";
			this.hex.Select();
		}
		private void Setup_Click(object sender, System.EventArgs e)
		{

			Test testDialog =new Test();
			testDialog.Sinputmode=this.inputmode;

			if(testDialog.ShowDialog().ToString()=="OK")
			{
				try
				{
					this.portid=testDialog.Sportid;
					this.baud=testDialog.Sbaud;
					this.databit=testDialog.Sdatabit;
					this.stopbit=testDialog.Sstopbit;
					this.parity=testDialog.Sparity;
					this.inputmode=testDialog.Sinputmode;
					if(axMSComm1.PortOpen) axMSComm1.PortOpen = false;
					axMSComm1.CommPort =portid;
					if(axMSComm1.PortOpen) axMSComm1.PortOpen = false;
					axMSComm1.Settings = baud+','+parity+','+databit+','+stopbit;
					axMSComm1.InputLen = 0;
					axMSComm1.InBufferSize = 1024;
					axMSComm1.OutBufferSize = 1024;
					axMSComm1.SThreshold = 1;
					axMSComm1.RThreshold = 1;
					if(!this.inputmode)
                        axMSComm1.InputMode=(MSCommLib.InputModeConstants)0;
					else axMSComm1.InputMode=(MSCommLib.InputModeConstants)1;
					axMSComm1.PortOpen = true;
					this.textBox3.Text="COM"+portid+':'+baud+','+parity+','+databit+','+stopbit;				
				}
				catch(System.Runtime.InteropServices.COMException exception)
				{
					string fault;
					fault=exception.Message+"串口已经打开!";
					MessageBox.Show(fault,"错误提示",MessageBoxButtons.OK, MessageBoxIcon.Error);
				}	
			}
					
		}
		private void Quit_Click(object sender, System.EventArgs e)
		{
			if(this.axMSComm1.PortOpen)
				this.axMSComm1.PortOpen= false;
			this.Close();
		}
		private void ClrScr_Click(object sender, System.EventArgs e)
		{
			this.dataDisplay.Text="";
			this.characterDisplay.Text="";
			this.pictureBox1.Enabled=!this.pictureBox1.Enabled;
		}

		private void SendData_Click(object sender, System.EventArgs e)
		{
			try
			{
				this.axMSComm1.Output=this.datasend.Text;
//				string str=this.datasend.Text.ToString("X");
//				for(int i=0;i<=str.Length;i++)
//				{
//					this.axMSComm1.Output=this.dataDisplay.Text+((byte)str[i]).ToString("X");
//				}
				this.datasend.Text="";
			}
			catch(System.Runtime.InteropServices.COMException exception)
			{
				string fault;
				fault=exception.Message+"串口末打开";
				MessageBox.Show(fault,"错误提示");
			}
		}

		private void axMSComm1_OnComm(object sender, System.EventArgs e)
		{
			if(this.axMSComm1.CommEvent==(short)2)
			{
				//文本方式处理
				if(!this.inputmode)
				{
					string str=this.axMSComm1.Input.ToString();
					this.characterDisplay.Text=this.characterDisplay.Text+str;
					for(int i=0;i<=str.Length;i++)
					{
						this.dataDisplay.Text=this.dataDisplay.Text+((byte)str[i]).ToString("X")+" ";
					}
				}
				//二进制方式处理
				else
				{
					byte[] receive;
					receive=(byte[])axMSComm1.Input;
					for(int i=0;i<=receive.Length;i++)
					{
						this.dataDisplay.Text=this.dataDisplay.Text+((byte)receive[i]).ToString("X")+" ";
						this.characterDisplay.Text=this.characterDisplay.Text+(char)receive[i];
					}
				}
			}		
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			//声明一个程序信息类
			System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
			//设置外部程序名
			Info.FileName = "TERMINAL.EXE";
			//设置外部程序的启动参数(命令行参数)为test.txt
//			Info.Arguments = "test.txt";
			//设置外部程序工作目录
			Info.WorkingDirectory = @"F:\vc编程\";
			//声明一个程序类
			System.Diagnostics.Process Proc;
			//启动外部程序
			Proc = System.Diagnostics.Process.Start(Info);
			Proc.WaitForExit(3000);
			//如果这个外部程序没有结束运行则对其强行终止
			if(!Proc.HasExited == false)
			{
				Proc.Kill();
			}

		}
	}
}

⌨️ 快捷键说明

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