在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 串口波形分析

C# 串口波形分析

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:3.34M
  • 下载次数:173
  • 浏览次数:1198
  • 发布时间:2018-11-28
  • 实例类别:C#语言基础
  • 发 布 人:readlove1
  • 文件格式:.zip
  • 所需积分:2
 相关标签: C# 串口 c

实例介绍

【实例简介】C# 串口波形分析
【实例截图】from clipboard

【核心代码】

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO.Ports;
using System.Text;
using System.Windows.Forms;

namespace Drawer
{
	public class MainForm : Form
	{
		private Drawer Displayer;

		private IContainer components;

		private TextBox textBox2;

		private Button button3;

		private TextBox textBox1;

		private Label 端口;

		public ComboBox comboBox1;

		private Label label1;

		private ComboBox comboBox2;

		private Label label2;

		private Label label3;

		private Panel panel1;

		private RadioButton radioButton2;

		private RadioButton radioButton1;

		private Panel panel2;

		private RadioButton radioButton3;

		private RadioButton radioButton4;

		private GroupBox groupBox1;

		private Button button4;

		private Button button2;

		private Button button1;

		private SerialPort serialPort1;

		private PictureBox pictureBox1;

		private Button button5;

		public MainForm()
		{
			this.InitializeComponent();
			this.serialPort1.Encoding = Encoding.GetEncoding("GB2312");
			Control.CheckForIllegalCrossThreadCalls = false;
		}

		public void ClosePort()
		{
			try
			{
				this.serialPort1.Close();
				this.button1.Enabled = true;
				this.button2.Enabled = false;
			}
			catch (Exception)
			{
			}
		}

		private Point GetMyPos()
		{
			return base.Location;
		}

		public void OpenPort()
		{
			try
			{
				this.serialPort1.Open();
				this.button1.Enabled = false;
				this.button2.Enabled = true;
			}
			catch (Exception)
			{
				MessageBox.Show("串口打开失败,请检查", "错误");
			}
		}

		public void ShowMe()
		{
			base.Show();
		}

		public void HideMe()
		{
			base.Hide();
		}

		private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
		{
			if (!this.radioButton3.Checked)
			{
				this.textBox1.AppendText(this.serialPort1.ReadExisting());
			}
			else
			{
				try
				{
					byte[] array = new byte[this.serialPort1.BytesToRead];
					this.serialPort1.Read(array, 0, array.Length);
					if (this.Displayer != null)
					{
						this.Displayer.AddData(array);
					}
					byte[] array2 = array;
					foreach (byte value in array2)
					{
						string text = Convert.ToString(value, 16).ToUpper();
						this.textBox1.AppendText("0x"   ((text.Length == 1) ? ("0"   text) : text)   " ");
					}
				}
				catch
				{
				}
			}
		}

		private void CreateNewDrawer()
		{
			this.Displayer = new Drawer();
			this.Displayer.ShowMainWindow = this.ShowMe;
			this.Displayer.HideMainWindow = this.HideMe;
			this.Displayer.GetMainPos = this.GetMyPos;
			this.Displayer.CloseSerialPort = this.ClosePort;
			this.Displayer.OpenSerialPort = this.OpenPort;
			this.Displayer.GetMainWidth = this.GetMyWidth;
			this.Displayer.Show();
		}

		private int GetMyWidth()
		{
			return base.Width;
		}

		private void CreateDisplayer()
		{
			base.Left = 0;
			this.CreateNewDrawer();
			Rectangle workingArea = Screen.GetWorkingArea(this);
			this.Displayer.SetWindow(workingArea.Width - base.Width, new Point(base.Width, base.Top));
		}

		private void button4_Click(object sender, EventArgs e)
		{
			if (this.Displayer == null)
			{
				this.CreateDisplayer();
			}
			else if (this.Displayer.IsDisposed)
			{
				this.CreateDisplayer();
			}
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			for (int i = 1; i < 20; i  )
			{
				this.comboBox1.Items.Add("COM"   i.ToString());
			}
			this.comboBox1.Text = "COM1";
			this.comboBox2.Text = "4800";
		}

