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

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

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


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Text.RegularExpressions;


namespace APP1
{
    public partial class fRame : Form
    {

        private StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建,定义到外面。
        public fRame()
        {
            InitializeComponent();
        }

        private void fRame_Load(object sender, EventArgs e) //装载主窗口
        {
            search_serial();                //搜寻可用串口号
            bandrate.SelectedIndex = bandrate.Items.IndexOf("9600");  //设置默认波特率
            datalen.SelectedIndex = datalen.Items.IndexOf("8");       //设置默认数据位
            stoplen.SelectedIndex = stoplen.Items.IndexOf("1");       //设置默认停止位
            crclen.SelectedIndex = crclen.Items.IndexOf("0");         //设置默认校验位

            //初始化SerialPort对象
            serialPort.NewLine = "\r\n";
            serialPort.RtsEnable = true;

            serialPort.Encoding = Encoding.GetEncoding("GB2312");

            oPen.Enabled  = true;
            cLose.Enabled = false;

         
        }

        private void oPen_Click(object sender, EventArgs e)         //打开串口按钮功能
        {
            serialPort.PortName = portname.Text;                                        //串口名称
            serialPort.BaudRate = int.Parse(bandrate.Text);                             //波特率
            serialPort.DataBits = int.Parse(datalen.Text);                              //数据长度
            serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits),stoplen.Text);   //停止位
            serialPort.Parity   = (Parity)Enum.Parse(typeof(Parity), crclen.Text);       //校验位

            try
            {
                serialPort.Open();
                sTate.Text = "串口打开";
                oPen.Enabled = false; //打开之后,失能
                cLose.Enabled = true;
            }
            catch
            {
                MessageBox.Show("串口打开错误","提示");
            }
            
        }

        private void cLose_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort.Close();
                sTate.Text = "";
                oPen.Enabled = true;
                cLose.Enabled = false;
            }
            catch
            {
                MessageBox.Show("串口关闭错误","提示");
            }
        }

        private void sEarch_Click(object sender, EventArgs e)   //搜寻串口按钮功能
        {
            search_serial();
        }
        public void search_serial()   //搜寻可用串口并添加到Port选择框
        {
            portname.Items.Clear();
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            portname.Items.AddRange(ports);
            if (portname.Items.Count == 0)
            {
                portname.Items.Add("COM1");        
            }
            portname.SelectedIndex = portname.Items.Count > 0 ? 0 : -1;
            
        }

        private void sErialport_receive(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            if (sWitch2.Checked)
            {
                this.Invoke(new Action(() =>
               {
                   txGet.AppendText(serialPort.ReadExisting());

               }));
            }
            else
            {
                this.Invoke(new Action(() =>
               {
                    int n = serialPort.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
                    byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据

                    serialPort.Read(buf, 0, n);//读取缓冲数据
                    builder.Clear();//清除字符串构造器的内容

                    //依次的拼接出16进制字符串
                    foreach (byte b in buf)
                    {
                        builder.Append(b.ToString("X2")   " ");
                    }
                    //追加的形式添加到文本框末端,并滚动到最后。
                    this.txGet.AppendText(builder.ToString());
               }));
            }
        }

        private void send1_Click(object sender, EventArgs e) //第一队列发送按钮功能
        {
            int send_num = 0;
            byte[] send_data = new byte[1];
            string Send_Data;
            if (serialPort.IsOpen)
            {
                if (sWitch4.Checked)        //选择字符格式发送
                {
                    try
                    {
                        serialPort.Write(txSend1.Text);
                    }
                    catch
                    {
                        MessageBox.Show("发送失败", "提示");
                    }
                }
                else
                {
                    Send_Data = txSend1.Text;
                    Send_Data = Send_Data.Replace(" ","");
                    Send_Data = Send_Data.Replace("0X","");
                    Send_Data = Send_Data.Replace("0x","");
                    for (int i = 0; i < (Send_Data.Length - Send_Data.Length % 2) / 2; i  )//转换偶数个
                    {
                        send_data[0] = Convert.ToByte(Send_Data.Substring(i * 2, 2), 16);           //转换
                        serialPort.Write(send_data,0,1);
                    }
                    if (Send_Data.Length % 2 != 0)
                    {
                        send_data[0] = Convert.ToByte(txSend1.Text.Substring(Send_Data.Length - 1, 1), 16);//单独处理最后一个字符
                        serialPort.Write(send_data, 0, 1);                              //写入
                    }
                    
                }

            }
            else
            {
                MessageBox.Show("串口未打开","提示");
            }
            
        }

        private void send2_Click(object sender, EventArgs e)
        {

        }

        private void send3_Click(object sender, EventArgs e)
        {

        }

        private void cLear_Click(object sender, EventArgs e)
        {
             this.Invoke(new Action(() =>
            {
                txGet.Text   = "";
                //txSend1.Text = "";
                //txSend2.Text = "";
                //txSend3.Text = "";
            }));
        }






    }
}