SerialMonitor: C # Serial Port debugging program written

Source: Internet
Author: User


Click "" on the right to maximize the Receiving Window.


The existing serial port is automatically detected.


You can add new lines, uppercase letters, or convert them to hexadecimal notation on the sent data.


You can display received data in hexadecimal notation and set the font of the receiving window.
Main source program:

[Csharp]
/*
* Created by SharpDevelop.
* User: PenG
* Date: 2012/10/31
* Time:
*
* 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. checkforillegalcrossthreadcils = 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 );
}
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.