基本信息
源码名称:C# 串口调试助手 源码下载
源码大小:0.09M
文件格式:.rar
开发语言:C#
更新时间:2016-10-08
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

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

本次赞助数额为: 1 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Collections;
using System.Threading;
namespace ComAssistant
{
    public partial class Form1 : Form
    {
        /// <remarks></remarks>
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int ii = 0;
            foreach (string s in SerialPort.GetPortNames())
            {
                if (ii >= 1)
                {
                    comboBox1.Items.Add(s);
                }
                ii  ;
            }

            if (SerialPort.GetPortNames().Length != 0)
            {
                comboBox1.Text = (string)comboBox1.Items[0];
            }

            string[] ss = new string[] { "9600", "19200", "57600", "115200" };
            comboBox2.DataSource = ss;

            comboBox3.DataSource = Enum.GetNames(typeof(Parity));

            ss = new string[] { "5", "6", "7", "8" };
            comboBox4.DataSource = ss;
            comboBox4.Text = "8";

            comboBox5.DataSource = Enum.GetNames(typeof(StopBits));
            comboBox5.Text = Enum.Format(typeof(StopBits), StopBits.One, "G");

            backgroundWorker1.RunWorkerAsync();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (serialPort1.IsOpen)
                {
                    serialPort1.Close();
                }
                else
                {
                    serialPort1.PortName = comboBox1.Text;
                    serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
                    serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox3.Text);
                    serialPort1.DataBits = Int32.Parse(comboBox4.Text);
                    serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBox5.Text);
                    serialPort1.Encoding = Encoding.GetEncoding("Gb2312");
                    serialPort1.Open();
                }
                RefrespictureBox1();
            }
            catch (Exception)
            { }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string s = "";
                if (checkBox1.Checked)
                {
                    ArrayList al = MyClass.Str16ToArrayList(textBox2.Text);
                    byte[] by = new byte[al.Count];
                    int i = 0;
                    foreach (string stmp in al)
                    {
                        by[i]  = Convert.ToByte(stmp, 16);
                        i  ;
                    }
                    s = Encoding.GetEncoding("Gb2312").GetString(by);
                }
                else
                {
                    s = textBox2.Text;
                }
                serialPort1.Write(s);
            }
            catch (Exception)
            { }
        }
        delegate void SetTextCallback(string text);
        private void SetText(string text)
        {
            try
            {
                if (this.richTextBox2.InvokeRequired)
                {
                    SetTextCallback d = new SetTextCallback(SetText);
                    this.Invoke(d, new object[] { text });
                }
                else
                {
                    this.richTextBox2.Text  = text;
                }
            }
            catch (Exception)
            {
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            char[] c;
            while (true)
            {
                try
                {
                    if (serialPort1.IsOpen)
                    {
                        c = new char[serialPort1.BytesToRead];
                        serialPort1.Read(c, 0, c.Length);
                        if (c.Length > 0)
                        {
                            if (checkBox2.Checked)
                            {
                                byte[] by = Encoding.GetEncoding("Gb2312").GetBytes(c);
                                foreach (byte btmp in by)
                                {
                                    string s = "00"   string.Format("{0:X}", btmp);
                                    s = s.Substring(s.Length - 2, 2);
                                    SetText(new string(s.ToCharArray()));
                                    SetText(" ");
                                }
                            }
                            else
                            {
                                SetText(new string(c));
                            }
                        }
                    }                 
                }
                catch (Exception) { }
                Thread.Sleep(1);
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Properties.Settings.Default.Save();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            string s = "";
            if (checkBox1.CheckState == CheckState.Checked)
            {
                byte[] by = Encoding.GetEncoding("Gb2312").GetBytes(textBox2.Text);
                textBox2.Text = "";
                foreach (byte btmp in by)
                {
                    s = "00"   string.Format("{0:X}", btmp);
                    textBox2.Text  = s.Substring(s.Length - 2, 2);
                    textBox2.Text  = " ";
                }
            }
            else
            {
                ArrayList al = MyClass.Str16ToArrayList(textBox2.Text);
                byte[] by = new byte[al.Count];
                int i = 0;
                foreach (string stmp in al)
                {
                    by[i]  = Convert.ToByte(stmp, 16);
                    i  ;
                }
                textBox2.Text = Encoding.GetEncoding("Gb2312").GetString(by);
            }
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }

        public void RefrespictureBox1()
        {
            if (serialPort1.IsOpen)
            {
                button1.BackColor = SystemColors.GradientActiveCaption;
                button1.Text = "断开";
            }
            else
            {
                button1.BackColor = SystemColors.Control;
                button1.Text = "连接";
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            richTextBox2.Text = "";
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (checkBox1.CheckState == CheckState.Checked)
            {
                if (!Uri.IsHexDigit(e.KeyChar) && e.KeyChar != ' ')
                {
                    e.KeyChar = (char)0;
                }
            }
        }
    }
}