		private void button1_Click(object sender, EventArgs e)
		{
			try
			{
				this.serialPort1.PortName = this.comboBox1.Text;
				this.serialPort1.BaudRate = Convert.ToInt32(this.comboBox2.Text);
				this.serialPort1.Open();
				this.button1.Enabled = false;
				this.button2.Enabled = true;
			}
			catch
			{
				MessageBox.Show("端口错误", "错误");
			}
		}

		private void button3_Click(object sender, EventArgs e)
		{
			byte[] array = new byte[1];
			if (this.serialPort1.IsOpen && this.textBox2.Text != "")
			{
				if (!this.radioButton1.Checked)
				{
					try
					{
						this.serialPort1.Write(this.textBox2.Text);
					}
					catch
					{
						MessageBox.Show("串口数据写入错误", "错误");
					}
				}
				else
				{
					try
					{
						for (int i = 0; i < (this.textBox2.Text.Length - this.textBox2.Text.Length % 2) / 2; i  )
						{
							array[0] = Convert.ToByte(this.textBox2.Text.Substring(i * 2, 2), 16);
							this.serialPort1.Write(array, 0, 1);
						}
						if (this.textBox2.Text.Length % 2 != 0)
						{
							array[0] = Convert.ToByte(this.textBox2.Text.Substring(this.textBox2.Text.Length - 1, 1), 16);
							this.serialPort1.Write(array, 0, 1);
						}
					}
					catch
					{
						MessageBox.Show("数据转换错误,请输入数字。", "错误");
					}
				}
			}
		}

		private void button2_Click(object sender, EventArgs e)
		{
			try
			{
				this.serialPort1.Close();
				this.button1.Enabled = true;
				this.button2.Enabled = false;
			}
			catch
			{
			}
		}

		private void pictureBox1_Click(object sender, EventArgs e)
		{
			Process.Start("explorer.exe", "http://www.doyoung.net");
		}

