基本信息
源码名称:C# 串口波形分析
源码大小:3.34M
文件格式:.zip
开发语言:C#
更新时间:2018-11-28
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
C# 串口波形分析

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();
		}
	}
}