基于C#的上位机开发——串口工具

界面如下图基于C#的上位机开发——串口工具_第1张图片
源代码如下
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 serialport_app
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        int i;
        for (i = 1; i < 15; i++)
        { 
            comboBox1.Items.Add("COM"+i.ToString());
        }
        comboBox1.Text = "COM1";
        comboBox2.Text = "4800";
        //串口需要手动添加事件,另一种办法是线程的比较麻烦。注册一下串口函数,类似于函数声明
        serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
    }

    private void radioButton3_CheckedChanged(object sender, EventArgs e)
    {

    }
    //串口数据接收模式
    private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        if (!radioButton3.Checked)
        {
            string str;
            str = serialPort1.ReadExisting();
            textBox1.AppendText(str);
            //textBox1.Text+=str;
        }
        else 
        {
            byte data;
            data=(byte)serialPort1.ReadByte();
            string str2=Convert.ToString(data,16).ToUpper();
            textBox1.AppendText("0x"+(str2.Length==1?"0"+str2:str2));
        }

    }



    private void port_SendData(object sender, EventArgs e)
    {
        byte[] data = new byte[1];
        //传输只用一个字节,不停的更新这个字节就可以传输。
        if (serialPort1.IsOpen)
        {
            if (!radioButton1.Checked)//发送字符
            {
                if (textBox2.Text != " ")
                {
                    try
                    {
                        serialPort1.Write(textBox2.Text);
                    }
                    catch
                    {
                        MessageBox.Show("写异常", "错误");
                        serialPort1.Close();
                        button1.Enabled = true;
                        button2.Enabled = false;

                    }

                }
                else
                {
                    MessageBox.Show("请输入发送的字节", "提示");
                }




            }
            else //发送数值
            {
                for (int i = 0; i < (textBox2.Text.Length - textBox2.Text.Length % 2); i++)
                { 
                    data[0]=Convert.ToByte(textBox2.Text.Substring(i*0,2),16);
                    serialPort1.Write(data, 0, 1);
                }
                if ((textBox2.Text.Length % 2) != 0)
                {
                    data[0]= Convert.ToByte(textBox2.Text.Substring(textBox2.Text.Length-1, 1),16);
                    serialPort1.Write(data, 0, 1);
                }
            }
        }

    }

    private void OpenSerial(object sender, EventArgs e)
    {
        try
        {
            serialPort1.PortName = comboBox1.Text;
            serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text, 10);//转换十进制
            serialPort1.Open();
            button1.Enabled = false;
            button2.Enabled = true;
        }
        catch {
            MessageBox.Show("端口异常","错误");
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        serialPort1.Close();
        button1.Enabled=true;
        button2.Enabled=false;
    }
}

}

你可能感兴趣的:(c#,开发语言,visual,studio)