C#使用SerialPort控件编写简单的串口通信(发送和接收)程序

C#使用SerialPort控件编写简单的串口通信(发送和接收)程序因为工作需要,需要一个串口应用程序,但是网络上好多都比较杂,没有一个论坛是编写的比较完整大部分都没有源文件,所以想自己写一个简单的串口应用程序,方便大家使用!本人能力有限,编写一个简单的串口应用程序,难免不足之处见谅!

**一、程序运行结果展示**

1.

C#使用SerialPort控件编写简单的串口通信(发送和接收)程序_第1张图片

2.


二、程序解释

1.//初始化窗体加载串口
     
 
private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                foreach (string com in System.IO.Ports.SerialPort.GetPortNames())
                    comboBox1.Items.Add(com);
            }
            catch (Exception er)
            {
                MessageBox.Show("端口加载失败!" + er.Message, "提示");
            }
            comboBox2.SelectedIndex = 4;
            comboBox3.SelectedIndex = 2;
            comboBox4.SelectedIndex = 0;
            comboBox5.SelectedIndex = 1;
            comboBox1.SelectedIndex = 0;
        }

2.//打开串口
       
private void btnOpen_Click(object sender, EventArgs e)
        {


            try
            {
                if (serialPort1.IsOpen)
                { serialPort1.Close(); }
                else
                {
                    serialPort1.BaudRate = int.Parse(comboBox2.Text.Trim());
                    serialPort1.DataBits = int.Parse(comboBox3.Text.Trim());
                    serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBox4.Text.Trim());
                    serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox5.Text.Trim());
                    serialPort1.PortName = comboBox1.Text;
                    serialPort1.Open();
                }
                groupBox1.Enabled = !serialPort1.IsOpen;
                if (btnOpen.Text == "打开串口")
                {
                    btnOpen.Text = "关闭串口";
                    pictureBox1.Visible = true;
                    pictureBox2.Visible = false;
                }
                else if (btnOpen.Text == "关闭串口")
                {
                    btnOpen.Text = "打开串口";
                    pictureBox1.Visible = false;
                    pictureBox2.Visible = true;
                }
            }
            catch (Exception er)
            {
                MessageBox.Show("串口打开失败!" + er.Message, "提示");
            }
        }

3. //发送数据

//单击发送按钮事件

private void btnSend_Click(object sender, EventArgs e)
        {
            if (btnOpen.Text == "打开串口")
            {
                MessageBox.Show("请先打开串口!");
                return;
            }
            SendData();//发送数据传递textBox1.text中数据
        }

//发送数据调用的方法

 //向串口发送数据
        public void SendData()
        {
            for (int i = 0; i < textBox1.Lines.Count(); i++)
            {
                serialPort1.WriteLine(textBox1.Lines[i]);
            }
        }

4.接收数据

//接收数据
        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            //使用委托进行跨线程读取数据
            Invoke
             (new EventHandler
               (delegate
               {
                   textBox2.Text += serialPort1.ReadExisting();
               }
               )
              );
        }

三、程序源码

附上源码:

链接:http://download.csdn.net/detail/coderjyf/9479729

你可能感兴趣的:(C#,串口通信,SerialPort,发送和接收,简单通信)