SerialMonitor:C#写的串口调试程序

下载地址: 【北方网通】    【电信网通】

【下载说明】

1 点击上面的地址,打开下载页面

2 点击"普通下载"--等待30秒--点击"下载"按钮--保存

运行截图:

SerialMonitor:C#写的串口调试程序_第1张图片

点击右侧的“》”按钮,可以将接收窗最大化。

SerialMonitor:C#写的串口调试程序_第2张图片

会自动探测当前存在的串口。


可以在发送数据上添加新行、大写或转换为16进制。

SerialMonitor:C#写的串口调试程序_第3张图片

可以十六进制显示接收的数据,并可以设置接收窗的字体。

主要源程序:

/*
 * Created by SharpDevelop.
 * User: PenG
 * Date: 2012/10/31
 * Time: 15:56
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

using System.Diagnostics;
using System.IO.Ports;
using System.Text;

namespace SerialMonitor
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public SerialPort sp = new SerialPort();
		public bool isHexDisplay = false;
		
		int richHeight = 0;
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			Control.CheckForIllegalCrossThreadCalls = false;
			
			richHeight = this.richTextBox1.Height;
		}
		
		public void SetFont(){
			try{
				FontDialog fd = new FontDialog();
				if(fd.ShowDialog() == DialogResult.OK){
					this.richTextBox1.Font = fd.Font;
				}
			}catch(Exception e){
				ShowException(e.Message);
			}
		}
		
		public void ClearScreen(){
			this.richTextBox1.Text = "";
		}
		
		public void PrintText(string msg){
			this.richTextBox1.Text += msg;
		}
		
		public string GetText(){
			return this.richTextBox1.Text;
		}
		
		public void ShowException(string msg){
			MessageBox.Show(msg,"EXCEPTION",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
		}
		
		public void ShowInformation(string msg){
			MessageBox.Show(msg,"INFORMATION",MessageBoxButtons.OK,MessageBoxIcon.Information);
		}
		
		void MainFormLoad(object sender, EventArgs e)
		{
			this.richTextBox1.ScrollBars = RichTextBoxScrollBars.ForcedBoth;
			this.richTextBox1.MaxLength = int.MaxValue;
			
			sp.DataReceived += delegate {
				if(!this.isHexDisplay){
					this.richTextBox1.Text += sp.ReadLine();
				}else{
					this.richTextBox1.Text += BitConverter.ToString(System.Text.Encoding.Default.GetBytes(sp.ReadLine())).Replace("-"," ") + Environment.NewLine;
				}
				this.richTextBox1.SelectionStart = this.richTextBox1.TextLength;
				this.richTextBox1.ScrollToCaret();
			};
			
			this.label1.Text = "";
		}
		
		void LinkLabel1LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			try{
				Process.Start("http://blog.csdn.net/pengqianhe");
			}catch(Exception ex){
				ShowException(ex.Message);
			}
		}
		
		void Timer1Tick(object sender, EventArgs e)
		{
			if(sp.IsOpen){
				this.label1.BackColor = Color.Lime;
				this.label1.ForeColor = Color.Blue;
				this.label1.Text = sp.PortName + " is Opened.";
			}else{
				this.label1.BackColor = Color.Red;
				this.label1.ForeColor = Color.Yellow;
				this.label1.Text = sp.PortName + " is Closed.";
			}
		}
		
		void BtnOpenClick(object sender, EventArgs e)
		{
			try{
				SerialMonitor.PortForm pf = new PortForm(this);
				pf.ShowDialog();
			}catch(Exception ex){
				ShowException(ex.Message);
			}
		}
		
		void BtnCloseClick(object sender, EventArgs e)
		{
			try{
				if(sp.IsOpen)
					sp.Close();
			}catch(Exception ex){
				ShowException(ex.Message);
			}
		}
		
		void MainFormFormClosing(object sender, FormClosingEventArgs e)
		{
			BtnCloseClick(sender,e);
		}
		
		void BtnSendClick(object sender, EventArgs e)
		{
			try{
				SerialMonitor.SendForm sf = new SendForm(this);
				sf.ShowDialog();
			}catch(Exception ex){
				ShowException(ex.Message);
			}
		}
		
		void BtnAdvancedClick(object sender, EventArgs e)
		{
			try{
				SerialMonitor.AdvancedForm af = new AdvancedForm(this);
				af.ShowDialog();
			}catch(Exception ex){
				ShowException(ex.Message);
			}
		}
		
		void BtnHideClick(object sender, EventArgs e)
		{
			try{
				if(this.btnHide.Text.Equals(">>")){
					this.richHeight = this.richTextBox1.Height;
					this.richTextBox1.Height = this.Height - 25;
					this.btnHide.Text = "<<";
					
					this.btnOpen.Visible = false;
					this.btnClose.Visible = false;
					this.btnSend.Visible = false;
					this.btnAdvanced.Visible = false;
					this.label1.Visible = false;
					this.linkLabel1.Visible = false;
					
					this.MinimizeBox = false;
					this.MaximizeBox = false;
					this.FormBorderStyle = FormBorderStyle.FixedSingle;
				}else{
					this.richTextBox1.Height = this.richHeight;
					this.btnHide.Text = ">>";
					
					this.btnOpen.Visible = true;
					this.btnClose.Visible = true;
					this.btnSend.Visible = true;
					this.btnAdvanced.Visible = true;
					this.label1.Visible = true;
					this.linkLabel1.Visible = true;
					this.richTextBox1.Refresh();
					
					this.MaximizeBox = true;
					this.MinimizeBox = true;
					this.FormBorderStyle = FormBorderStyle.Sizable;
				}
			}catch(Exception ex){
				ShowException(ex.Message);
			}
		}
	}
}

【更多阅读】
  1. [原]C#将文本转换为图片,支持中文字符
  2. [转]开启“以后自动采用相同的动作处理此类文件”命令
  3. [原]Html2Pdf:C调用wkhtmltopdf的API来将Html转换为pdf文件
  4. [原]Android NDK开发环境的搭建,无需Cygwin
  5. [原]C#用firefox3.6下载yunfile的文件
  6. [原]DLP Coder:C#写的一个用于编辑DLP投影机程序的编辑器
  7. [译]C#水波纹效果控件的实现
  8. [原]EnvironmentVariableEditor:用C#编辑系统环境变量
  9. [译]C#实现同一个对话框打开目录和文件
  10. [原]Freeplot: MATLAB来帮你手动画图

你可能感兴趣的:(SerialMonitor:C#写的串口调试程序)