基本信息
源码名称:C# 串口数据接收与发送 示例源码
源码大小:0.21M
文件格式:.zip
开发语言:C#
更新时间:2018-10-12
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们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.Windows.Forms;
using System.IO.Ports;

namespace serialp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SerialPort sp = null;
        bool isOpen = false;
        bool isSetProperty = false;
        bool Enter_flag = false;
        bool Rece_flag = false;

        private void Form1_Load(object sender, EventArgs e)
        {
            this.MaximumSize = this.Size;
            this.MinimumSize = this.Size;
            this.MinimizeBox = false;
            for (int i = 0; i < 10; i  )
            {
                comboBox1.Items.Add("COM"   Convert.ToString(i   1));
            }
             comboBox2.Items.Add("1200");
             comboBox2.Items.Add("2400");
             comboBox2.Items.Add("4800");
             comboBox2.Items.Add("9600");
             comboBox2.Items.Add("19200");
             comboBox2.Items.Add("38400");
             comboBox2.Items.Add("43000");
             comboBox2.Items.Add("115200");
             comboBox2.SelectedIndex = 3;
        }
        void SetPortProperty()
        {
            sp = new SerialPort();
            sp.PortName = comboBox1.Text.Trim();
            sp.BaudRate = Convert.ToInt32(comboBox2.Text.Trim());
            sp.StopBits = StopBits.One;
            sp.Parity = Parity.None;
            sp.DataBits = 8;
            sp.RtsEnable = true;
            sp.ReadTimeout = -1;
            sp.DataReceived  = new SerialDataReceivedEventHandler(sp_DataReceived);
        }
        //接收数据
        private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            this.Invoke((EventHandler)(delegate
            {
                try
                {
                    float data;
                    string str = sp.ReadExisting();
                    textBox2.AppendText(str   " ");
                    textBox3.Text = str;
                    data = Convert.ToSingle(str);
                    AddData(data);


                }
                catch (Exception)
                {
                    MessageBox.Show("读数据出错", "错误提示!");
                }


                if (Rece_flag)
                    textBox2.Text  = "\r\n";


                this.textBox2.Focus();
                this.textBox2.Select(this.textBox1.TextLength, 0);
                this.textBox2.ScrollToCaret();
                sp.DiscardInBuffer();
            }));
        }
        private void AddData(float str)
        {
            this.textBox2.Text  = str   "\r\n";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            bool comExistence = false;
            comboBox1.Items.Clear();
            for (int i = 0; i < 10; i  )
            {
                try
                {
                    SerialPort sp = new SerialPort("COM"   Convert.ToString(i   1));
                    sp.Open();
                    sp.Close();
                    comboBox1.Items.Add("COM"   Convert.ToString(i   1));
                    comExistence = true;
                }
                catch (Exception)
                {
                    continue;
                }
            }
            if (comExistence)
            {

                comboBox1.SelectedIndex = 0;
            }
            else
            {
                MessageBox.Show("没有找到可用串口!", "错误提示");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
             if (isOpen == false)
             {
                 if (!isSetProperty)
                 {
                     isSetProperty = true;
                     SetPortProperty();
                 }
                 try
                 {
                     sp.Open();
                     isOpen = true;
                     button1.Enabled = false;
                     button2.BackColor = Color.Red;
                     button2.Text = "关闭";
                 }
                 catch (Exception)
                 {
                     isSetProperty = false;
                     isOpen = false;
                     MessageBox.Show("串口无效或已被占用!", "错误提示");
                 }
             }
             else 
             {
                 try
                 {
                     sp.Close();
                     isOpen = false;
                     isSetProperty = false;
                     button1.Enabled = true;
                     button2.BackColor = Color.White;
                     button2.Text = "开";
                         }
                 catch (Exception)
                 {
                     MessageBox.Show("串口异常断开!", "错误提示");
                      }
                 }
         
        }

    }
}