做的一个C#串口发送和接收的小软件

把串口的2、3短接
 
 把串口的2、3短接
 
 

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.PortName = comboBox1.Text.ToString();
                serialPort1.Open();
            }
            catch
            {
                return;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string s=textBox2.Text.ToString();
            try
            {
                this.serialPort1.Write(s);
            }
            catch
            {
                return;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                //int ss = this.serialPort1.BytesToRead;
                serialPort1.Dispose();
                this.serialPort1.Close();
            }
            catch
            {
               
                return;
            }
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            int i = 0;
            i = this.serialPort1.BytesToRead;
            CheckForIllegalCrossThreadCalls = false;
            this.textBox1.Text += this.serialPort1.ReadExisting();
        }
    }




原程序 http://files.cnblogs.com/slcfhr/chuankou.rar
把2、3针短接

你可能感兴趣的:(C#)