		private void button5_Click(object sender, EventArgs e)
		{
			Process.Start("notepad.exe", "快捷键说明.txt");
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			this.components = new Container();
			ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(MainForm));
			this.textBox2 = new TextBox();
			this.button3 = new Button();
			this.textBox1 = new TextBox();
			this.端口 = new Label();
			this.comboBox1 = new ComboBox();
			this.label1 = new Label();
			this.comboBox2 = new ComboBox();
			this.label2 = new Label();
			this.label3 = new Label();
			this.panel1 = new Panel();
			this.radioButton2 = new RadioButton();
			this.radioButton1 = new RadioButton();
			this.panel2 = new Panel();
			this.radioButton3 = new RadioButton();
			this.radioButton4 = new RadioButton();
			this.groupBox1 = new GroupBox();
			this.button4 = new Button();
			this.button2 = new Button();
			this.button1 = new Button();
			this.serialPort1 = new SerialPort(this.components);
			this.pictureBox1 = new PictureBox();
			this.button5 = new Button();
			this.panel1.SuspendLayout();
			this.panel2.SuspendLayout();
			this.groupBox1.SuspendLayout();
			((ISupportInitialize)this.pictureBox1).BeginInit();
			base.SuspendLayout();
			this.textBox2.ImeMode = ImeMode.On;
			this.textBox2.Location = new Point(12, 453);
			this.textBox2.Multiline = true;
			this.textBox2.Name = "textBox2";
			this.textBox2.ScrollBars = ScrollBars.Vertical;
			this.textBox2.Size = new Size(211, 28);
			this.textBox2.TabIndex = 9;
			this.button3.Font = new Font("宋体", 9f, FontStyle.Regular, GraphicsUnit.Point, 134);
			this.button3.Location = new Point(229, 453);
			this.button3.Name = "button3";
			this.button3.Size = new Size(78, 28);
			this.button3.TabIndex = 8;
			this.button3.Text = "发送";
			this.button3.UseVisualStyleBackColor = true;
			this.button3.Click  = this.button3_Click;
			this.textBox1.BackColor = SystemColors.Menu;
			this.textBox1.Location = new Point(12, 285);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.ScrollBars = ScrollBars.Vertical;
			this.textBox1.Size = new Size(295, 162);
			this.textBox1.TabIndex = 7;
			this.端口.AutoSize = true;
			this.端口.Location = new Point(27, 23);
			this.端口.Name = "端口";
			this.端口.Size = new Size(29, 12);
			this.端口.TabIndex = 0;
			this.端口.Text = "端口";
			this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
			this.comboBox1.FormattingEnabled = true;
			this.comboBox1.Location = new Point(111, 20);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new Size(153, 20);
			this.comboBox1.TabIndex = 1;
			this.label1.AutoSize = true;
			this.label1.Location = new Point(27, 52);
			this.label1.Name = "label1";
			this.label1.Size = new Size(41, 12);
			this.label1.TabIndex = 2;
			this.label1.Text = "波特率";
			this.comboBox2.AutoCompleteCustomSource.AddRange(new string[8]
			{
				"600",
				"1200",
				"2400",
				"4800",
				"9600",
				"14400",
				"19200",
				"115200"
			});
			this.comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;
			this.comboBox2.FormattingEnabled = true;
			this.comboBox2.Items.AddRange(new object[8]
			{
				"600",
				"1200",
				"2400",
				"4800",
				"9600",
				"14400",
				"19200",
				"115200"
			});
			this.comboBox2.Location = new Point(113, 49);
			this.comboBox2.Name = "comboBox2";
			this.comboBox2.Size = new Size(151, 20);
			this.comboBox2.TabIndex = 3;
			this.label2.AutoSize = true;
			this.label2.Location = new Point(27, 87);
			this.label2.Name = "label2";
			this.label2.Size = new Size(53, 12);
			this.label2.TabIndex = 5;
			this.label2.Text = "发送模式";
			this.label3.AutoSize = true;
			this.label3.Location = new Point(27, 121);
			this.label3.Name = "label3";
			this.label3.Size = new Size(53, 12);
			this.label3.TabIndex = 8;
			this.label3.Text = "接收模式";
			this.panel1.Controls.Add(this.radioButton2);
			this.panel1.Controls.Add(this.radioButton1);
			this.panel1.Location = new Point(113, 84);
			this.panel1.Name = "panel1";
			this.panel1.Size = new Size(155, 27);
			this.panel1.TabIndex = 4;
			this.radioButton2.AutoSize = true;
			this.radioButton2.Location = new Point(104, 3);
			this.radioButton2.Name = "radioButton2";
			this.radioButton2.Size = new Size(47, 16);
			this.radioButton2.TabIndex = 9;
			this.radioButton2.Text = "字符";
			this.radioButton2.UseVisualStyleBackColor = true;
			this.radioButton1.AutoSize = true;
			this.radioButton1.Checked = true;
			this.radioButton1.Location = new Point(3, 3);
			this.radioButton1.Name = "radioButton1";
			this.radioButton1.Size = new Size(47, 16);
			this.radioButton1.TabIndex = 8;
			this.radioButton1.TabStop = true;
			this.radioButton1.Text = "数值";
			this.radioButton1.UseVisualStyleBackColor = true;
			this.panel2.Controls.Add(this.radioButton3);
			this.panel2.Controls.Add(this.radioButton4);
			this.panel2.Location = new Point(113, 117);
			this.panel2.Name = "panel2";
			this.panel2.Size = new Size(155, 27);
			this.panel2.TabIndex = 10;
			this.radioButton3.AutoSize = true;
			this.radioButton3.Checked = true;
			this.radioButton3.Location = new Point(3, 3);
			this.radioButton3.Name = "radioButton3";
			this.radioButton3.Size = new Size(47, 16);
			this.radioButton3.TabIndex = 9;
			this.radioButton3.TabStop = true;
			this.radioButton3.Text = "数值";
			this.radioButton3.UseVisualStyleBackColor = true;
			this.radioButton4.AutoSize = true;
			this.radioButton4.Location = new Point(104, 3);
			this.radioButton4.Name = "radioButton4";
			this.radioButton4.Size = new Size(47, 16);
			this.radioButton4.TabIndex = 8;
			this.radioButton4.Text = "字符";
			this.radioButton4.UseVisualStyleBackColor = true;
			this.groupBox1.Controls.Add(this.button5);
			this.groupBox1.Controls.Add(this.button4);
			this.groupBox1.Controls.Add(this.button2);
			this.groupBox1.Controls.Add(this.panel2);
			this.groupBox1.Controls.Add(this.panel1);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.button1);
			this.groupBox1.Controls.Add(this.comboBox2);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.comboBox1);
			this.groupBox1.Controls.Add(this.端口);
			this.groupBox1.Location = new Point(12, 12);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new Size(295, 267);
			this.groupBox1.TabIndex = 6;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "设置";
			this.button4.Location = new Point(29, 150);
			this.button4.Name = "button4";
			this.button4.Size = new Size(239, 23);
			this.button4.TabIndex = 12;
			this.button4.Text = "波形显示";
			this.button4.UseVisualStyleBackColor = true;
			this.button4.Click  = this.button4_Click;
			this.button2.Enabled = false;
			this.button2.Location = new Point(29, 208);
			this.button2.Name = "button2";
			this.button2.Size = new Size(239, 23);
			this.button2.TabIndex = 11;
			this.button2.Text = "关闭端口";
			this.button2.UseVisualStyleBackColor = true;
			this.button2.Click  = this.button2_Click;
			this.button1.Location = new Point(29, 179);
			this.button1.Name = "button1";
			this.button1.Size = new Size(239, 23);
			this.button1.TabIndex = 4;
			this.button1.Text = "打开端口";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click  = this.button1_Click;
			this.serialPort1.DataReceived  = this.serialPort1_DataReceived;
			this.pictureBox1.Cursor = Cursors.Hand;
			//this.pictureBox1.Image = (Image)componentResourceManager.GetObject("pictureBox1.Image");
			this.pictureBox1.Location = new Point(12, 487);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new Size(295, 77);
			this.pictureBox1.TabIndex = 10;
			this.pictureBox1.TabStop = false;
			this.pictureBox1.Click  = this.pictureBox1_Click;
			this.button5.Location = new Point(29, 238);
			this.button5.Name = "button5";
			this.button5.Size = new Size(239, 23);
			this.button5.TabIndex = 13;
			this.button5.Text = "快捷键说明";
			this.button5.UseVisualStyleBackColor = true;
			this.button5.Click  = this.button5_Click;
			base.AutoScaleDimensions = new SizeF(6f, 12f);
			base.AutoScaleMode = AutoScaleMode.Font;
			base.ClientSize = new Size(319, 576);
			base.Controls.Add(this.pictureBox1);
			base.Controls.Add(this.textBox2);
			base.Controls.Add(this.button3);
			base.Controls.Add(this.textBox1);
			base.Controls.Add(this.groupBox1);
			base.FormBorderStyle = FormBorderStyle.FixedSingle;
			base.MaximizeBox = false;
			base.Name = "MainForm";
			this.Text = "串口波形分析V1.0";
			base.Load  = this.MainForm_Load;
			this.panel1.ResumeLayout(false);
			this.panel1.PerformLayout();
			this.panel2.ResumeLayout(false);
			this.panel2.PerformLayout();
			this.groupBox1.ResumeLayout(false);
			this.groupBox1.PerformLayout();
			((ISupportInitialize)this.pictureBox1).EndInit();
			base.ResumeLayout(false);
			base.PerformLayout();
		}
	}
}

标签: C# 串口 c

实例下载地址

C# 串口波形分析

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警