程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> SerialMonitor:C#寫的串口調試程序

SerialMonitor:C#寫的串口調試程序

編輯: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 = ""; 
        } 
         
        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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved