当前位置:编程学习 > C#/ASP.NET >>

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


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


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


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


可以十六进制显示接收的数据,并可以设置接收窗的字体。
主要源程序:

[csharp] 
/*
 * 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 = ""; 
  

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,