用C#在一台电脑上面做串口调试的程序代码

   今天要做一个串口的调试小程序,我就在一个例子的上面做了一些小的修改!!

代码如下:

 

sing 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 SerialPortTest
{
    public partial class Form1 : Form
    {
        SerialPort sp = null;
        bool IsOpen = false;
        bool IsSetProperty = false;

        public Form1()
        {
            InitializeComponent();
        }
       
        private void btnExit_Click(object sender, EventArgs e) //退出程序
        {
            sp.Close();
            Application.Exit();
        }

        private void btnFindPort_Click(object sender, EventArgs e) //检测可用的串口
        {
            PortChoose.Items.Clear();
                for (int i = 0; i <256; i++)
                {
                    try
                    {
                        SerialPort sp =new SerialPort("COM"+(i+1).ToString());
                        sp.Open();
                        sp.Close();
                        PortChoose.Items.Add("COM"+(i+1).ToString());

                    }
                    catch(Exception) 
                    {
                        labelState.Text= "COM"+(i+1).ToString()+"不可用";
                        continue;
                    }
                }
          
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.MaximumSize = this.Size;
            this.MinimumSize = this.Size;

            for (int i = 0; i < 256; i++)
            {
                PortChoose.Items.Add("COM" + (i + 1).ToString());
            }

        }
        private bool CheckPortSetting() //检查串口设置
        {
            if (PortChoose.Text.Trim() == "" || PortJiOu.Text.Trim() == "" || PortBaude.Text.Trim() == "" || PortData.Text.Trim() == "" || PortStop.Text.Trim() == "")
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        private bool SendData()
        {
            if (SendDataBox.Text.Trim() == "")
            { return false; }
            else
            { return true;}
        }
        private void SetPortProperty() //设置串口的属性
        {
            sp = new SerialPort();
            sp.PortName = PortChoose.Text.Trim();
            sp.BaudRate = Convert.ToInt32( PortBaude.Text.Trim());
           
            float f = Convert.ToSingle(PortStop.Text.Trim());
            if (f == 0)
            {
                sp.StopBits = StopBits.None;
            }
            else if (f == 1)
            {
                sp.StopBits = StopBits.One;
            }
            else if (f == 1.5)
            {
                sp.StopBits = StopBits.OnePointFive;
            }
            else if (f == 2)
            {
                sp.StopBits = StopBits.Two;
            }
            else
            {
                sp.StopBits = StopBits.One;
            }
            sp.DataBits = Convert.ToInt16(PortData.Text.Trim());
            //设置ji偶校验位
            string s = PortJiOu.Text.Trim();
            if (s.CompareTo("无")==0)
            {
                sp.Parity = Parity.None;
            }
            else if (s.CompareTo("奇校验") == 0)
            {
                sp.Parity = Parity.Odd;
            }
            else if (s.CompareTo("偶校验") == 0)
            {
                sp.Parity = Parity.Even;
            }
            else
            {
                sp.Parity = Parity.None;
            }
            sp.ReadTimeout = -1; //设置超时读取时间
          
            //打开串口
            try
            {
                sp.Open();
                IsOpen = true;
            }
            catch (Exception)
            {
                labelState.Text = "打开串口时发生错误!";
            }
        }

        private void btnSendData_Click(object sender, EventArgs e)  //发送数据
        {
            if (!CheckPortSetting())
            {
                MessageBox.Show("串口未设置!", "错误提示!");
                return;
            }
            if (!SendData())
            {
                MessageBox.Show("输入要发送的数据!", "错误提示!");
                return;
            }
            if (!IsSetProperty)
            {
                SetPortProperty();
                IsSetProperty = true;
            }
            if (IsOpen)
            {
                try
                {
                    sp.Close();
                    sp.Open();
                    sp.WriteLine(SendDataBox.Text);
                }
                catch (Exception)
                {
                    labelState.Text = "发送数据时发生错误!";
                    return;
                }
            }
            else
            {
                MessageBox.Show("串口未打开!","错误提示!");
                return;
            }
            SendDataBox.Text = "";
        }

        private void btnAcepptData_Click(object sender, EventArgs e)  /接收数据
        {
           
            if (IsOpen)
            {
                try
                {
                    //读取串口数据

                    AcceptDataBox.Text += sp.ReadLine();
                }
                catch (Exception)
                {
                    labelState.Text = "读取失败!";
                    return;
                }
                finally
                {
                    sp.Close();
                }
            }
            else
            {
                MessageBox.Show("串口未打开!", "提示!");
                return;
            }
        }
    }
}

 

你可能感兴趣的:(C#,Visual,Studio,2010,数据,串口)