串口助手1.0.0

别等时光非礼了梦想. 提交于 2019-12-01 19:05:10
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ChuanKou
{
    public partial class Serial : Form
    {
        public Serial()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// form页面加载数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Serial_Load(object sender, EventArgs e)
        {
            for(int i = 1; i < 20; i++)
            {
                comboBox1.Items.Add("COM" + i.ToString());
            }
            comboBox1.Text = "COM1";//设置端口的默认值
            comboBox2.Text = "4800";//设置波特的默认值

            /***************非常重要*********************/
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
        }
        /// <summar
        /// 处理串口接收的数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void port_DataReceived(object sender,SerialDataReceivedEventArgs e)
        {
            if (!radioButton2.Checked)
            {
                //如果接收模式为字符模式
                string str = serialPort1.ReadExisting();//字符串方式读
                textBox1.AppendText(str);//添加内容
            }
            else
            {
                //如果接收模式为数值模式接收
                byte data;
                data=(byte)serialPort1.ReadByte();//此处需要强制装换,将int类型转换为byte类型
                string str = Convert.ToString(data, 16).ToUpper();//转换为大写的十六进制的字符串
                textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");//空位补0

            }
        }
        /// <summary>
        /// 打开端口事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void open_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.PortName = port.Text;//获取端口信息
                serialPort1.BaudRate = Convert.ToInt32(bote.Text,10);//波特率十进制数据转换
                serialPort1.Open();
                open.Enabled = false; //打开串口按钮不可用
                close.Enabled = true;//关闭串口可用
            }
            catch
            {
                MessageBox.Show("端口错误,请检查串口", "错误");
            }
        }
        /// <summary>
        /// 关闭串口事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void close_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Close();//关闭串口
                open.Enabled = true;//打开串口可用
                close.Enabled = false;//关闭串口不可用
            }
            catch(Exception err)
            {
                //一般情况下不会出错,所以不做处理
            }
        }
        /// <summary>
        /// 发送按钮的点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            byte[] Data = new byte[1];//作用同上集
            if (serialPort1.IsOpen)
            {
                //判断串口是否打开,如果打开了执行下一步
                if (textBox2.Text != "")
                {
                    //发送的信息不为空
                    if (!radioButton1.Checked)
                    {
                        //如果发送模式为字符模式
                        try
                        {
                            serialPort1.WriteLine(textBox2.Text);//写数据

                        }
                        catch (Exception err)
                        {
                            MessageBox.Show("串口数据写入错误", "错误");
                            serialPort1.Close();//关闭串口
                            open.Enabled = true;//调整打开和关闭
                            open.Enabled = false;
                        }
                    }
                    else
                    {
                        try
                        {
                            //如果发送的是数值
                            for (int i = 0; i < (textBox2.Text.Length - textBox2.Text.Length % 2) / 2; i++)
                            {
                                Data[0] = Convert.ToByte(textBox2.Text.Substring(i * 2, 2), 16);
                                serialPort1.Write(Data, 0, 1);//循环发送,如果输入字符为0A0ABB,则只发送0A 0B
                            }
                            if (textBox2.Text.Length % 2 != 0)
                            {
                                //剩余一位单独处理
                                Data[0] = Convert.ToByte(textBox2.Text.Substring(textBox2.Text.Length - 1, 1), 16);//单独发送B(0B)
                                serialPort1.Write(Data, 0, 1);
                            }
                        }
                        catch (Exception err)
                        {
                            MessageBox.Show("串口数据写入错误", "错误");
                            serialPort1.Close();//关闭串口
                            open.Enabled = true;//调整打开和关闭
                            open.Enabled = false;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 文本框1的处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}效果图:

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!